class Vhx::Client
Attributes
api_base_url[R]
api_key[R]
client_id[R]
client_secret[R]
connection[R]
oauth_token[R]
ssl[R]
Public Class Methods
new(options = {})
click to toggle source
# 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
Public Instance Methods
access_token()
click to toggle source
# File lib/vhx/client.rb, line 46 def access_token unless oauth_token return nil end oauth_token.access_token end
configure_connection()
click to toggle source
# 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.use Vhx::Middleware::ErrorResponse faraday.response :json faraday.adapter Faraday.default_adapter end @connection end
configured_headers()
click to toggle source
# File lib/vhx/client.rb, line 36 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()
click to toggle source
# File lib/vhx/client.rb, line 62 def credentials unless oauth_token return nil end oauth_token.to_h end
expired?()
click to toggle source
# File lib/vhx/client.rb, line 54 def expired? unless oauth_token return false end oauth_token.expires && oauth_token.expires_at < Time.now.to_i end
refresh_access_token!()
click to toggle source
# File lib/vhx/client.rb, line 70 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