Class: Professionali::ApiCore

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ ApiCore

Returns a new instance of ApiCore.



5
6
7
# File 'lib/professionali/api_core.rb', line 5

def initialize token
  @token = token
end

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



3
4
5
# File 'lib/professionali/api_core.rb', line 3

def token
  @token
end

Instance Method Details

#api_call(path, method = "get", params = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/professionali/api_core.rb', line 9

def api_call path, method = "get", params = {}
  params = params.merge(access_token: @token)
  begin
    case method
      when "get"
        response = RestClient.get api_url(path), params: params
      when "put"
        response = RestClient.put api_url(path), params: params, accept: :json
      when "post"
        response = RestClient.post api_url(path), params: params, accept: :json
      else
        response = RestClient.send method, api_url(path), params: params
    end

  rescue => e
    raise_errors e.response
  end
  Professionali::Mash.from_json response.body
end

#getObject



51
52
53
# File 'lib/professionali/api_core.rb', line 51

def get
  :get
end

#postObject



47
48
49
# File 'lib/professionali/api_core.rb', line 47

def post
  :post
end

#raise_errors(response) ⇒ Object



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

def raise_errors response
  case response.code.to_i
  when 400
    data = Mash.from_json(response.body)
    raise Professionali::Errors::GeneralError.new(data), "(#{data.status}): #{data.message}"
  when 403
    raise Professionali::Errors::AccessDeniedError, "(#{response.code}): #{response.message}"
  when 405, 401
    raise Professionali::Errors::UnauthorizedError, "(#{response.code}): #{response.message}"
  when 404
    raise Professionali::Errors::NotFoundError, "(#{response.code}): #{response.message}"
  when 500
    raise Professionali::Errors::InformWhitLiError, "Professionali had an internal error. (#{response.code}): #{response.message}"
  when 502..503
    raise Professionali::Errors::UnavailableError, "(#{response.code}): #{response.message}"
  end
end