awesm 0.1.6 → 0.1.7

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/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ .DS_Store
data/README.md CHANGED
@@ -21,21 +21,44 @@ And in your code:
21
21
  # Create a project
22
22
  project = Awesm::Project.create(:name => 'TotallyAwesm')
23
23
  project.api_key # => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9'
24
+ # or
25
+ project = Awesm::Project.new(:name => 'TotallyAwesm')
26
+ project.save # => true
27
+ project.api_key # => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9'
24
28
 
25
29
  # List projects
26
30
  projects = Awesm::Project.list
27
31
  projects.first.class # => Awesm::Project
28
32
 
33
+ # Notify Awe.sm of a goal conversion
34
+ Awesm::Conversion.convert(
35
+ :key => "f2d8aeb112f1e0bedd7c05653e3265d2622635a3180f336f73b172267f7fe6ee",
36
+ :awesm_url => "awe.sm_5WXHo",
37
+ :conversion_type => "goal_1",
38
+ :conversion_value => 1230
39
+ )
40
+ # => #<Awesm::Conversion account_conversionid=nil account_id="12" account_userid=nil awesm_url="awe.sm_5WXHo" clicker_id=nil converted_at=1323475432 href=nil id="bfdaddec-2298-43fb-9da0-f12d81febbf6" ip_address=nil language=nil redirection_id="94585739" referrer=nil session_id=nil type="goal_1" user_agent=nil value=1230>
41
+
29
42
  # Create a sharing link
30
- Awesm::Url.share(:url => 'http://developers.awe.sm/',
31
- :key => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
32
- :tool => 'mKU7uN',
33
- :channel => 'twitter',
34
- :destination => 'http://twitter.com/intent/tweet?text=This+is+the+coolest+API+evar!%26url=AWESM_URL%26via=awesm',
35
- :parent => 'awe.sm_s5d99',
36
- :user_id => '42')
43
+ Awesm::Url.share(
44
+ :url => 'http://developers.awe.sm/',
45
+ :key => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
46
+ :tool => 'mKU7uN',
47
+ :channel => 'twitter',
48
+ :destination => 'http://twitter.com/intent/tweet?text=This+is+the+coolest+API+evar!%26url=AWESM_URL%26via=awesm',
49
+ :parent => 'awe.sm_s5d99',
50
+ :user_id => '42'
51
+ )
37
52
  # => "http://api.awe.sm/url/share?v=3&url=http://developers.awe.sm/&key=5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9&tool=mKU7uN&channel=twitter&destination=http://twitter.com/intent/tweet?text=This+is+the+coolest+API+evar!%26url=AWESM_URL%26via=awesm&parent=awe.sm_s5d99&user_id=42"
38
53
 
54
+ # Retrieve stats in return for your hard work!
55
+ stats = Awesm::Stats.range(
56
+ :key => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
57
+ :start_date => '2011-09-01',
58
+ :end_date => '2011-10-01'
59
+ )
60
+ stats.totals.clicks # => 1024
61
+
39
62
  ## Contributing ##
40
63
 
41
64
  * fork
@@ -6,6 +6,7 @@ require 'awesm/version'
6
6
  require 'awesm/project'
7
7
  require 'awesm/conversion'
8
8
  require 'awesm/url'
9
+ require 'awesm/stats'
9
10
 
10
11
  module Awesm
11
12
  def self.subscription_key=(key)
@@ -4,12 +4,8 @@ module Awesm
4
4
  base_uri 'http://api.awe.sm/projects'
5
5
 
6
6
  def self.create(attributes)
7
- response = post('/new', :query => { :application_key => Awesm.application_key, :subscription_key => Awesm.subscription_key, :json => attributes.to_json })
8
- if response.has_key?("error")
9
- nil
10
- else
11
- new(response['response']['project'])
12
- end
7
+ project = self.new(attributes)
8
+ project.save ? project : nil
13
9
  end
14
10
 
15
11
  def self.list
@@ -24,5 +20,21 @@ module Awesm
24
20
  projects
25
21
  end
26
22
  end
23
+
24
+ ####################
25
+ # Instance Methods #
26
+ ####################
27
+
28
+ def save
29
+ response = self.class.post('/new', :query => { :application_key => Awesm.application_key, :subscription_key => Awesm.subscription_key, :json => self.to_hash.to_json })
30
+ if response.include?('error')
31
+ # use mash to update error code and message
32
+ update(response)
33
+ false
34
+ else
35
+ update(response['response']['project'])
36
+ true
37
+ end
38
+ end
27
39
  end
28
40
  end
@@ -0,0 +1,16 @@
1
+ module Awesm
2
+ class Stats < Hashie::Mash
3
+ include HTTParty
4
+ base_uri 'http://api.awe.sm/stats'
5
+ format :json
6
+
7
+ def self.range(options)
8
+ response = get '/range.json', :query => { :v => 3 }.merge(options)
9
+ if response.has_key?('error')
10
+ nil
11
+ else
12
+ new(response)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module Awesm
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
@@ -65,7 +65,7 @@ describe Awesm::Conversion do
65
65
  :key => "badkeyabcdefghijklmnopqrstuvwxyz1234567890",
66
66
  :awesm_url => "awe.sm_5WXHo",
67
67
  :conversion_type => "goal_1",
68
- :conversion_value => "1230"
68
+ :conversion_value => 1230
69
69
  }
70
70
  conversion = Awesm::Conversion.convert(params)
71
71
  conversion.should == nil
@@ -78,7 +78,7 @@ describe Awesm::Conversion do
78
78
  :key => "f2d8aeb112f1e0bedd7c05653e3265d2622635a3180f336f73b172267f7fe6ee",
79
79
  :awesm_url => "awe.sm_5WXHo",
80
80
  :conversion_type => "goal_1",
81
- :conversion_value => "1230"
81
+ :conversion_value => 1230
82
82
  }
83
83
 
84
84
  conversion = Awesm::Conversion.convert(params)
@@ -91,7 +91,7 @@ describe Awesm::Conversion do
91
91
  :key => "f2d8aeb112f1e0bedd7c05653e3265d2622635a3180f336f73b172267f7fe6ee",
92
92
  :awesm_url => "awe.sm_5WXHo",
93
93
  :conversion_type => "goal_1",
94
- :conversion_value => "1230"
94
+ :conversion_value => 1230
95
95
  }
96
96
 
97
97
  conversion = Awesm::Conversion.convert(params)
@@ -12,53 +12,53 @@ describe Awesm::Project do
12
12
  Awesm.application_key = nil
13
13
  end
14
14
 
15
- context '.create' do
16
- let(:json_response) do
17
- {
18
- "request" => {
19
- "application_key" => "app-xxxxxx",
20
- "json" => "{\"name\" =>\"TotallyAwesomeProject\"}",
21
- "method" => "new",
22
- "object" => "project",
23
- "subscription_key" => "sub-xxxxxx"
24
- },
25
- "response" => {
26
- "project" => {
27
- "admins" => [],
28
- "api_key" => "6xxxxxxxxx58xx0xxx74xx3x76xx83x6x34xx48x7xxxx55x167037818d65x66x",
29
- "created_at" => "2011-10-25 00:43:49",
30
- "default_domain" => "awe.sm",
31
- "domains" => [],
32
- "name" => "TotallyAwesomeProject",
33
- "sharers" => [],
34
- "updated_at" => "2011-10-25 00:43:49",
35
- "viewers" => []
36
- }
15
+ let(:new_project_response) do
16
+ {
17
+ "request" => {
18
+ "application_key" => "app-xxxxxx",
19
+ "json" => "{\"name\" =>\"TotallyAwesomeProject\"}",
20
+ "method" => "new",
21
+ "object" => "project",
22
+ "subscription_key" => "sub-xxxxxx"
23
+ },
24
+ "response" => {
25
+ "project" => {
26
+ "admins" => [],
27
+ "api_key" => "6xxxxxxxxx58xx0xxx74xx3x76xx83x6x34xx48x7xxxx55x167037818d65x66x",
28
+ "created_at" => "2011-10-25 00:43:49",
29
+ "default_domain" => "awe.sm",
30
+ "domains" => [],
31
+ "name" => "TotallyAwesomeProject",
32
+ "sharers" => [],
33
+ "updated_at" => "2011-10-25 00:43:49",
34
+ "viewers" => []
37
35
  }
38
- }.to_json
39
- end
36
+ }
37
+ }.to_json
38
+ end
40
39
 
41
- let(:json_error_response) do
42
- {
43
- "request" => {
44
- "application_key" => "app-xxxxxx",
45
- "json" => "{\"name\" =>\"ExistingAwesomeProject\"}",
46
- "method" => "new",
47
- "object" => "project",
48
- "subscription_key" => "sub-xxxxxx"
49
- },
50
- "error" => {
51
- "code" => 10001,
52
- "message" => "Project name already exists (not necessarily in your subscription). Please choose another."
53
- }
54
- }.to_json
55
- end
40
+ let(:new_project_error_response) do
41
+ {
42
+ "request" => {
43
+ "application_key" => "app-xxxxxx",
44
+ "json" => "{\"name\" =>\"ExistingAwesomeProject\"}",
45
+ "method" => "new",
46
+ "object" => "project",
47
+ "subscription_key" => "sub-xxxxxx"
48
+ },
49
+ "error" => {
50
+ "code" => 10001,
51
+ "message" => "Project name already exists (not necessarily in your subscription). Please choose another."
52
+ }
53
+ }.to_json
54
+ end
56
55
 
56
+ context '.create' do
57
57
  before do
58
58
  stub_request(:post, "http://api.awe.sm/projects/new?json=%7B%22name%22:%22TotallyAwesomeProject%22%7D&subscription_key=sub-xxxxxx&application_key=app-xxxxxx").
59
- to_return(:status => 200, :body => json_response, :headers => { 'Content-Type' => 'application/json;charset=utf-8' })
59
+ to_return(:status => 200, :body => new_project_response, :headers => { 'Content-Type' => 'application/json;charset=utf-8' })
60
60
  stub_request(:post, "http://api.awe.sm/projects/new?json=%7B%22name%22:%22ExistingAwesomeProject%22%7D&subscription_key=sub-xxxxxx&application_key=app-xxxxxx").
61
- to_return(:status => 400, :body => json_error_response, :headers => { 'Content-Type' => 'application/json;charset=utf-8' })
61
+ to_return(:status => 400, :body => new_project_error_response, :headers => { 'Content-Type' => 'application/json;charset=utf-8' })
62
62
  end
63
63
 
64
64
  context 'when an error occurs' do
@@ -84,8 +84,60 @@ describe Awesm::Project do
84
84
  end
85
85
  end
86
86
 
87
+ describe '.new' do
88
+ it 'accepts project attributes' do
89
+ project = Awesm::Project.new(:name => 'HooHah')
90
+ project.name.should == 'HooHah'
91
+ end
92
+ end
93
+
94
+ describe '#save' do
95
+ before do
96
+ stub_request(:post, "http://api.awe.sm/projects/new?json=%7B%22name%22:%22TotallyAwesomeProject%22%7D&subscription_key=sub-xxxxxx&application_key=app-xxxxxx").
97
+ to_return(:status => 200, :body => new_project_response, :headers => { 'Content-Type' => 'application/json;charset=utf-8' })
98
+ stub_request(:post, "http://api.awe.sm/projects/new?json=%7B%22name%22:%22ExistingAwesomeProject%22%7D&subscription_key=sub-xxxxxx&application_key=app-xxxxxx").
99
+ to_return(:status => 400, :body => new_project_error_response, :headers => { 'Content-Type' => 'application/json;charset=utf-8' })
100
+ end
101
+
102
+ let(:project) { Awesm::Project.new(:name => 'TotallyAwesomeProject') }
103
+ let(:existing_project) { Awesm::Project.new(:name => 'ExistingAwesomeProject') }
104
+
105
+ context 'with no existing projects' do
106
+ it 'calls the correct project/new api' do
107
+ project.save
108
+
109
+ a_request(:post, "http://api.awe.sm/projects/new").
110
+ with(:query => {:subscription_key => "sub-xxxxxx", :application_key => "app-xxxxxx", :json => { "name" => "TotallyAwesomeProject" }.to_json }).
111
+ should have_been_made.once
112
+ end
113
+
114
+ it 'returns true' do
115
+ project.save.should == true
116
+ end
117
+ end
118
+
119
+ context 'when the project already exists' do
120
+ it 'returns false' do
121
+ existing_project.save.should == false
122
+ end
123
+
124
+ it 'sets the error code' do
125
+ existing_project.save
126
+ existing_project.error.code.should == 10001
127
+ end
128
+
129
+ it 'sets the error message' do
130
+ existing_project.save
131
+ existing_project.error.message.should == 'Project name already exists (not necessarily in your subscription). Please choose another.'
132
+ end
133
+ end
134
+
135
+ it 'updates a project in awe.sm if it exists and attributes have changed'
136
+ it 'does not update a project in awe.sm if it exists and attributes have not changed'
137
+ end
138
+
87
139
  describe '.list' do
88
- let(:json_response) do
140
+ let(:list_project_response) do
89
141
  {
90
142
  "request" => {
91
143
  "application_key" => "app-xxxxxx",
@@ -137,13 +189,13 @@ describe Awesm::Project do
137
189
  }.to_json
138
190
  end
139
191
 
140
- let(:json_error_response) do
192
+ let(:list_project_error_response) do
141
193
  {
142
194
  "request" => {
143
- "action" => "list",
144
- "subscription_key" => "butt",
145
- "application_key" => "tlVC3D",
146
- "controller" => "project"
195
+ "action" => "list",
196
+ "subscription_key" => "butt",
197
+ "application_key" => "tlVC3D",
198
+ "controller" => "project"
147
199
  },
148
200
  "error" => "Invalid subscription key"
149
201
  }.to_json
@@ -151,9 +203,9 @@ describe Awesm::Project do
151
203
 
152
204
  before do
153
205
  stub_request(:post, "http://api.awe.sm/projects/list?subscription_key=sub-xxxxxx&application_key=app-xxxxxx").
154
- to_return(:status => 200, :body => json_response, :headers => { 'Content-Type' => 'application/json;charset=utf-8' })
206
+ to_return(:status => 200, :body => list_project_response, :headers => { 'Content-Type' => 'application/json;charset=utf-8' })
155
207
  stub_request(:post, "http://api.awe.sm/projects/list?subscription_key=invalid&application_key=app-xxxxxx").
156
- to_return(:status => 400, :body => json_error_response, :headers => { 'Content-Type' => 'application/json;charset=utf-8' })
208
+ to_return(:status => 400, :body => list_project_error_response, :headers => { 'Content-Type' => 'application/json;charset=utf-8' })
157
209
  end
158
210
 
159
211
  it 'posts to the awe.sm project list api properly' do
@@ -181,34 +233,9 @@ describe Awesm::Project do
181
233
  end
182
234
 
183
235
  describe '#api_key' do
184
- let(:json_response) do
185
- {
186
- "request" => {
187
- "application_key" => "app-xxxxxx",
188
- "json" => "{\"name\" =>\"TotallyAwesomeProject\"}",
189
- "method" => "new",
190
- "object" => "project",
191
- "subscription_key" => "sub-xxxxxx"
192
- },
193
- "response" => {
194
- "project" => {
195
- "admins" => [],
196
- "api_key" => "6xxxxxxxxx58xx0xxx74xx3x76xx83x6x34xx48x7xxxx55x167037818d65x66x",
197
- "created_at" => "2011-10-25 00:43:49",
198
- "default_domain" => "awe.sm",
199
- "domains" => [],
200
- "name" => "TotallyAwesomeProject",
201
- "sharers" => [],
202
- "updated_at" => "2011-10-25 00:43:49",
203
- "viewers" => []
204
- }
205
- }
206
- }.to_json
207
- end
208
-
209
236
  before do
210
237
  stub_request(:post, "http://api.awe.sm/projects/new?json=%7B%22name%22:%22TotallyAwesomeProject%22%7D&subscription_key=sub-xxxxxx&application_key=app-xxxxxx").
211
- to_return(:status => 200, :body => json_response, :headers => { 'Content-Type' => 'application/json;charset=utf-8' })
238
+ to_return(:status => 200, :body => new_project_response, :headers => { 'Content-Type' => 'application/json;charset=utf-8' })
212
239
  end
213
240
 
214
241
  it 'returns the awe.sm api_key' do
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+
3
+ describe Awesm::Stats do
4
+ describe '.range' do
5
+ let(:basic_response) do
6
+ {
7
+ "end_date" => "2011-10-01T00:00:00Z",
8
+ "filters" => [],
9
+ "group_by" => nil,
10
+ "groups" => [],
11
+ "last_offset" => 0,
12
+ "offset" => 0,
13
+ "page" => nil,
14
+ "per_page" => 10,
15
+ "pivot" => nil,
16
+ "pivot_sort_order" => nil,
17
+ "pivot_sort_type" => nil,
18
+ "sort_order" => nil,
19
+ "sort_type" => nil,
20
+ "start_date" => "2011-09-01T00:00:00Z",
21
+ "total_results" => 0,
22
+ "totals" => {
23
+ "clicks" => 166,
24
+ "clicks_per_share" => 2.5538,
25
+ "shares" => 65
26
+ },
27
+ "with_conversions" => false,
28
+ "with_metadata" => false,
29
+ "with_zeros" => false
30
+ }.to_json
31
+ end
32
+
33
+ let(:invalid_key_response) do
34
+ {
35
+ "error" => 403,
36
+ "error_message" => "Invalid API key specified"
37
+ }.to_json
38
+ end
39
+
40
+ before do
41
+ stub_request(:get, "http://api.awe.sm/stats/range.json?v=3&end_date=2011-10-01&key=5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9&start_date=2011-09-01").
42
+ to_return(:status => 200, :body => basic_response, :headers => { 'Content-Type' => 'application/json;charset=utf-8' })
43
+ stub_request(:get, "http://api.awe.sm/stats/range.json?v=3&end_date=2011-10-01&key=fake_key&start_date=2011-09-01").
44
+ to_return(
45
+ :status => 403,
46
+ :body => invalid_key_response,
47
+ :headers => {
48
+ "x-powered-by" => ["PHP/5.3.2-1ubuntu4.7"],
49
+ "connection"=>["close"],
50
+ "content-type"=>["text/html"],
51
+ "date"=>["Tue, 13 Dec 2011 18:51:20 GMT"],
52
+ "server"=>["Apache/2.2.14 (Ubuntu)"],
53
+ "content-length"=>["57"],
54
+ "vary"=>["Accept-Encoding"]
55
+ })
56
+ end
57
+
58
+ it 'gets the stats range api correctly' do
59
+ stats = Awesm::Stats.range(
60
+ :key => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
61
+ :start_date => '2011-09-01',
62
+ :end_date => '2011-10-01'
63
+ )
64
+
65
+ a_request(:get, "http://api.awe.sm/stats/range.json").
66
+ with(:query => {
67
+ :v => '3',
68
+ :key => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
69
+ :start_date => '2011-09-01',
70
+ :end_date => '2011-10-01'
71
+ }).
72
+ should have_been_made.once
73
+ end
74
+
75
+ it 'gets with an incorrect key' do
76
+ stats = Awesm::Stats.range(
77
+ :key => 'fake_key',
78
+ :start_date => '2011-09-01',
79
+ :end_date => '2011-10-01'
80
+ )
81
+ stats.should == nil
82
+ end
83
+
84
+ it 'returns an instance of Awesm::Stats' do
85
+ stats = Awesm::Stats.range(
86
+ :key => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
87
+ :start_date => '2011-09-01',
88
+ :end_date => '2011-10-01'
89
+ )
90
+ stats.should be_an_instance_of(Awesm::Stats)
91
+ end
92
+ end
93
+ end
@@ -50,7 +50,7 @@ describe Awesm::Url do
50
50
  :user_id_profile_url => 'http://test.com/users/42/',
51
51
  :user_id_username => 'johndoe@test.com'
52
52
  ))
53
- url.should == "http://api.awe.sm/url/share?v=3&url=http://developers.awe.sm/&key=5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9&tool=mKU7uN&channel=twitter&destination=http://twitter.com/intent/tweet?text=This+is+the+coolest+API+evar!%26url=AWESM_URL%26via=awesm&user_id_icon_url=http://test.com/users/42/avatar.png&user_id_profile_url=http://test.com/users/42/&user_id_username=johndoe@test.com&user_id=42"
53
+ url.should == "http://api.awe.sm/url/share?v=3&url=http://developers.awe.sm/&key=5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9&tool=mKU7uN&channel=twitter&destination=http://twitter.com/intent/tweet?text=This+is+the+coolest+API+evar!%26url=AWESM_URL%26via=awesm&user_id=42&user_id_icon_url=http://test.com/users/42/avatar.png&user_id_profile_url=http://test.com/users/42/&user_id_username=johndoe@test.com"
54
54
  end
55
55
 
56
56
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awesm
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 6
10
- version: 0.1.6
9
+ - 7
10
+ version: 0.1.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sathya Sekaran
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-12-08 00:00:00 Z
19
+ date: 2011-12-14 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  prerelease: false
@@ -179,10 +179,12 @@ files:
179
179
  - lib/awesm.rb
180
180
  - lib/awesm/conversion.rb
181
181
  - lib/awesm/project.rb
182
+ - lib/awesm/stats.rb
182
183
  - lib/awesm/url.rb
183
184
  - lib/awesm/version.rb
184
185
  - spec/awesm/conversion_spec.rb
185
186
  - spec/awesm/project_spec.rb
187
+ - spec/awesm/stats_spec.rb
186
188
  - spec/awesm/url_spec.rb
187
189
  - spec/awesm_spec.rb
188
190
  - spec/spec_helper.rb
@@ -222,6 +224,7 @@ summary: Totally awe.sm!
222
224
  test_files:
223
225
  - spec/awesm/conversion_spec.rb
224
226
  - spec/awesm/project_spec.rb
227
+ - spec/awesm/stats_spec.rb
225
228
  - spec/awesm/url_spec.rb
226
229
  - spec/awesm_spec.rb
227
230
  - spec/spec_helper.rb