outbrain-api 0.1.0 → 0.1.1

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: 0f66b5ef4e32101b544427473855635ee498d3e4
4
- data.tar.gz: 0e894ebfe8a7655a7827b590f3f8e7f779996f74
3
+ metadata.gz: c989cd84656d8f8bb53a3cf81091017746affd6a
4
+ data.tar.gz: 70494a86c706a0c49d2e357de5b23b04c4d4d331
5
5
  SHA512:
6
- metadata.gz: 2d84897df03ae6cc597a4ed2f21ebd05ff14e2b0cbdb4295d4cab8740dcbd4c790c17a2da618d67b403405dac50f087c8ded3794f619dea34632edb2d9c45a9e
7
- data.tar.gz: 64035b9c6963f5d0b0411da87a79216a6cdbf8b552d33ec95d3e4905d371323ae82899316b4874230dd892784ef65d3d0a7c458ab30d1402285e2acaf95eb154
6
+ metadata.gz: 213c1a88c77290236f758ef2a699fccd4e17f5de66089e8a28670c34648fbc451099f6d824514ccee9054354a05c3b70b44cb6c4e14d52c015125b37e458e808
7
+ data.tar.gz: e7d72ffcccff1b7ea7dfdf6a2bfb7df746722629dceed2d1b067445b3a8484a70b47b963312dcbac4d8b97de43bdf0a2e396b472104ef6ea0baf05b4447fda4f
@@ -1,56 +1,18 @@
1
1
  require "outbrain/api/version"
2
- require 'faraday'
3
- require 'json'
2
+
3
+ # configs
4
+ require "outbrain/connection"
5
+ require "outbrain/config"
6
+ require "outbrain/base"
7
+ require "outbrain/request"
8
+
9
+ # models
10
+ require "outbrain/api/marketer"
11
+ require "outbrain/api/campaign"
4
12
 
5
13
  module Outbrain
6
14
  HEADER_AUTH_KEY = 'OB-TOKEN-V1'
7
15
 
8
16
  module Api
9
17
  end
10
-
11
- class Connection
12
- attr_accessor :token, :user_name, :user_password, :connection, :api_version, :logging
13
-
14
- def initialize(args={})
15
- @token = args[:token] || args['token']
16
- @user_name = args[:user_name] || args['user_name']
17
- @user_password = args[:user_password] || args['user_password']
18
- @api_version = args[:api_version] || args['api_version'] || 'v0.1'
19
- @logging = args[:logging] || args['logging'] || true # (default right now)
20
-
21
- get_token! unless @token
22
- # should raise if not authenticated properly
23
- end
24
-
25
- # authenticates using basic auth to get token.
26
- # => http://docs.amplifyv01.apiary.io/#reference/authentications
27
- def get_token!
28
- @temp_connection = Faraday.new(:url => "https://api.outbrain.com") do |faraday|
29
- faraday.response :logger if logging
30
- faraday.adapter Faraday.default_adapter
31
- end
32
-
33
- @temp_connection.basic_auth user_name, user_password
34
- response = @temp_connection.get("/amplify/#{api_version}/login")
35
- @token = JSON.parse(response.body)[HEADER_AUTH_KEY]
36
- # need to raise error here if token does not exist
37
- end
38
-
39
- def connection
40
- @connection ||= refresh_connection
41
- end
42
-
43
- def refresh_connection
44
- @connection = Faraday.new(:url => "https://api.outbrain.com") do |faraday|
45
- faraday.response :logger if @logging
46
- faraday.adapter Faraday.default_adapter
47
- faraday.headers['Content-Type'] = 'application/json'
48
- faraday.headers[HEADER_AUTH_KEY] = token
49
- end
50
- end
51
-
52
- def marketers
53
- connection.get("/amplify/#{api_version}/marketers")
54
- end
55
- end
56
18
  end
@@ -0,0 +1,20 @@
1
+ module Outbrain
2
+ module Api
3
+ class Budget < Base
4
+
5
+ def self.path(id)
6
+ "/marketers/#{id}/budgets"
7
+ end
8
+
9
+ def self.find_by(attributes={})
10
+ marketer_id = attributes[:marketer_id]
11
+ fail InvalidOption 'find_by requires marketer-id' unless marketer_id
12
+ Request.all(path(marketer_id), { as: self, resource_name: 'budgets'})
13
+ end
14
+
15
+ def create_campaign(attributes)
16
+ Campaign.create(attributes.merge(budgetId: id))
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ module Outbrain
2
+ module Api
3
+ class Campaign < Base
4
+ PATH = 'campaigns'
5
+
6
+ def self.create(attributes)
7
+ Request.create(PATH, { as: self, attributes: attributes })
8
+ end
9
+ end
10
+ end
11
+ end
File without changes
@@ -0,0 +1,5 @@
1
+ module Outbrain
2
+ module Api
3
+ class EndpointNotAvialable < NoMethodError; end
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ require "outbrain/api/publisher"
2
+ require "outbrain/api/budget"
3
+
4
+ module Outbrain
5
+ module Api
6
+ class Marketer < Base
7
+ coerce_key :blockedPublishers, Array[Publisher]
8
+
9
+ PATH = 'marketers'
10
+
11
+ def self.all
12
+ Request.all(PATH, { as: self })
13
+ end
14
+
15
+ def self.create(*)
16
+ raise EndpointNotAvialable.new('Marketers can not be created via the api.')
17
+ end
18
+
19
+ def budgets
20
+ Budget.find_by(marketer_id: id)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ module Outbrain
2
+ module Api
3
+ class Publisher < Base ; end
4
+ end
5
+ end
@@ -1,5 +1,5 @@
1
1
  module Outbrain
2
2
  module Api
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -0,0 +1,28 @@
1
+ require 'hashie'
2
+
3
+ # remaking some of active-model here :-( but don't want rails
4
+
5
+ module Outbrain
6
+ class Base < Hash
7
+ include Hashie::Extensions::MethodAccess
8
+ include Hashie::Extensions::Coercion
9
+
10
+ attr_accessor :errors
11
+
12
+ def initialize(attributes={})
13
+ attributes.each do |key, value|
14
+ self.send("#{key}=", value)
15
+ end
16
+ @errors = attributes.fetch(:errors, [])
17
+ self
18
+ end
19
+
20
+ def valid?
21
+ errors.emtpy?
22
+ end
23
+
24
+ def persisted?
25
+ try(:id).present?
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,33 @@
1
+ module Outbrain
2
+ class Config
3
+ BASE = Outbrain::Connection::BASE
4
+
5
+ @@api = nil
6
+ @@api_version = nil
7
+
8
+ def self.api=(api)
9
+ @@api = api
10
+ end
11
+
12
+ def self.api_version=(api_version)
13
+ @@api_version = api_version
14
+ end
15
+
16
+ private
17
+ def self.api
18
+ @@api
19
+ end
20
+
21
+ def api
22
+ self.class.api
23
+ end
24
+
25
+ def api_version
26
+ self.class.api_version
27
+ end
28
+
29
+ def self.api_version
30
+ @@api_version
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,55 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module Outbrain
5
+ class Connection
6
+ BASE = 'https://api.outbrain.com'
7
+ DEFAULT_API_VERSION = 'v0.1'
8
+ attr_accessor :token, :user_name, :user_password, :connection, :api_version, :logging
9
+
10
+ def initialize(args={})
11
+ @token = args[:token] || args['token']
12
+ @user_name = args[:user_name] || args['user_name']
13
+ @user_password = args[:user_password] || args['user_password']
14
+ @api_version = args[:api_version] || args['api_version'] || DEFAULT_API_VERSION
15
+ @logging = args[:logging] || args['logging'] || true # (default right now)
16
+
17
+ get_token! unless @token
18
+ # should raise if not authenticated properly
19
+ end
20
+
21
+ def self.configure(params={})
22
+ connection = new(params)
23
+ Config.api = connection.api
24
+ Config.api_version = connection.api_version
25
+ connection
26
+ end
27
+
28
+ # authenticates using basic auth to get token.
29
+ # => http://docs.amplifyv01.apiary.io/#reference/authentications
30
+ def get_token!
31
+ @temp_api = Faraday.new(:url => BASE) do |faraday|
32
+ faraday.response :logger if logging
33
+ faraday.adapter Faraday.default_adapter
34
+ end
35
+
36
+ @temp_api.basic_auth user_name, user_password
37
+ response = @temp_api.get("/amplify/#{api_version}/login")
38
+ @token = JSON.parse(response.body)[Outbrain::HEADER_AUTH_KEY]
39
+ # need to raise error here if token does not exist
40
+ end
41
+
42
+ def api
43
+ @api ||= refresh_api
44
+ end
45
+
46
+ def refresh_api
47
+ @api = Faraday.new(:url => BASE) do |faraday|
48
+ faraday.response :logger if logging
49
+ faraday.adapter Faraday.default_adapter
50
+ faraday.headers['Content-Type'] = 'application/json'
51
+ faraday.headers[Outbrain::HEADER_AUTH_KEY] = token
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,35 @@
1
+ module Outbrain
2
+ class Request < Config
3
+
4
+ def self.where(resource_path, query={}, options={})
5
+ response = api.get("/amplify/#{api_version}/#{resource_path}")
6
+ resource_name = options.fetch(:resource_name, resource_path)
7
+ json_body = JSON.parse(response.body) # catch and raise proper api error
8
+
9
+ fail InvalidOption 'requires an as option' unless options[:as]
10
+
11
+ json_body[resource_name].map do |obj|
12
+ options[:as].new(obj)
13
+ end
14
+ end
15
+
16
+ def self.all(resource, options={})
17
+ where(resource, {}, options)
18
+ end
19
+
20
+ def self.create(resource, options={})
21
+ attributes = options.fetch(:attributes, {})
22
+
23
+ response = api.post("/amplify/#{api_version}/#{resource}", attributes.to_json)
24
+ json_body = JSON.parse(response.body)
25
+
26
+ if response.status == 200
27
+ options[:as].new(json_body)
28
+ else
29
+ a = options[:as].new
30
+ a.errors.push(json_body)
31
+ a
32
+ end
33
+ end
34
+ end
35
+ end
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_runtime_dependency("faraday")
23
23
  spec.add_runtime_dependency("json")
24
+ spec.add_runtime_dependency("hashie")
24
25
 
25
26
  spec.add_development_dependency "bundler", "~> 1.11"
26
27
  spec.add_development_dependency "rake", "~> 10.0"
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.1.0
4
+ version: 0.1.1
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-03-03 00:00:00.000000000 Z
11
+ date: 2016-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: hashie
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -97,7 +111,17 @@ files:
97
111
  - bin/console
98
112
  - bin/setup
99
113
  - lib/outbrain/api.rb
114
+ - lib/outbrain/api/budget.rb
115
+ - lib/outbrain/api/campaign.rb
116
+ - lib/outbrain/api/config.rb
117
+ - lib/outbrain/api/errors.rb
118
+ - lib/outbrain/api/marketer.rb
119
+ - lib/outbrain/api/publisher.rb
100
120
  - lib/outbrain/api/version.rb
121
+ - lib/outbrain/base.rb
122
+ - lib/outbrain/config.rb
123
+ - lib/outbrain/connection.rb
124
+ - lib/outbrain/request.rb
101
125
  - outbrain-api.gemspec
102
126
  homepage: https://github.com/simplereach/outbrain-api
103
127
  licenses: