Class: GovKit::OpenCongress::OpenCongressObject

Inherits:
Object
  • Object
show all
Defined in:
lib/gov_kit/open_congress.rb

Overview

Parent class for classes that wrap OpenCongress data.

Unlike the wrapper classes for data from FollowTheMoney, OpenStates, TransparencyData, and VoteSmart, OpenCongressObject does not inherit from Resource

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, params) ⇒ OpenCongressObject

Returns a new instance of OpenCongressObject.



24
25
26
27
28
29
# File 'lib/gov_kit/open_congress.rb', line 24

def initialize(obj, params)
  params.each do |key, value|
    key = key.to_sym if RUBY_VERSION[0,3] == "1.9"
    instance_variable_set("@#{key}", value) if obj.instance_methods.include? key
  end      
end

Class Method Details

.construct_url(api_method, params) ⇒ Object

Create a query url, by adding the method path and query parameters to the base url of http://www.opencongress.org/api.



33
34
35
36
37
38
# File 'lib/gov_kit/open_congress.rb', line 33

def self.construct_url(api_method, params)
  url = nil
  getkey = GovKit::configuration.opencongress_apikey.nil? ? "" : "&key=#{GovKit::configuration.opencongress_apikey}"
  url = "http://#{GovKit::configuration.opencongress_base_url}api/#{api_method}?format=json#{hash2get(params)}#{getkey}"
  return url
end

.hash2get(h) ⇒ String

Convert a hash to a string of query parameters.

Parameters:

  • h (Hash)

    a hash.

Returns:

  • (String)

    a string of query parameters.



44
45
46
47
48
49
50
51
52
# File 'lib/gov_kit/open_congress.rb', line 44

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

.make_call(this_url) ⇒ Object

Get the data from OpenCongress data. Called by subclasses.

Parses the data using JSON.parse, which returns an array of hashes.

Returns:

  • the returned data, as an array of hashes.



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gov_kit/open_congress.rb', line 113

def self.make_call(this_url)
  result = nil
  begin
    result = JSON.parse(open(this_url).read)
  rescue => e
    puts e
  end

  return result

end

.parse_supporting_results(result) ⇒ Object

Iterates through the array returned by make_call, converting each hash to an OpenCongressObject subclass.

Parameters:

  • results (Hash)

    the array returned by make_call.

Returns:

  • a hash of arrays of OpenCongressObject objects, with these keys:

    • :also_supporting_bills

    • :also_opposing_bills

    • :also_disapproved_senators

    • :also_disapproved_representatives

    • :also_approved_senators

    • :also_approved_representatives



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gov_kit/open_congress.rb', line 65

def self.parse_supporting_results(result)
  working = result["opencongress_users_tracking"]

  also_supporting_bills = []
  working["also_supporting_bills"]["bill"].each do |bill|
    also_supporting_bills << Bill.new(bill)
  end

  also_opposing_bills = []
  working["also_opposing_bills"]["bill"].each do |bill|
    also_opposing_bills << Bill.new(bill)
  end

  also_disapproved_senators = []
  working["also_disapproved_senators"]["person"].each do |person|
    also_disapproved_senators << Person.new(person)
  end

  also_disapproved_representatives = []
  working["also_disapproved_representatives"]["person"].each do |person|
    also_disapproved_representatives << Person.new(person)
  end

  also_approved_senators = []
  working["also_approved_senators"]["person"].each do |person|
    also_approved_senators << Person.new(person)
  end

  also_approved_representatives = []
  working["also_approved_representatives"]["person"].each do |person|
    also_approved_representatives << Person.new(person)
  end

  return {:also_supporting_bills => also_supporting_bills,
          :also_opposing_bills => also_opposing_bills,
          :also_disapproved_senators => also_disapproved_senators,
          :also_disapproved_representatives => also_disapproved_representatives,
          :also_approved_senators => also_approved_senators,
          :also_approved_representatives => also_approved_representatives}

end