perfecta 0.1.2 → 0.2.0

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.
data/README.md CHANGED
@@ -39,23 +39,105 @@ You can now use the client instance to query the API.
39
39
  p client.campaign_reports.inspect
40
40
  ```
41
41
 
42
+ Optional Query Parameters include
43
+
44
+ | Name | Type |
45
+ | ------------- |:-------------------------------------------------------------:|
46
+ | interval | ['today','yesterday','last_7_days','last_30_days','lifetime'] |
47
+ | start_date | 'YYYY-MM-DD' |
48
+ | end_date | 'YYYY-MM-DD' |
49
+ | site_id | 'the_site_id' |
50
+ | campaign_id | 'the_site_id' |
51
+
52
+ and can be used by supplying a hash e.g.
53
+
54
+ ```ruby
55
+ p client.campaign_reports(interval: 'yesterday').inspect
56
+ ```
57
+
42
58
  ### List all Ad Reports
43
59
 
44
60
  ```ruby
45
61
  p client.ad_reports.inspect
46
62
  ```
47
63
 
64
+ Any of the additional query parameters from the above table can be supplied as a hash.
65
+
48
66
  ### List all Conversion Reports
49
67
 
50
68
  ```ruby
51
69
  p client.conversion_reports.inspect
52
70
  ```
53
71
 
72
+ Any of the additional query parameters from the above table can be supplied as a hash.
73
+
74
+ ### List all Sites
75
+
76
+ ```ruby
77
+ p client.sites.inspect
78
+ ```
79
+
80
+ ### Show a single Site
81
+
82
+ ```ruby
83
+ p client.site('SITE_ID').inspect
84
+ ```
85
+
86
+ ### List all Campaigns
87
+
88
+ ```ruby
89
+ p client.campaigns.inspect
90
+ ```
91
+
92
+ ### Show a single Campaign
93
+
94
+ ```ruby
95
+ p client.campaign('CAMPAIGN_ID').inspect
96
+ ```
97
+
98
+ ### List all Ads
99
+
100
+ ```ruby
101
+ p client.ads.inspect
102
+ ```
103
+
104
+ ### Show a single Ad
105
+
106
+ ```ruby
107
+ p client.ad('AD_ID').inspect
108
+ ```
109
+
110
+ ### List all Segments
111
+
112
+ ```ruby
113
+ p client.segments.inspect
114
+ ```
115
+
116
+ ### Show a single Segment
117
+
118
+ ```ruby
119
+ p client.segment('SEGMENT_ID').inspect
120
+ ```
121
+
122
+ ### List all Conversions
123
+
124
+ ```ruby
125
+ p client.conversions.inspect
126
+ ```
127
+
128
+ ### Show a single Conversion
129
+
130
+ ```ruby
131
+ p client.conversion('CONVERSION_ID').inspect
132
+ ```
133
+
54
134
  ## TODO
55
135
 
56
- 1. Flesh out the rest of the API
57
- 2. Error handling, specifically for when token expires
58
- 3. Relationships and delegation between of objects
136
+ 1. ~~Flesh out the rest of the API~~
137
+ 2. ~~Accept query parameters~~
138
+ 3. Error handling, specifically for when token expires
139
+ 4. Relationships and delegation between of objects
140
+ 5. ~~Get rid of all the duplication~~
59
141
 
60
142
  ## Contributing
61
143
 
@@ -0,0 +1,7 @@
1
+ module Perfecta
2
+ class Ad < ApiResource
3
+ def initialize attrs
4
+ super attrs
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Perfecta
2
+ class Campaign < ApiResource
3
+ def initialize attrs
4
+ super attrs
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Perfecta
2
+ class Conversion < ApiResource
3
+ def initialize attrs
4
+ super attrs
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Perfecta
2
+ class Segment < ApiResource
3
+ def initialize attrs
4
+ super attrs
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Perfecta
2
+ class Site < ApiResource
3
+ def initialize attrs
4
+ super attrs
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module Perfecta
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/perfecta.rb CHANGED
@@ -3,14 +3,28 @@ require "perfecta/api_resource"
3
3
  require "perfecta/campaign_report"
4
4
  require "perfecta/ad_report"
5
5
  require "perfecta/conversion_report"
6
+ require "perfecta/site"
7
+ require "perfecta/campaign"
8
+ require "perfecta/ad"
9
+ require "perfecta/conversion"
10
+ require "perfecta/segment"
6
11
  require "rest-client"
7
12
  require "json"
13
+ require "active_support/inflector"
8
14
 
9
15
  module Perfecta
10
16
  class Client
11
17
 
12
18
  BASE_API_PATH = 'https://api.perfectaudience.com'
13
19
 
20
+ ALLOWED_REPORT_PARAMS = [
21
+ :interval,
22
+ :start_date,
23
+ :end_date,
24
+ :site_id,
25
+ :campaign_id
26
+ ].freeze
27
+
14
28
  attr_accessor :email, :password, :token
15
29
 
16
30
  def initialize &block
@@ -18,28 +32,71 @@ module Perfecta
18
32
  @token = exchange_credentials_for_token
19
33
  end
20
34
 
21
- def campaign_reports
22
- url = "#{BASE_API_PATH}/reports/campaign_report"
23
- resp = JSON.parse(RestClient.get(url, {Authorization: @token}))
35
+ def campaign_reports params = {}
36
+ url, params = build_report_url_and_params_for('campaign', params)
37
+
38
+ resp = JSON.parse(RestClient.get(url, params))
39
+
40
+ build_collection_of 'CampaignReport', resp
41
+ end
42
+
43
+ def ad_reports params = {}
44
+ url, params = build_report_url_and_params_for('ad', params)
45
+
46
+ resp = JSON.parse(RestClient.get(url, params))
47
+
48
+ build_collection_of 'AdReport', resp
49
+ end
50
+
51
+ def conversion_reports params = {}
52
+ url, params = build_report_url_and_params_for('conversion', params)
53
+
54
+ resp = JSON.parse(RestClient.get(url, params))
55
+
56
+ build_collection_of 'ConversionReport', resp
57
+ end
58
+
59
+ def sites
60
+ get_many 'sites'
61
+ end
62
+
63
+ def site id
64
+ get_one 'site', id
65
+ end
66
+
67
+ def campaigns
68
+ get_many 'campaigns'
69
+ end
70
+
71
+ def campaign id
72
+ get_one 'campaign', id
73
+ end
74
+
75
+ def ads
76
+ get_many 'ads'
77
+ end
24
78
 
25
- build_collection_of 'CampaignReport', resp['report']
79
+ def ad id
80
+ get_one 'ad', id
26
81
  end
27
82
 
28
- def ad_reports
29
- url = "#{BASE_API_PATH}/reports/ad_report"
30
- resp = JSON.parse(RestClient.get(url, {Authorization: @token}))
83
+ def conversions
84
+ get_many 'conversions'
85
+ end
31
86
 
32
- build_collection_of 'AdReport', resp['report']
87
+ def conversion id
88
+ get_one 'conversion', id
33
89
  end
34
90
 
35
- def conversion_reports
36
- url = "#{BASE_API_PATH}/reports/conversion_report"
37
- resp = JSON.parse(RestClient.get(url, {Authorization: @token}))
91
+ def segments
92
+ get_many 'segments'
93
+ end
38
94
 
39
- build_collection_of 'ConversionReport', resp['report']
95
+ def segment id
96
+ get_one 'segment', id
40
97
  end
41
98
 
42
- private
99
+ private
43
100
 
44
101
  def exchange_credentials_for_token
45
102
  authenticate
@@ -61,12 +118,56 @@ module Perfecta
61
118
  def build_collection_of klass, data
62
119
  retval = []
63
120
 
64
- data.each do |d|
121
+ raise BadResponse unless data['status'] == 200
122
+
123
+ data['report'].each do |d|
65
124
  obj = Perfecta.const_get(klass.to_sym).new d
66
125
  retval << obj
67
126
  end
68
127
 
69
128
  retval
70
129
  end
130
+
131
+ def get_one type, id
132
+ url = "#{BASE_API_PATH}/#{type.pluralize}/#{id}"
133
+
134
+ resp = JSON.parse(RestClient.get(url, Authorization: @token))
135
+
136
+ klass_name = type.capitalize
137
+ klass = Perfecta.const_get(klass_name.to_sym)
138
+
139
+ obj = klass.new resp[type]
140
+
141
+ obj
142
+ end
143
+
144
+ def get_many type
145
+ url = "#{BASE_API_PATH}/#{type}"
146
+
147
+ resp = JSON.parse(RestClient.get(url, Authorization: @token))
148
+
149
+ klass_name = type.capitalize.singularize
150
+ klass = Perfecta.const_get(klass_name.to_sym)
151
+
152
+ retval = []
153
+
154
+ resp[type].each do |obj|
155
+ retval << klass.new(obj)
156
+ end
157
+
158
+ retval
159
+ end
160
+
161
+ def build_report_url_and_params_for type, params
162
+ url = "#{BASE_API_PATH}/reports/#{type}_report"
163
+
164
+ params.keep_if {|p| ALLOWED_REPORT_PARAMS.include? p}
165
+
166
+ params = {params: params}.merge! Authorization: @token
167
+
168
+ [url, params]
169
+ end
71
170
  end
171
+
172
+ class BadResponse < Exception; end
72
173
  end
data/perfecta.gemspec CHANGED
@@ -18,5 +18,6 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_dependency "rest-client"
21
+ gem.add_dependency "activesupport"
21
22
  gem.add_development_dependency "mocha"
22
23
  end
@@ -0,0 +1,20 @@
1
+ require_relative 'test_helper'
2
+
3
+ class CampaignTest < MiniTest::Unit::TestCase
4
+
5
+ describe 'Campaign' do
6
+
7
+ before do
8
+ @attrs = {name: 'Campaign 1'}
9
+ @report = Perfecta::Campaign.new @attrs
10
+ end
11
+
12
+ it 'should be created from a hash' do
13
+ @report.must_be_instance_of Perfecta::Campaign
14
+ end
15
+
16
+ it 'should set the values for each property' do
17
+ @report.name.must_equal 'Campaign 1'
18
+ end
19
+ end
20
+ end
data/test/site_test.rb ADDED
@@ -0,0 +1,20 @@
1
+ require_relative 'test_helper'
2
+
3
+ class SiteTest < MiniTest::Unit::TestCase
4
+
5
+ describe 'Site' do
6
+
7
+ before do
8
+ @attrs = {name: 'Site 1'}
9
+ @report = Perfecta::Site.new @attrs
10
+ end
11
+
12
+ it 'should be created from a hash' do
13
+ @report.must_be_instance_of Perfecta::Site
14
+ end
15
+
16
+ it 'should set the values for each property' do
17
+ @report.name.must_equal 'Site 1'
18
+ end
19
+ end
20
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: perfecta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: mocha
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -56,16 +72,23 @@ files:
56
72
  - README.md
57
73
  - Rakefile
58
74
  - lib/perfecta.rb
75
+ - lib/perfecta/ad.rb
59
76
  - lib/perfecta/ad_report.rb
60
77
  - lib/perfecta/api_resource.rb
78
+ - lib/perfecta/campaign.rb
61
79
  - lib/perfecta/campaign_report.rb
80
+ - lib/perfecta/conversion.rb
62
81
  - lib/perfecta/conversion_report.rb
82
+ - lib/perfecta/segment.rb
83
+ - lib/perfecta/site.rb
63
84
  - lib/perfecta/version.rb
64
85
  - perfecta.gemspec
65
86
  - test/ad_report_test.rb
66
87
  - test/campaign_report_test.rb
88
+ - test/campaign_test.rb
67
89
  - test/conversion_report_test.rb
68
90
  - test/perfecta_test.rb
91
+ - test/site_test.rb
69
92
  - test/test_helper.rb
70
93
  homepage: https://github.com/gary-rafferty/perfecta
71
94
  licenses: []
@@ -94,6 +117,8 @@ summary: Ruby client for the PerfectAudience Reporting API
94
117
  test_files:
95
118
  - test/ad_report_test.rb
96
119
  - test/campaign_report_test.rb
120
+ - test/campaign_test.rb
97
121
  - test/conversion_report_test.rb
98
122
  - test/perfecta_test.rb
123
+ - test/site_test.rb
99
124
  - test/test_helper.rb