Class: Amorail::Client

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

Overview

Amorail http client

Constant Summary collapse

SUCCESS_STATUS_CODES =
[200, 204].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_endpoint: Amorail.config.api_endpoint, client_id: Amorail.config.client_id, client_secret: Amorail.config.client_secret, code: Amorail.config.code, redirect_uri: Amorail.config.redirect_uri) ⇒ Client

Returns a new instance of Client.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/amorail/client.rb', line 23

def initialize(api_endpoint: Amorail.config.api_endpoint,
               client_id: Amorail.config.client_id,
               client_secret: Amorail.config.client_secret,
               code: Amorail.config.code,
               redirect_uri: Amorail.config.redirect_uri)
  @store = Amorail.token_store
  @api_endpoint = api_endpoint
  @client_id = client_id
  @client_secret = client_secret
  @code = code
  @redirect_uri = redirect_uri
  @access = AccessToken.find(@client_secret, store)
  @access_token = @access.token
  @refresh_token = @access.refresh_token

  @connect = Faraday.new(url: api_endpoint) do |faraday|
    faraday.response :json, content_type: /\bjson$/
    faraday.use :instrumentation
    faraday.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#accessObject (readonly)

Returns the value of attribute access.



14
15
16
# File 'lib/amorail/client.rb', line 14

def access
  @access
end

#access_tokenObject (readonly)

Returns the value of attribute access_token.



14
15
16
# File 'lib/amorail/client.rb', line 14

def access_token
  @access_token
end

#api_endpointObject (readonly)

Returns the value of attribute api_endpoint.



14
15
16
# File 'lib/amorail/client.rb', line 14

def api_endpoint
  @api_endpoint
end

#client_idObject (readonly)

Returns the value of attribute client_id.



14
15
16
# File 'lib/amorail/client.rb', line 14

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



14
15
16
# File 'lib/amorail/client.rb', line 14

def client_secret
  @client_secret
end

#codeObject (readonly)

Returns the value of attribute code.



14
15
16
# File 'lib/amorail/client.rb', line 14

def code
  @code
end

#redirect_uriObject (readonly)

Returns the value of attribute redirect_uri.



14
15
16
# File 'lib/amorail/client.rb', line 14

def redirect_uri
  @redirect_uri
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



14
15
16
# File 'lib/amorail/client.rb', line 14

def refresh_token
  @refresh_token
end

#storeObject

Returns the value of attribute store.



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

def store
  @store
end

Instance Method Details

#auth_paramsObject



65
66
67
68
69
70
71
72
73
# File 'lib/amorail/client.rb', line 65

def auth_params
  {
    client_id: client_id,
    client_secret: client_secret,
    grant_type: 'authorization_code',
    code: @code,
    redirect_uri: redirect_uri
  }
end

#authorizeObject



53
54
55
56
57
# File 'lib/amorail/client.rb', line 53

def authorize
  response = post(Amorail.config.auth_url, auth_params)
  create_access_token(response)
  response
end

#connectObject



49
50
51
# File 'lib/amorail/client.rb', line 49

def connect
  @connect || self.class.new
end

#get(url, params = {}) ⇒ Object



91
92
93
94
95
96
# File 'lib/amorail/client.rb', line 91

def get(url, params = {})
  response = connect.get(url, params) do |request|
    request.headers['Authorization'] = "Bearer #{access_token}" if access_token.present?
  end
  handle_response(response)
end

#post(url, params = {}) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/amorail/client.rb', line 98

def post(url, params = {})
  response = connect.post(url) do |request|
    request.headers['Authorization'] = "Bearer #{access_token}" if access_token.present?
    request.headers['Content-Type'] = 'application/json'
    request.body = params.to_json
  end
  handle_response(response)
end

#propertiesObject



45
46
47
# File 'lib/amorail/client.rb', line 45

def properties
  @properties ||= Property.new(self)
end

#refresh_paramsObject



75
76
77
78
79
80
81
82
83
# File 'lib/amorail/client.rb', line 75

def refresh_params
  {
    client_id: client_id,
    client_secret: client_secret,
    grant_type: 'refresh_token',
    refresh_token: refresh_token,
    redirect_uri: redirect_uri
  }
end

#refresh_token!Object



59
60
61
62
63
# File 'lib/amorail/client.rb', line 59

def refresh_token!
  response = post(Amorail.config.auth_url, refresh_params)
  update_access_token(response)
  response
end

#safe_request(method, url, params = {}) ⇒ Object



85
86
87
88
89
# File 'lib/amorail/client.rb', line 85

def safe_request(method, url, params = {})
  authorize if access_token.blank?
  refresh_token! if access.expired?
  public_send(method, url, params)
end