Class: USA::Base
- Inherits:
-
Object
- Object
- USA::Base
- Defined in:
- lib/usaspending/base.rb
Direct Known Subclasses
Constant Summary collapse
- API_URL =
"http://www.usaspending.gov"
Instance Method Summary collapse
-
#construct_url(database, params) ⇒ Object
construct the url.
-
#get_data(url) ⇒ Object
Use the Open URI and NokoGiri libraries to make the API call.
-
#hash2get(h) ⇒ Object
Converts a hash to a GET string.
-
#set_instance_methods(params) ⇒ Object
Sets attr_accessor methods for sub classes to keys in xml hashes.
Instance Method Details
#construct_url(database, params) ⇒ Object
construct the url. Requires a database name, currently FAADS or FPDS
7 8 9 10 11 12 13 |
# File 'lib/usaspending/base.rb', line 7 def construct_url(database, params) if database == nil or database == '' raise "Failed to provide a Federal Database such as FAADS or FPDS" else "#{API_URL}/#{database.downcase}/#{database.downcase}.php?datype=X#{hash2get(params)}" end end |
#get_data(url) ⇒ Object
Use the Open URI and NokoGiri libraries to make the API call
Usage:
Class::Instance.get_data("http://someurl.com") # returns Hash of data or nil
30 31 32 33 34 35 36 37 |
# File 'lib/usaspending/base.rb', line 30 def get_data(url) response = Net::HTTP.get_response(URI.parse(url)) if response.class == Net::HTTPOK result = XmlSimple.xml_in(response.body) else nil end end |
#hash2get(h) ⇒ Object
Converts a hash to a GET string
16 17 18 19 20 21 22 23 24 |
# File 'lib/usaspending/base.rb', line 16 def hash2get(h) get_string = "" h.each_pair do |key, value| get_string += "&#{key.to_s}=#{CGI::escape(value.to_s)}" end get_string end |
#set_instance_methods(params) ⇒ Object
Sets attr_accessor methods for sub classes to keys in xml hashes
41 42 43 44 45 46 47 |
# File 'lib/usaspending/base.rb', line 41 def set_instance_methods(params) params.each do |key, value| instance_variable_set("@#{key.downcase}", value) if self.methods.include? key.downcase and !value.empty? end end |