Class: Sunlight::Base
- Inherits:
-
Object
- Object
- Sunlight::Base
- Defined in:
- lib/sunlight/base.rb
Overview
Houses general methods to work with the Sunlight and Google Maps APIs
Direct Known Subclasses
Constant Summary collapse
- API_URL =
"http://services.sunlightlabs.com/api/"- API_FORMAT =
"json"- @@api_key =
''
Class Method Summary collapse
- .api_key ⇒ Object
- .api_key=(key) ⇒ Object
-
.construct_url(api_method, params) ⇒ Object
Constructs a Sunlight API-friendly URL.
-
.get_json_data(url) ⇒ Object
Use the Net::HTTP and JSON libraries to make the API call.
-
.hash2get(h) ⇒ Object
Converts a hash to a GET string.
Class Method Details
.api_key ⇒ Object
10 11 12 |
# File 'lib/sunlight/base.rb', line 10 def self.api_key @@api_key end |
.api_key=(key) ⇒ Object
14 15 16 |
# File 'lib/sunlight/base.rb', line 14 def self.api_key=(key) @@api_key = key end |
.construct_url(api_method, params) ⇒ Object
Constructs a Sunlight API-friendly URL
19 20 21 22 23 24 25 |
# File 'lib/sunlight/base.rb', line 19 def self.construct_url(api_method, params) if api_key == nil or api_key == '' raise "Failed to provide Sunlight API Key" else "#{API_URL}#{api_method}.#{API_FORMAT}?apikey=#{api_key}#{hash2get(params)}" end end |
.get_json_data(url) ⇒ Object
Use the Net::HTTP and JSON libraries to make the API call
Usage:
Legislator::District.get_json_data("http://someurl.com") # returns Hash of data or nil
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/sunlight/base.rb', line 44 def self.get_json_data(url) response = Net::HTTP.get_response(URI.parse(url)) if response.class == Net::HTTPOK result = JSON.parse(response.body) else nil end end |
.hash2get(h) ⇒ Object
Converts a hash to a GET string
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sunlight/base.rb', line 28 def self.hash2get(h) get_string = "" h.each_pair do |key, value| get_string += "&#{key.to_s}=#{CGI::escape(value.to_s)}" end get_string end |