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.



14
15
16
# File 'lib/vote_smart/common.rb', line 14

def initialize attributes = {}
  update_attributes attributes
end

Class Attribute Details

.attribute_mapObject (readonly)

Returns the value of attribute attribute_map.



11
12
13
# File 'lib/vote_smart/common.rb', line 11

def attribute_map
  @attribute_map
end

Class Method Details

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

Constructs a VoteSmart API-friendly URL



62
63
64
# File 'lib/vote_smart/common.rb', line 62

def self.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


84
85
86
87
88
89
90
91
92
# File 'lib/vote_smart/common.rb', line 84

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
    raise RequestFailed.new("Request was not OK: #{response.class}: #{url} #{response.body}")
  end
  
end

.hash2get(h) ⇒ Object

Converts a hash to a GET string



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

def self.hash2get(h)
  
  get_string = ""
  
  h.each_pair do |key, value|
    get_string += "&#{key.to_s}=#{CGI::escape(value.to_s)}" unless value.nil?
  end
  
  get_string
  
end

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



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

def self.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



36
37
38
39
40
41
42
# File 'lib/vote_smart/common.rb', line 36

def self.response_child response, *children
  for child in children
    response = response[child] if response
  end
  
  response || {}
end

.response_child_array(response, *children) ⇒ Object



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

def self.response_child_array response, *children
  child = response_child response, *children
  child.kind_of?(Array) ? child : [child]
end

.set_attribute_map(map) ⇒ Object



18
19
20
# File 'lib/vote_smart/common.rb', line 18

def self.set_attribute_map map
  @attribute_map = map
end

Instance Method Details

#update_attributes(attributes) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vote_smart/common.rb', line 22

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