Class: VoteSmart::Common

Inherits:
Object
  • Object
show all
Defined in:
lib/vote_smart/common.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Common

Returns a new instance of Common.



111
112
113
# File 'lib/vote_smart/common.rb', line 111

def initialize attributes = {}
  update_attributes attributes
end

Class Attribute Details

.attribute_mapObject (readonly)

Returns the value of attribute attribute_map.



38
39
40
# File 'lib/vote_smart/common.rb', line 38

def attribute_map
  @attribute_map
end

Class Method Details

.construct_url(api_method, params = {}) ⇒ Object

Constructs a VoteSmart API-friendly URL



58
59
60
# File 'lib/vote_smart/common.rb', line 58

def construct_url(api_method, params = {})
  "#{API_URL}#{api_method}?key=#{VoteSmart.api_key}&o=#{API_FORMAT}#{hash2get(params)}"
end

.get_json_data(url) ⇒ Object

Use the Net::HTTP and JSON libraries to make the API call

Usage:

District.get_json_data("http://someurl.com")    # returns Hash of data or nil


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/vote_smart/common.rb', line 91

def get_json_data(url)
  response = session.get(url)
  if response.status != 200
    raise RequestFailed.new("Request was not OK: #{response.class}: #{url} #{response.body}")
  end
  JSON.parse(response.body)
rescue
  @json_retries ||= 0
  if @json_retries < 5
    puts "Retrying #{url}"
    sleep(2 ** @json_retries)
    @json_retries += 1
    retry
  end
  raise
ensure
  @json_retries = 0
end

.hash2get(h) ⇒ Object

Converts a hash to a GET string



63
64
65
66
67
# File 'lib/vote_smart/common.rb', line 63

def hash2get(h)
  h.map do |(key, value)|
    "&#{key.to_s}=#{CGI::escape(value.to_s)}" if value
  end.compact.join
end

.parallelize!Object



40
41
42
43
# File 'lib/vote_smart/common.rb', line 40

def parallelize!
  gem 'typhoeus'
  extend ParallelQueries
end

.request(api_method, params = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/vote_smart/common.rb', line 69

def request(api_method, params = {})
  url = construct_url api_method, params

  json = get_json_data(url)

  if json['error'] and json['error']['errorMessage'] == 'Authorization failed'
    raise RequestFailed.new(json['error']['errorMessage'])
  end

  json
end

.response_child(response, *children) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/vote_smart/common.rb', line 49

def response_child response, *children
  for child in children
    response = response[child] if response
  end

  response || {}
end

.sessionObject



81
82
83
84
85
# File 'lib/vote_smart/common.rb', line 81

def session
  @session ||= Patron::Session.new.tap do |session|
    session.timeout = 15
  end
end

.set_attribute_map(map) ⇒ Object



45
46
47
# File 'lib/vote_smart/common.rb', line 45

def set_attribute_map map
  @attribute_map = map
end

Instance Method Details

#update_attributes(attributes) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/vote_smart/common.rb', line 115

def update_attributes attributes
  map = self.class.attribute_map
  raise "map not set over-ride needed" unless map
  
  attributes.each do |key, value|
    if key.kind_of?(Symbol)
      send("#{key}=", value)
    else
      map_to = map[key]
      send("#{map_to}=", value) if map_to
    end
  end
end