Module: Appilf::APIActions

Included in:
Client, Listing, ResourcePage
Defined in:
lib/appilf/api_actions.rb

Constant Summary collapse

BASE_API_URL =
'https://api.flippa.com/v3'

Instance Method Summary collapse

Instance Method Details

#api_delete(path, payload, headers = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/appilf/api_actions.rb', line 14

def api_delete(path, payload, headers = {})
  RestClient::Request.execute(method: :delete, url: path,
                              payload: payload, headers: headers)
  :success
rescue RestClient::RequestFailed => e
  handle_from_response(e)
end

#api_get(path, headers = {}) ⇒ Object



22
23
24
25
26
27
# File 'lib/appilf/api_actions.rb', line 22

def api_get(path, headers = {})
  response = RestClient::Request.execute(method: :get, url: path, headers: headers)
  JSON.parse(response)
rescue RestClient::RequestFailed => e
  handle_from_response(e)
end

#api_post(path, payload, headers = {}) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/appilf/api_actions.rb', line 6

def api_post(path, payload, headers = {})
  response = RestClient::Request.execute(method: :post, url: path,
                                         payload: payload, headers: headers)
  JSON.parse(response)
rescue RestClient::RequestFailed => e
  handle_from_response(e)
end

#handle_from_response(response_error) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/appilf/api_actions.rb', line 29

def handle_from_response(response_error)
  message = JSON.parse(response_error.response)['error']
  case response_error.http_code
    when 400
      raise BadRequest.new(message)
    when 401
      raise Unauthorized.new(message)
    when 403
      raise Forbidden.new(message)
    when 404
      raise NotFound.new(message)
    when 500
      raise InternalServerError.new(message)
    else
      raise response_error
  end
end