#!/usr/bin/env ruby ################################################################################ # # sclapi (Socialight command-line API script) # author: naveen selvadurai (naveen@socialight.com) # date: 2008-04-25 # # Allows you to access some parts of the Socialight API via the command-line. # Be sure to enter proper access details in the 'CONFIGURATION' section below. # The script uses Yahoo's Geocoding service to convert your free-text location # into a latitude/longitude. # # # EXAMPLE ---------------------------------------------------------------------- # # $ ./sclapi.rb 10013 shoes # => All # Anbar Shoes (0.37 miles away) # 60 Reade Street New York 10007 # http://www.stevenalan.com/ (0.13 miles away) # 103 Franklin Street, New York, NY # Shop: Té Casan (0.28 miles away) # 382 West Broadway, new york new york # Bloomingdale's SoHo (0.33 miles away) # 504 Broadway, New York, NY # Opening Ceremony (0.22 miles away) # 35 Howard street, 10013 # # => People I'm following # Shop: Té Casan (0.28 miles away) # 382 West Broadway, new york new york # Opening Ceremony (0.22 miles away) # 35 Howard street, 10013 # New favorite Soho Boutique (0.55 miles away) # 83 Mercer St 10012 # really great discount department store (0.76 miles away) # 22 Cortlandt Street, NY, NY # Babydoll Dresses and Tunics Galore (0.55 miles away) # 281 Mott St 10012 # # => My notes # Prada (0.49 miles away) # 575 Broadway # Reservoir dogs (4.83 miles away) # 40.782166, -73.962570 # # LICENSE ---------------------------------------------------------------------- # # The MIT License # # Copyright (c) 2007, 2008 Naveen Selvadurai # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # ################################################################################ require 'net/http' require 'rexml/document' require 'cgi' # CONFIGURATION ---------------------------------------------------------------- YAHOO_APPID = "YD-D48tnoo_JX3JA_tXSiXvGaHTKJbPq4nQJw--" SOCIALIGHT_APPID = '' SOCIALIGHT_USERNAME = '' SOCIALIGHT_PASSWORD = '' # REST ------------------------------------------------------------------------- def geocode(location) location = CGI::escape(location) u = "/MapsService/V1/geocode?appid=#{YAHOO_APPID}" u += "&location=#{location}" http = Net::HTTP.new('local.yahooapis.com') http.start do |http| request = Net::HTTP::Get.new(u) response = http.request(request) doc = REXML::Document.new(response.body) # the nice thing here...even if multiple Results, this will take the first one. easy for now @location = Hash.new @location['latitude'] = doc.get_text('ResultSet/Result/Latitude') @location['longitude'] = doc.get_text('ResultSet/Result/Longitude') end @location end def fetch(endpoint, parameters = nil) http = Net::HTTP.new('socialight.com') http.start do |http| u = "/api/#{endpoint}?appid=#{SOCIALIGHT_APPID}" u += "&" + parameters if !parameters.nil? request = Net::HTTP::Get.new(u) request.basic_auth SOCIALIGHT_USERNAME, SOCIALIGHT_PASSWORD response = http.request(request) doc = REXML::Document.new(response.body) puts "Error: " + doc.root.get_text("/error").to_s if doc.root.get_text("/error") doc end end def layers l = Hash.new fetch('layers').elements.each('layers/layer') do |e| l[e.get_text('link').to_s] = e.get_text('name').to_s end l end def layer(parameters = nil) l = Hash.new fetch('layer', parameters).elements.each('result/note') do |n| l[n.get_text('id').to_s] = n.get_text('title').to_s + " (" + n.get_text('distance').to_s + " miles away)\n " + n.get_text('address').to_s end l end if ARGV[0] @location = geocode(ARGV[0]) @search = ARGV[1] ? CGI::escape(ARGV[1]) : "" if @location and @location['latitude'] layers.each do |link,title| notes = layer("#{link}&latitude=#{@location['latitude']}&longitude=#{@location['longitude']}&what=#{@search}") puts "=> #{title}\n" notes.each do |nk,nv| puts " #{nv}\n" end puts "\n" end else puts "Sorry, unable to parse location. Please enter a proper address as the first argument." end else puts "Usage: sclapi [location] [query]\n" puts " location : a free-form text string to indicate your location (10013, 'New York City', ...)\n" puts " query : a string indicating what you want to search for (optional)" end