Class: Outbrain::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/outbrain/connection.rb

Constant Summary collapse

BASE =
'https://api.outbrain.com'
DEFAULT_API_VERSION =
'v0.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_versionObject

Returns the value of attribute api_version.



8
9
10
# File 'lib/outbrain/connection.rb', line 8

def api_version
  @api_version
end

#base_urlObject

Returns the value of attribute base_url.



8
9
10
# File 'lib/outbrain/connection.rb', line 8

def base_url
  @base_url
end

#connectionObject

Returns the value of attribute connection.



8
9
10
# File 'lib/outbrain/connection.rb', line 8

def connection
  @connection
end

#loggingObject

Returns the value of attribute logging.



8
9
10
# File 'lib/outbrain/connection.rb', line 8

def logging
  @logging
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/outbrain/connection.rb', line 8

def token
  @token
end

#user_nameObject

Returns the value of attribute user_name.



8
9
10
# File 'lib/outbrain/connection.rb', line 8

def user_name
  @user_name
end

#user_passwordObject

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

.configure(params = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/outbrain/connection.rb', line 21

def self.configure(params={})
  connection = new(params)
  Config.api = connection.api
  Config.api_version = connection.api_version
  connection
end

Instance Method Details

#apiObject



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_apiObject



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