Class: Outbrain::Connection
- Inherits:
-
Object
- Object
- Outbrain::Connection
- Defined in:
- lib/outbrain/connection.rb
Constant Summary collapse
- BASE =
'https://api.outbrain.com'
- DEFAULT_API_VERSION =
'v0.1'
Instance Attribute Summary collapse
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#logging ⇒ Object
Returns the value of attribute logging.
-
#token ⇒ Object
Returns the value of attribute token.
-
#user_name ⇒ Object
Returns the value of attribute user_name.
-
#user_password ⇒ Object
Returns the value of attribute user_password.
Class Method Summary collapse
Instance Method Summary collapse
- #api ⇒ Object
-
#get_token! ⇒ Object
authenticates using basic auth to get token.
-
#initialize(args = {}) ⇒ Connection
constructor
A new instance of Connection.
- #refresh_api ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Connection
Returns a new instance of Connection.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/outbrain/connection.rb', line 10 def initialize(args={}) @token = args[:token] || args['token'] @user_name = args[:user_name] || args['user_name'] @user_password = args[:user_password] || args['user_password'] @api_version = args[:api_version] || args['api_version'] || DEFAULT_API_VERSION @logging = (args.key?(:logging) || args.key?('logging')) ? (args[:logging] || args['logging']) : true # (default right now) @base_url = args[:base_url] || "#{BASE}/amplify/#{api_version}/" get_token! unless @token # should raise if not authenticated properly end |
Instance Attribute Details
#api_version ⇒ Object
Returns the value of attribute api_version.
8 9 10 |
# File 'lib/outbrain/connection.rb', line 8 def api_version @api_version end |
#base_url ⇒ Object
Returns the value of attribute base_url.
8 9 10 |
# File 'lib/outbrain/connection.rb', line 8 def base_url @base_url end |
#connection ⇒ Object
Returns the value of attribute connection.
8 9 10 |
# File 'lib/outbrain/connection.rb', line 8 def connection @connection end |
#logging ⇒ Object
Returns the value of attribute logging.
8 9 10 |
# File 'lib/outbrain/connection.rb', line 8 def logging @logging end |
#token ⇒ Object
Returns the value of attribute token.
8 9 10 |
# File 'lib/outbrain/connection.rb', line 8 def token @token end |
#user_name ⇒ Object
Returns the value of attribute user_name.
8 9 10 |
# File 'lib/outbrain/connection.rb', line 8 def user_name @user_name end |
#user_password ⇒ Object
Returns the value of attribute user_password.
8 9 10 |
# File 'lib/outbrain/connection.rb', line 8 def user_password @user_password end |
Class Method Details
Instance Method Details
#api ⇒ Object
42 43 44 |
# File 'lib/outbrain/connection.rb', line 42 def api @api ||= refresh_api end |
#get_token! ⇒ Object
authenticates using basic auth to get token.
> docs.amplifyv01.apiary.io/#reference/authentications
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/outbrain/connection.rb', line 30 def get_token! @temp_api = Faraday.new(:url => base_url) do |faraday| faraday.response :logger if logging faraday.adapter Faraday.default_adapter end @temp_api.basic_auth user_name, user_password response = @temp_api.get("/login") @token = JSON.parse(response.body)[Outbrain::HEADER_AUTH_KEY] # need to raise error here if token does not exist end |
#refresh_api ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/outbrain/connection.rb', line 46 def refresh_api @api = Faraday.new(:url => base_url) do |faraday| faraday.response :logger if logging faraday.adapter Faraday.default_adapter faraday.headers['Content-Type'] = 'application/json' faraday.headers[Outbrain::HEADER_AUTH_KEY] = token end end |