Class: Vhx::Client
- Inherits:
-
Object
- Object
- Vhx::Client
- Defined in:
- lib/vhx/client.rb
Instance Attribute Summary collapse
-
#api_base_url ⇒ Object
readonly
Returns the value of attribute api_base_url.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#oauth_token ⇒ Object
readonly
Returns the value of attribute oauth_token.
-
#ssl ⇒ Object
readonly
Returns the value of attribute ssl.
Instance Method Summary collapse
- #access_token ⇒ Object
- #configure_connection ⇒ Object
- #configured_headers ⇒ Object
- #credentials ⇒ Object
- #expired? ⇒ Boolean
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #refresh_access_token! ⇒ Object
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( = {}) = Hash[.map{ |k, v| [k.to_sym, v] }] @api_base_url = [:api_base] || API_BASE_URL @client_id = [:client_id] @client_secret = [:client_secret] @oauth_token = [:api_key] ? nil : OAuthToken.new([:oauth_token], refreshed = false) @api_key = [:api_key] @auto_refresh = [:auto_refresh] @ssl = [:ssl] || {} @headers = {} configure_connection end |
Instance Attribute Details
#api_base_url ⇒ Object (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_key ⇒ Object (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_id ⇒ Object (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_secret ⇒ Object (readonly)
Returns the value of attribute client_secret.
3 4 5 |
# File 'lib/vhx/client.rb', line 3 def client_secret @client_secret end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
3 4 5 |
# File 'lib/vhx/client.rb', line 3 def connection @connection end |
#oauth_token ⇒ Object (readonly)
Returns the value of attribute oauth_token.
3 4 5 |
# File 'lib/vhx/client.rb', line 3 def oauth_token @oauth_token end |
#ssl ⇒ Object (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_token ⇒ Object
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_connection ⇒ Object
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_headers ⇒ Object
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 |
#credentials ⇒ Object
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
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 |