Class: GoogleAPI

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

Instance Method Summary collapse

Constructor Details

#initialize(api_key, service_name) ⇒ GoogleAPI

Returns a new instance of GoogleAPI.



7
8
9
10
11
# File 'lib/civic_info.rb', line 7

def initialize(api_key, service_name)
  @api_key = ENV['GOOGLE_API_KEY']
  @service_url = "https://www.googleapis.com#{service_name}"
  @conn = Faraday.new(:url => @service_url)
end

Instance Method Details

#get(call_string) ⇒ Object



13
14
15
# File 'lib/civic_info.rb', line 13

def get(call_string)
  JSON.parse(@conn.get("#{call_string}?key=#{@api_key}").body).with_indifferent_access
end

#post(call_string, body) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/civic_info.rb', line 17

def post(call_string, body)
  response = @conn.post do |conn|
    conn.url "#{call_string}?key=#{@api_key}"
    conn.headers['Content-Type'] = 'application/json' 
    conn.body = body.to_json
  end
  JSON.parse(response.body).with_indifferent_access
end