Class: RescueGroups::RemoteClient

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

Constant Summary collapse

BASE_URL =
'https://api.rescuegroups.org/http/json'.freeze

Instance Method Summary collapse

Instance Method Details

#connectionObject



30
31
32
# File 'lib/remote_client.rb', line 30

def connection
  @connection ||= Faraday.new
end

#headersObject



9
10
11
# File 'lib/remote_client.rb', line 9

def headers
  { 'Content-Type' => 'application/json' }.merge(connection.headers)
end

#post_and_respond(post_body) ⇒ Object

method: post_and_respond purpose: make a POST request to the RescueGroups API and respond param: post_body - <Hash> - attributes to be included in the post body return: response <Response> object with details of HTTP response



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/remote_client.rb', line 17

def post_and_respond(post_body)
  if RescueGroups.config.apikey.nil? || RescueGroups.config.apikey.length == 0
    raise 'No RescueGroups API Key set'
  end

  response = connection.post(BASE_URL) do |request|
    request.headers = headers
    request.body = JSON(post_body.merge(apikey: RescueGroups.config.apikey))
  end

  Response.new(response)
end