outbrain-api 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a156c1b833c40ae218e3ed8c982111ff5b79fbec
4
- data.tar.gz: b4f0d95d8048b68ac9647ce3c88b47469fc992f8
3
+ metadata.gz: 611076af635dfef59515e4d7370424ae0924e924
4
+ data.tar.gz: 22e7cdfdce710aa25d8aaa97f1ccefdb353c84a2
5
5
  SHA512:
6
- metadata.gz: e3a0ac6568a62ebf1af6fb00f7cb91f1dfc73ad1c10787e15928634eae0392ae5c323246adc9fdc2e0da35c633d748cac73f7123eb4843c0bc7ab35e6643bc40
7
- data.tar.gz: 571521763074e1b9ffa294b027c96cc8b57472407b6147fdc67ada77238db181cc84fb19b51fd513582b70385d52333508ffafa489c6cd693d661cf5599eaf0b
6
+ metadata.gz: ab1b4431d4e0a92e15c31e1616891e1a466375317a8b3d8d6bdbe58b63255ed21f535e0d993fd9414412e437e296d9ffa25706e15a30ff3f87ca24f9b8a6d17f
7
+ data.tar.gz: 6b856617eeeb6c0fb438c18a0ab053f40d98743ab45ce5d3876d28618489df352f8946425606e1272209a12ef21dca586e094f822e4cf89860b98877058d5109
data/lib/outbrain/api.rb CHANGED
@@ -14,6 +14,8 @@ require "outbrain/api/budget"
14
14
  require "outbrain/api/campaign_report"
15
15
  require "outbrain/api/publisher_report"
16
16
  require "outbrain/api/campaign_by_day_report"
17
+ require "outbrain/api/promoted_link_performance_report"
18
+ require "outbrain/api/promoted_link"
17
19
 
18
20
  module Outbrain
19
21
  HEADER_AUTH_KEY = 'OB-TOKEN-V1'
@@ -4,15 +4,9 @@ module Outbrain
4
4
  module Api
5
5
  class CampaignByDayReport < Report
6
6
  PATH = 'performanceByDay'
7
-
8
- def self.path(campaign_id)
9
- "campaigns/#{campaign_id}/#{PATH}"
10
- end
11
-
12
- def self.where(options = {})
13
- super(options.merge({path: self.path(options.fetch(:campaign_id))}))
7
+ def self.path(options)
8
+ "campaigns/#{options.fetch(:campaign_id)}/#{PATH}/"
14
9
  end
15
10
  end
16
11
  end
17
12
  end
18
-
@@ -4,13 +4,8 @@ module Outbrain
4
4
  module Api
5
5
  class CampaignReport < Report
6
6
  PATH = 'performanceByCampaign'
7
-
8
- def self.path(marketer_id)
9
- "marketers/#{marketer_id}/#{PATH}"
10
- end
11
-
12
- def self.where(options = {})
13
- super(options.merge({path: self.path(options.fetch(:marketer_id))}))
7
+ def self.path(options)
8
+ "marketers/#{options.fetch(:marketer_id)}/#{PATH}/"
14
9
  end
15
10
  end
16
11
  end
@@ -0,0 +1,19 @@
1
+ require 'outbrain/api/report'
2
+
3
+ module Outbrain
4
+ module Api
5
+ class PromotedLink < Base
6
+ PATH = "promotedLinks"
7
+ RESOURCE_NAME = PATH
8
+
9
+ def self.find(id)
10
+ Request.find( PATH, id, { as: self })
11
+ end
12
+
13
+ def self.where(options)
14
+ path = "campaigns/#{options.fetch(:campaign_id)}/#{PATH}"
15
+ Request.where(path, options, as: self, resource_name: RESOURCE_NAME)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ require 'outbrain/api/report'
2
+
3
+ module Outbrain
4
+ module Api
5
+ class PromotedLinkPerformanceReport < Report
6
+ PATH = 'performanceByPromotedLink'
7
+
8
+ def self.path(options)
9
+ "campaigns/#{options.fetch(:campaign_id)}/#{PATH}/"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -4,15 +4,9 @@ module Outbrain
4
4
  module Api
5
5
  class PublisherReport < Report
6
6
  PATH = 'performanceByPublisher'
7
-
8
- def self.path(campaign_id)
9
- "campaigns/#{campaign_id}/#{PATH}"
10
- end
11
-
12
- def self.where(options = {})
13
- super(options.merge({path: self.path(options.fetch(:campaign_id))}))
7
+ def self.path(options)
8
+ "campaigns/#{options.fetch(:campaign_id)}/#{PATH}/"
14
9
  end
15
10
  end
16
11
  end
17
12
  end
18
-
@@ -1,20 +1,21 @@
1
1
  module Outbrain
2
2
  module Api
3
- class Relation
3
+ class Relation < OpenStruct
4
4
  include Enumerable
5
- attr_accessor :relations, :errors, :totalDataCount, :overAllMetrics, :aggregatedBy, :details, :overallMetrics
6
-
5
+ extend Forwardable
6
+ def_delegator :relations, :each, :each
7
7
  def initialize(options = {})
8
- @relations = []
9
- @errors = []
8
+ super
9
+ self.errors = []
10
+ relation = options.delete(options[:relation_name])
11
+ Hashie::Mash.new(options).each{ |k,v| self[k] = v }
12
+ setup_relations(relation)
10
13
  end
11
14
 
12
- def each &block
13
- @relations.each{|relation| block.call(relation) }
14
- end
15
+ private
15
16
 
16
- def any?
17
- relations.any?
17
+ def setup_relations(relation)
18
+ self.relations = relation.map { |e| relation_class.new(e) }
18
19
  end
19
20
  end
20
21
  end
@@ -1,13 +1,16 @@
1
1
  module Outbrain
2
2
  module Api
3
3
  class Report < Base
4
- REPORT_PARAMETERS = [:from, :to, :limit, :offset, :sort]
5
- RESOURCE_NAME = 'details'
4
+ def self.resource_name
5
+ 'details'
6
+ end
6
7
 
7
8
  def self.where(options = {})
8
- report_query = options.select{|option, v| REPORT_PARAMETERS.include?(option) && !v.nil?}
9
- report_options = { as: self, resource_name: RESOURCE_NAME, meta_resource_names: options.fetch(:meta_resource_names, []) }
10
- Request.where(options.fetch(:path), report_query, report_options)
9
+ report_options = {
10
+ as: self,
11
+ resource_name: resource_name,
12
+ }
13
+ Request.where(self.path(options), options, report_options)
11
14
  end
12
15
  end
13
16
  end
@@ -1,5 +1,5 @@
1
1
  module Outbrain
2
2
  module Api
3
- VERSION = "0.2.2"
3
+ VERSION = "0.2.3"
4
4
  end
5
5
  end
data/lib/outbrain/base.rb CHANGED
@@ -24,5 +24,9 @@ module Outbrain
24
24
  def persisted?
25
25
  try(:id).present?
26
26
  end
27
+
28
+ def extract_query_options(options, query_keys)
29
+ options.partition{|option, v| query_keys.include?(option) && !v.nil?}
30
+ end
27
31
  end
28
32
  end
@@ -1,7 +1,5 @@
1
1
  module Outbrain
2
2
  class Config
3
- BASE = Outbrain::Connection::BASE
4
-
5
3
  @@api = nil
6
4
  @@api_version = nil
7
5
 
@@ -5,7 +5,7 @@ module Outbrain
5
5
  class Connection
6
6
  BASE = 'https://api.outbrain.com'
7
7
  DEFAULT_API_VERSION = 'v0.1'
8
- attr_accessor :token, :user_name, :user_password, :connection, :api_version, :logging
8
+ attr_accessor :token, :user_name, :user_password, :connection, :api_version, :logging, :base_url
9
9
 
10
10
  def initialize(args={})
11
11
  @token = args[:token] || args['token']
@@ -13,7 +13,7 @@ module Outbrain
13
13
  @user_password = args[:user_password] || args['user_password']
14
14
  @api_version = args[:api_version] || args['api_version'] || DEFAULT_API_VERSION
15
15
  @logging = args[:logging] || args['logging'] || true # (default right now)
16
-
16
+ @base_url = args[:base_url] || "#{BASE}/amplify/#{api_version}/"
17
17
  get_token! unless @token
18
18
  # should raise if not authenticated properly
19
19
  end
@@ -28,13 +28,13 @@ module Outbrain
28
28
  # authenticates using basic auth to get token.
29
29
  # => http://docs.amplifyv01.apiary.io/#reference/authentications
30
30
  def get_token!
31
- @temp_api = Faraday.new(:url => BASE) do |faraday|
31
+ @temp_api = Faraday.new(:url => base_url) do |faraday|
32
32
  faraday.response :logger if logging
33
33
  faraday.adapter Faraday.default_adapter
34
34
  end
35
35
 
36
36
  @temp_api.basic_auth user_name, user_password
37
- response = @temp_api.get("/amplify/#{api_version}/login")
37
+ response = @temp_api.get("/login")
38
38
  @token = JSON.parse(response.body)[Outbrain::HEADER_AUTH_KEY]
39
39
  # need to raise error here if token does not exist
40
40
  end
@@ -44,7 +44,7 @@ module Outbrain
44
44
  end
45
45
 
46
46
  def refresh_api
47
- @api = Faraday.new(:url => BASE) do |faraday|
47
+ @api = Faraday.new(:url => base_url) do |faraday|
48
48
  faraday.response :logger if logging
49
49
  faraday.adapter Faraday.default_adapter
50
50
  faraday.headers['Content-Type'] = 'application/json'
@@ -1,25 +1,20 @@
1
1
  module Outbrain
2
2
  class Request < Config
3
3
  def self.where(resource_path, query={}, options={})
4
- request_path = "/amplify/#{api_version}/#{resource_path}"
4
+ request_path = resource_path
5
5
  query_string = query.map{|k,v| "#{k}=#{v}"}.join("&")
6
6
  request_path = request_path + '?' + query_string unless query_string.empty?
7
7
  response = api.get(request_path)
8
8
  resource_name = options.fetch(:resource_name, resource_path)
9
- meta_requests = options.fetch(:meta_resource_names)
10
9
  json_body = JSON.parse(response.body) # catch and raise proper api error
11
10
 
12
11
  fail InvalidOption 'requires an as option' unless options[:as]
13
12
 
14
13
  if response.status == 200
15
- Outbrain::Api::Relation.new.tap do |r|
16
- meta_requests.each{ |field| r.send("#{field}=", json_body[field]) }
17
- r.relations = json_body[resource_name].map{ |obj| options[:as].new(obj) }
18
- end
14
+ Outbrain::Api::Relation
15
+ .new(json_body.merge(relation_class: options[:as], relation_name: resource_name))
19
16
  else
20
- Outbrain::Api::Relation.new.tap do |r|
21
- r.errors << json_body
22
- end
17
+ Outbrain::Api::Relation.new(errors: json_body)
23
18
  end
24
19
  end
25
20
 
@@ -28,7 +23,7 @@ module Outbrain
28
23
  end
29
24
 
30
25
  def self.find(resource_path, id, options={})
31
- response = api.get("/amplify/#{api_version}/#{resource_path}/#{id}")
26
+ response = api.get("#{resource_path}/#{id}")
32
27
  json_body = JSON.parse(response.body)
33
28
 
34
29
  fail InvalidOption 'requires an as option' unless options[:as]
@@ -45,7 +40,7 @@ module Outbrain
45
40
  def self.create(resource, options={})
46
41
  attributes = options.fetch(:attributes, {})
47
42
 
48
- response = api.post("/amplify/#{api_version}/#{resource}", attributes.to_json)
43
+ response = api.post("#{resource}", attributes.to_json)
49
44
  json_body = JSON.parse(response.body)
50
45
 
51
46
  if response.status == 200
@@ -59,7 +54,7 @@ module Outbrain
59
54
 
60
55
  def self.update(resource_path, id, options={})
61
56
  attributes = options.fetch(:attributes, {})
62
- response = api.put("/amplify/#{api_version}/#{resource_path}/#{id}", attributes.to_json)
57
+ response = api.put("#{resource_path}/#{id}", attributes.to_json)
63
58
  json_body = JSON.parse(response.body)
64
59
 
65
60
  if response.status == 200
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: outbrain-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Blanchet
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-11 00:00:00.000000000 Z
11
+ date: 2016-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -118,6 +118,8 @@ files:
118
118
  - lib/outbrain/api/config.rb
119
119
  - lib/outbrain/api/errors.rb
120
120
  - lib/outbrain/api/marketer.rb
121
+ - lib/outbrain/api/promoted_link.rb
122
+ - lib/outbrain/api/promoted_link_performance_report.rb
121
123
  - lib/outbrain/api/publisher.rb
122
124
  - lib/outbrain/api/publisher_report.rb
123
125
  - lib/outbrain/api/relation.rb