Class: Vhx::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.


5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/vhx/client.rb', line 5

def initialize(options = {})
  options            = Hash[options.map{ |k, v| [k.to_sym, v] }]
  @api_base_url      = options[:api_base] || API_BASE_URL
  @client_id         = options[:client_id]
  @client_secret     = options[:client_secret]
  @oauth_token       = options[:api_key] ? nil : OAuthToken.new(options[:oauth_token], refreshed = false)
  @api_key           = options[:api_key]
  @auto_refresh      = options[:auto_refresh]
  @ssl               = options[:ssl] || {}
  @headers           = {}

  configure_connection
end

Instance Attribute Details

#api_base_urlObject (readonly)

Returns the value of attribute api_base_url.


3
4
5
# File 'lib/vhx/client.rb', line 3

def api_base_url
  @api_base_url
end

#api_keyObject (readonly)

Returns the value of attribute api_key.


3
4
5
# File 'lib/vhx/client.rb', line 3

def api_key
  @api_key
end

#client_idObject (readonly)

Returns the value of attribute client_id.


3
4
5
# File 'lib/vhx/client.rb', line 3

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.


3
4
5
# File 'lib/vhx/client.rb', line 3

def client_secret
  @client_secret
end

#connectionObject (readonly)

Returns the value of attribute connection.


3
4
5
# File 'lib/vhx/client.rb', line 3

def connection
  @connection
end

#oauth_tokenObject (readonly)

Returns the value of attribute oauth_token.


3
4
5
# File 'lib/vhx/client.rb', line 3

def oauth_token
  @oauth_token
end

#sslObject (readonly)

Returns the value of attribute ssl.


3
4
5
# File 'lib/vhx/client.rb', line 3

def ssl
  @ssl
end

Instance Method Details

#access_tokenObject


47
48
49
50
51
52
53
# File 'lib/vhx/client.rb', line 47

def access_token
  unless oauth_token
    return nil
  end

  oauth_token.access_token
end

#configure_connectionObject


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vhx/client.rb', line 19

def configure_connection
  @connection = Faraday::Connection.new(url: api_base_url, headers: configured_headers, ssl: ssl) do |faraday|
    faraday.request  :url_encoded
    faraday.request  :json
    faraday.response :logger

    if @auto_refresh
      faraday.use Vhx::Middleware::OAuth2, :vhx_client => self
    end

    faraday.adapter Faraday.default_adapter

    faraday.use Vhx::Middleware::ErrorResponse
    faraday.response :json
  end
  @connection
end

#configured_headersObject


37
38
39
40
41
42
43
44
45
# File 'lib/vhx/client.rb', line 37

def configured_headers
  if access_token
    @headers[:Authorization] = "Bearer #{access_token}"
  elsif api_key
    @headers[:Authorization] = Faraday::Request::BasicAuthentication.header(api_key, '')
  end

  @headers
end

#credentialsObject


63
64
65
66
67
68
69
# File 'lib/vhx/client.rb', line 63

def credentials
  unless oauth_token
    return nil
  end

  oauth_token.to_h
end

#expired?Boolean

Returns:

  • (Boolean)

55
56
57
58
59
60
61
# File 'lib/vhx/client.rb', line 55

def expired?
  unless oauth_token
    return false
  end

  oauth_token.expires && oauth_token.expires_at < Time.now.to_i
end

#refresh_access_token!Object


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vhx/client.rb', line 71

def refresh_access_token!
  conn = @connection.dup
  conn.headers.delete(:Authorization)
  response = conn.post do |req|
    req.url '/oauth/token'
    req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    req.body = {
      grant_type:    'refresh_token',
      refresh_token: oauth_token.refresh_token,
      client_id:     client_id,
      client_secret: client_secret
    }
  end
  @oauth_token = OAuthToken.new(response.body, refreshed = true)

  configure_connection
end