Skip to content

Instantly share code, notes, and snippets.

@tonyheupel
Created August 4, 2011 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonyheupel/1124263 to your computer and use it in GitHub Desktop.
Save tonyheupel/1124263 to your computer and use it in GitHub Desktop.
Consuming/using the JSON API from Ruby
require 'net/http'
class Search
URL = 'http://api.ischyrus.com/JsonSearchApiService.svc/json/'
def initialize(api_key)
@api_key = api_key
end
def search(category, term, client_ip, user_agent)
url = "#{URL}?apiKey=#{URI.escape(@api_key)}&term=#{URI.escape(term)}&ip=#{URI.escape(client_ip)}&agent=User-agent:%20#{URI.escape(user_agent)}&cat=#{URI.escape(category)}"
response = Net::HTTP.get_response(URI.parse(url))
JSON.parse(response.body) # => returns a raw hash matching the JSON returned
end
end
require './search'
require 'json' # assumes json gem installed
category = 'web'
term = 'cars'
client_ip = '127.0.0.1' # => request.env['REMOTE_ADDR'] from rails/rack
user_agent = 'Mozilla' # => request.env['HTTP_USER_AGENT'] from rails/rack
s = Search.new('93c46c6a-c11e-4532-88e0-90fb540b0e3b')
results = s.search(category, term, client_ip, user_agent)
results["ResultGroups"][0]["Result"].each do |r|
puts "Title:\t\t#{r['Title']}"
puts "Link:\t\t#{r['SiteLink']}"
puts "Description:\t#{r['Description']}"
puts "Display url:\t#{r['DisplayUrl']}"
# Collate found on...
sources = r["ContentSources"].select { |source| source["CanDisplay"] }
if sources.length > 0
label = sources.length == 1 ? "Found exclusively on: " : "Found on: "
puts "#{label}#{sources.map {|source| source["FriendlyName"]}.join(', ')}"
end
puts "=" * 50
end
# ---outputs something like: ---
=begin
Title: <strong>Cars</strong> 2 | Disney
Link: http://clickhandler.ischyrus.com/ClickHandler.ashx?ap=1&app=1&c=lobby&coi=239138&cop=main-title&ep=0&euip=127.0.0.1&npp=1&p=0&pp=0&pvaid=1d364a9dcb3c4b5f826c067c43be87cf&ru=http%3a%2f%2fdisney.go.com%2fcars%2f&s=lobby&hash=87D7D834D3718C0B21BE2BBFB39D76E2
Description: The latest <strong>Cars</strong> 2 movie clips, character biographies, games, photos,…
Display url: disney.go.com/<strong>cars</strong>/
Found on: Google, Yahoo! Search
==================================================
Title: New &amp; Used <strong>Cars</strong> for Sale, Auto Dealers, <strong>Car</strong> Reviews and <strong>Car</strong> ...
Link: http://clickhandler.ischyrus.com/ClickHandler.ashx?ap=2&app=1&c=lobby&coi=239138&cop=main-title&ep=1&euip=127.0.0.1&npp=2&p=0&pp=0&pvaid=1d364a9dcb3c4b5f826c067c43be87cf&ru=http%3a%2f%2fwww.cars.com%2f&s=lobby&hash=10C1C735FACC86907D22BFB3AA53B563
Description: Search 2.6 million new &amp; used <strong>car</strong> listings, price a new <strong>car</strong>, get a…
Display url: www.<strong>cars</strong>.com/
Found on: Google, Yahoo! Search, Bing
==================================================
Title: New <strong>Cars</strong>, Used <strong>Cars</strong> - Find <strong>Cars</strong> at AutoTrader.com
Link: http://clickhandler.ischyrus.com/ClickHandler.ashx?ap=3&app=1&c=lobby&coi=239138&cop=main-title&ep=2&euip=127.0.0.1&npp=3&p=0&pp=0&pvaid=1d364a9dcb3c4b5f826c067c43be87cf&ru=http%3a%2f%2fwww.autotrader.com%2f&s=lobby&hash=95365CE5C6BAA4CF07235463F3C03FF5
Description: Find used <strong>cars</strong> and new <strong>cars</strong> for sale at AutoTrader.com. With millions of…
Display url: www.autotrader.com/
Found on: Google, Yahoo! Search, Bing
...
=end
# This version has a friendlier looking syntax, thanks to the dynahash gem!
require './search'
require 'json' # assumes json gem installed
require 'dynahash' # hashes look like objects
category = 'web'
term = 'cars'
client_ip = '127.0.0.1' # => request.env['REMOTE_ADDR'] from rails/rack
user_agent = 'Mozilla' # => request.env['HTTP_USER_AGENT'] from rails/rack
s = Search.new('93c46c6a-c11e-4532-88e0-90fb540b0e3b')
results = s.search(category, term, client_ip, user_agent)
results.result_groups.first.result.each do |r|
puts "Title:\t\t#{r.title}"
puts "Link:\t\t#{r.site_link}"
puts "Description:\t#{r.description}"
puts "Display url:\t#{r.display_url}"
# Collate found on...
sources = r.content_sources.select { |source| source.can_display? }
if sources.length > 0
label = sources.length == 1 ? "Found exclusively on: " : "Found on: "
puts "#{label}#{sources.map {|source| source.friendly_name}.join(', ')}"
end
puts "=" * 50
end
# ---outputs something like: ---
=begin
Title: <strong>Cars</strong> 2 | Disney
Link: http://clickhandler.ischyrus.com/ClickHandler.ashx?ap=1&app=1&c=lobby&coi=239138&cop=main-title&ep=0&euip=127.0.0.1&npp=1&p=0&pp=0&pvaid=1d364a9dcb3c4b5f826c067c43be87cf&ru=http%3a%2f%2fdisney.go.com%2fcars%2f&s=lobby&hash=87D7D834D3718C0B21BE2BBFB39D76E2
Description: The latest <strong>Cars</strong> 2 movie clips, character biographies, games, photos,…
Display url: disney.go.com/<strong>cars</strong>/
Found on: Google, Yahoo! Search
==================================================
Title: New &amp; Used <strong>Cars</strong> for Sale, Auto Dealers, <strong>Car</strong> Reviews and <strong>Car</strong> ...
Link: http://clickhandler.ischyrus.com/ClickHandler.ashx?ap=2&app=1&c=lobby&coi=239138&cop=main-title&ep=1&euip=127.0.0.1&npp=2&p=0&pp=0&pvaid=1d364a9dcb3c4b5f826c067c43be87cf&ru=http%3a%2f%2fwww.cars.com%2f&s=lobby&hash=10C1C735FACC86907D22BFB3AA53B563
Description: Search 2.6 million new &amp; used <strong>car</strong> listings, price a new <strong>car</strong>, get a…
Display url: www.<strong>cars</strong>.com/
Found on: Google, Yahoo! Search, Bing
==================================================
Title: New <strong>Cars</strong>, Used <strong>Cars</strong> - Find <strong>Cars</strong> at AutoTrader.com
Link: http://clickhandler.ischyrus.com/ClickHandler.ashx?ap=3&app=1&c=lobby&coi=239138&cop=main-title&ep=2&euip=127.0.0.1&npp=3&p=0&pp=0&pvaid=1d364a9dcb3c4b5f826c067c43be87cf&ru=http%3a%2f%2fwww.autotrader.com%2f&s=lobby&hash=95365CE5C6BAA4CF07235463F3C03FF5
Description: Find used <strong>cars</strong> and new <strong>cars</strong> for sale at AutoTrader.com. With millions of…
Display url: www.autotrader.com/
Found on: Google, Yahoo! Search, Bing
...
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment