Skip to content
Browse files

Adding the Google Search and Google Weather API wrappers.

  • Loading branch information...
1 parent 7bdc7bd commit 4d4a160daa4aad3629def20597e57c1c2106480f @caius committed
Showing with 70 additions and 0 deletions.
  1. +33 −0 google/google.rb
  2. +37 −0 weather/weather.rb
View
33 google/google.rb
@@ -0,0 +1,33 @@
+require "rubygems"
+require "httparty"
+
+class Search
+ include HTTParty
+
+ base_uri "http://ajax.googleapis.com/ajax/services/search/web"
+ default_params :v => 1.0
+
+ attr_accessor :title, :url, :content
+
+ def initialize &block
+ yield(self) if block_given?
+ end
+
+ def self.find terms
+ results = []
+ get("", :query => {:q => terms})["responseData"]["results"].each do | result |
+ results << Search.new do | s |
+ s.title = result["titleNoFormatting"]
+ s.url = result["url"]
+ s.content = result["content"]
+ end
+ end
+ results
+ end
+
+end
+
+if $0 == __FILE__
+ p Search.find("caius durling").map {|x| x.title }
+end
+
View
37 weather/weather.rb
@@ -0,0 +1,37 @@
+require "rubygems"
+require "httparty"
+
+class Weather
+ include HTTParty
+
+ attr_accessor :humidity, :condition, :temp
+
+ def initialize
+ yield(self) if block_given?
+ @temp ||= {}
+ end
+
+ base_uri "http://www.google.com/ig/api"
+
+ def self.find location
+ r = get("", :query => {:weather => location})["xml_api_reply"]["weather"]["current_conditions"]
+
+ Weather.new do | w |
+ w.humidity = r["humidity"]["data"]
+ w.condition = r["condition"]["data"]
+ w.temp = {
+ :c => r["temp_c"]["data"],
+ :f => r["temp_c"]["data"]
+ }
+ end
+ end
+
+ def to_s
+ "Currently #{condition} at #{@temp[:c]}°C (#{temp[:f]}°f) #{humidity} humidity"
+ end
+
+end
+
+if $0 == __FILE__
+ puts Weather.find("Leeds, UK").to_s
+end

0 comments on commit 4d4a160

Please sign in to comment.
Something went wrong with that request. Please try again.