badgeville 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ * 0.0.6
2
+
3
+ * change set player to use new players/info end point
4
+ * add delete method for earned rewards
5
+ * add definition_id to reward
6
+
1
7
  * 0.0.5
2
8
 
3
9
  * set default timeout to 3s
data/Gemfile CHANGED
@@ -8,9 +8,9 @@ gem 'rest-client'
8
8
  # Add dependencies to develop your gem here.
9
9
  # Include everything needed to run rake, tests, features, etc.
10
10
  group :development do
11
- gem "rspec", "~> 2.3.0"
12
- gem "bundler", "~> 1.0.0"
13
- gem "jeweler", "~> 1.6.4"
11
+ gem "rspec"
12
+ gem "bundler"
13
+ gem "jeweler"
14
14
  gem "rcov", ">= 0"
15
15
  end
16
16
 
data/Gemfile.lock CHANGED
@@ -2,28 +2,28 @@ GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
4
  addressable (2.2.6)
5
- crack (0.1.8)
6
- diff-lcs (1.1.2)
5
+ crack (0.3.1)
6
+ diff-lcs (1.1.3)
7
7
  git (1.2.5)
8
8
  jeweler (1.6.4)
9
9
  bundler (~> 1.0)
10
10
  git (>= 1.2.5)
11
11
  rake
12
- json (1.5.3)
13
- mime-types (1.16)
14
- rake (0.8.7)
15
- rcov (0.9.9)
16
- rest-client (1.6.1)
12
+ json (1.6.1)
13
+ mime-types (1.17.2)
14
+ rake (0.9.2.2)
15
+ rcov (0.9.11)
16
+ rest-client (1.6.7)
17
17
  mime-types (>= 1.16)
18
- rspec (2.3.0)
19
- rspec-core (~> 2.3.0)
20
- rspec-expectations (~> 2.3.0)
21
- rspec-mocks (~> 2.3.0)
22
- rspec-core (2.3.1)
23
- rspec-expectations (2.3.0)
18
+ rspec (2.7.0)
19
+ rspec-core (~> 2.7.0)
20
+ rspec-expectations (~> 2.7.0)
21
+ rspec-mocks (~> 2.7.0)
22
+ rspec-core (2.7.1)
23
+ rspec-expectations (2.7.0)
24
24
  diff-lcs (~> 1.1.2)
25
- rspec-mocks (2.3.0)
26
- webmock (1.6.4)
25
+ rspec-mocks (2.7.0)
26
+ webmock (1.7.7)
27
27
  addressable (~> 2.2, > 2.2.5)
28
28
  crack (>= 0.1.7)
29
29
 
@@ -31,10 +31,10 @@ PLATFORMS
31
31
  ruby
32
32
 
33
33
  DEPENDENCIES
34
- bundler (~> 1.0.0)
35
- jeweler (~> 1.6.4)
34
+ bundler
35
+ jeweler
36
36
  json
37
37
  rcov
38
38
  rest-client
39
- rspec (~> 2.3.0)
39
+ rspec
40
40
  webmock
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
data/badgeville.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Gilad Buchman"]
12
- s.date = %q{2011-10-07}
12
+ s.date = %q{2011-10-10}
13
13
  s.description = %q{Client for Badgeville's Core API v2. http://rules.badgeville.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.md"
@@ -20,4 +20,6 @@ pp badgeville.count_activities :verb => "junk"
20
20
 
21
21
  pp badgeville.get_rewards #earned for user
22
22
 
23
- badgeville.award "Best User Ever"
23
+ #award and delete rewards
24
+ reward = badgeville.award "The First Step"
25
+ badgeville.delete reward
@@ -3,7 +3,7 @@ module Badgeville
3
3
  class Reward
4
4
  include Badgeville::Helpers
5
5
  attr_accessor :name, :hint, :image_url, :active, :earned_at, :id
6
- attr_accessor :verb, :threshold, :tags
6
+ attr_accessor :verb, :threshold, :tags, :definition_id
7
7
 
8
8
  # example:
9
9
  # {
@@ -37,6 +37,7 @@ module Badgeville
37
37
  reward_definition ? init_from(reward_definition) : init_from(json)
38
38
  if json.has_key?('user_id') # it's an earned reward for a specific user
39
39
  @earned_at = DateTime.parse(json["created_at"]).to_time
40
+ @id = json["id"]
40
41
  end
41
42
  end
42
43
  end
@@ -52,7 +53,7 @@ module Badgeville
52
53
  @active = json["active"]
53
54
  @hint = json["hint"]
54
55
  @image_url = json["image_url"]
55
- @id = json["_id"]
56
+ @id = @definition_id = json["_id"]
56
57
  if json["data"] && json["type"] == "achievement"
57
58
  @verb = json["data"]["verb"]
58
59
  @threshold = json["data"]["threshold"].to_i
data/lib/badgeville.rb CHANGED
@@ -10,16 +10,16 @@ module Badgeville
10
10
  PROTOCOL = "http"
11
11
 
12
12
  class BadgevilleError < StandardError
13
- attr_accessor :code
13
+ attr_accessor :code, :data
14
14
 
15
- def initialize (err_code=nil, error_msg="")
16
- super error_msg
17
- @message = error_msg
15
+ def initialize (err_code=nil, error_data="")
16
+ super error_data.to_s
17
+ @data = error_data
18
18
  @code = err_code
19
19
  end
20
20
 
21
21
  def to_s
22
- "ERROR #{@code} : #{@message}"
22
+ "ERROR #{@code} : #{@data}"
23
23
  end
24
24
  end
25
25
 
@@ -96,11 +96,24 @@ module Badgeville
96
96
  Reward.new(make_call(:post, :rewards, params))
97
97
  end
98
98
 
99
+ def delete reward
100
+ if reward.respond_to?(:earned_at) && reward.earned_at
101
+ end_point = "rewards/#{reward.id}.json"
102
+ begin
103
+ !!session[end_point].delete
104
+ rescue => e
105
+ raise BadgevilleError.new(e.http_code, e.message)
106
+ end
107
+ else
108
+ raise BadgevilleError.new(nil, "can only remove earned rewards. a #{reward.to_json} was given")
109
+ end
110
+ end
111
+
99
112
  def set_player
100
- end_point = "#{@site}/players/info.json"
113
+ end_point = "/players/info.json"
101
114
  begin
102
115
  response = session[end_point].get(:params =>
103
- {:user => @user, :site => @site})
116
+ {:email => @user, :site => @site})
104
117
  data = response.body
105
118
  json = JSON.parse(data)
106
119
  json = json["data"]
@@ -116,10 +129,6 @@ module Badgeville
116
129
  end
117
130
  end
118
131
 
119
- def timeout
120
- @timeout
121
- end
122
-
123
132
  def site_id
124
133
  unless @site_id
125
134
  set_player
@@ -143,7 +152,8 @@ module Badgeville
143
152
  def session
144
153
  if @session.nil?
145
154
  base_url = "#{@protocol}://#{@host}/api/berlin/#{@private_key}"
146
- @session = RestClient::Resource.new base_url, :timeout => @timeout
155
+ @session = RestClient::Resource.new base_url,
156
+ :timeout => @timeout, :open_timeout => @timeout
147
157
  end
148
158
  @session
149
159
  end
@@ -166,9 +176,13 @@ module Badgeville
166
176
  if e.respond_to? :response
167
177
  begin
168
178
  data = JSON.parse(e.response)
179
+ data.merge!(:params => params,
180
+ :end_point => end_point,
181
+ :method => method)
169
182
  raise BadgevilleError.new(e.http_code, data)
170
183
  rescue TypeError, JSON::ParserError
171
- raise BadgevilleError.new(e.http_code, e.message)
184
+ msg = "#{e.message} on #{method} #{end_point}: #{params}"
185
+ raise BadgevilleError.new(e.http_code, msg)
172
186
  end
173
187
  else
174
188
  raise e
@@ -36,7 +36,7 @@ describe Badgeville do
36
36
 
37
37
  it "parses response from activity record api call" do
38
38
  @activity.verb.should == "commented"
39
- @activity.created_at.iso8601.should == @result["created_at"]
39
+ @activity.created_at.iso8601.should == Time.parse(@result["created_at"]).iso8601
40
40
  @activity.points.should == 0
41
41
  @activity.rewards.should be_empty
42
42
  @activity.meta.should be_empty
@@ -123,9 +123,43 @@ describe Badgeville do
123
123
  end
124
124
  end
125
125
 
126
+ describe "#delete" do
127
+ before do
128
+ @mock_reward = Badgeville::Reward.new
129
+ @mock_reward.id = 5
130
+ @mock_reward.earned_at = Time.now
131
+ @url = /http:\/\/#{Badgeville::HOST}.*rewards\/5.json/
132
+ stub_http_request(:delete, @url).to_return(:status => 200)
133
+ end
134
+
135
+ it "succeed for an earned reward" do
136
+ @badgeville.delete(@mock_reward).should be_true
137
+ end
138
+
139
+ it "fails for an un-earned reward (reward definition)" do
140
+ @mock_reward.earned_at = nil
141
+ lambda {
142
+ @badgeville.delete(@mock_reward)
143
+ }.should raise_error(Badgeville::BadgevilleError)
144
+ end
145
+
146
+ it "fails for non reward object" do
147
+ lambda {
148
+ @badgeville.delete(nil)
149
+ }.should raise_error(Badgeville::BadgevilleError)
150
+ end
151
+
152
+ it "handles response error codes" do
153
+ stub_http_request(:delete, @url).to_return(:status => 500)
154
+ lambda {
155
+ @badgeville.delete(@mock_reward)
156
+ }.should raise_error(Badgeville::BadgevilleError)
157
+ end
158
+ end
159
+
126
160
  describe "#set_player" do
127
161
  before do
128
- @url = /http:\/\/#{Badgeville::HOST}.*example.com\/players\/info\.json.*user=#{@user}.*/
162
+ @url = /http:\/\/#{Badgeville::HOST}.*\/players\/info\.json.*email=#{@user}.*/
129
163
  stub_http_request(:get, @url).to_return(:body => {"data" => {"id" => "1", "site_id" => "site"}}.to_json)
130
164
  end
131
165
 
data/spec/reward_spec.rb CHANGED
@@ -17,7 +17,7 @@ describe Badgeville::Reward do
17
17
  "reward_template": {
18
18
  "message": ""
19
19
  },
20
- "_id": "4e4dfab6c47eed727b005c38",
20
+ "_id": "4e4dfab6c47eed727b005c38",
21
21
  "tags": null,
22
22
  "id": "4e4dfab6c47eed727b005c38",
23
23
  "active_end_at": null,
@@ -41,6 +41,7 @@ describe Badgeville::Reward do
41
41
  @reward.active.should be_true
42
42
  @reward.id.should == "4e4dfab6c47eed727b005c38"
43
43
  @reward.tags.should == []
44
+ @reward.definition_id.should == '4e4dfab6c47eed727b005c38'
44
45
  end
45
46
 
46
47
  it "has an grayscale image" do
@@ -68,7 +69,7 @@ describe Badgeville::Reward do
68
69
 
69
70
  context "when json is a reward earned by user" do
70
71
  before do
71
- @parsed_json = {"user_id" => "1",
72
+ @parsed_json = {"user_id" => "1", "id" => "new_id",
72
73
  "created_at" => "2011-08-18T22:55:03-07:00",
73
74
  "definition" => @parsed_json}
74
75
  @reward = Badgeville::Reward.new(@parsed_json)
@@ -78,8 +79,9 @@ describe Badgeville::Reward do
78
79
  @reward.name.should == "A Way with Words"
79
80
  @reward.image_url.should =~ /original.png/
80
81
  @reward.active.should be_true
81
- @reward.earned_at.iso8601.should == @parsed_json["created_at"]
82
- @reward.id.should == "4e4dfab6c47eed727b005c38"
82
+ @reward.earned_at.iso8601.should == Time.parse(@parsed_json["created_at"]).iso8601
83
+ @reward.id.should == "new_id"
84
+ @reward.definition_id.should == '4e4dfab6c47eed727b005c38'
83
85
  end
84
86
  end
85
87
 
@@ -88,7 +90,7 @@ describe Badgeville::Reward do
88
90
  @reward = Badgeville::Reward.new(@parsed_json)
89
91
  end
90
92
 
91
- it "has verb adn threshold accessors" do
93
+ it "has verb and threshold accessors" do
92
94
  @reward.verb.should == "commented"
93
95
  @reward.threshold.should == 2
94
96
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: badgeville
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-10 00:00:00.000000000 -07:00
12
+ date: 2011-11-15 00:00:00.000000000 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json
17
- requirement: &2160449300 !ruby/object:Gem::Requirement
17
+ requirement: &2152997400 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2160449300
25
+ version_requirements: *2152997400
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rest-client
28
- requirement: &2160448300 !ruby/object:Gem::Requirement
28
+ requirement: &2152996920 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,43 +33,43 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *2160448300
36
+ version_requirements: *2152996920
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rspec
39
- requirement: &2160447640 !ruby/object:Gem::Requirement
39
+ requirement: &2152996440 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
- - - ~>
42
+ - - ! '>='
43
43
  - !ruby/object:Gem::Version
44
- version: 2.3.0
44
+ version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *2160447640
47
+ version_requirements: *2152996440
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: bundler
50
- requirement: &2160447000 !ruby/object:Gem::Requirement
50
+ requirement: &2152995960 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
- - - ~>
53
+ - - ! '>='
54
54
  - !ruby/object:Gem::Version
55
- version: 1.0.0
55
+ version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2160447000
58
+ version_requirements: *2152995960
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: jeweler
61
- requirement: &2160446280 !ruby/object:Gem::Requirement
61
+ requirement: &2152995480 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
- - - ~>
64
+ - - ! '>='
65
65
  - !ruby/object:Gem::Version
66
- version: 1.6.4
66
+ version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *2160446280
69
+ version_requirements: *2152995480
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rcov
72
- requirement: &2160445520 !ruby/object:Gem::Requirement
72
+ requirement: &2152995000 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,7 +77,7 @@ dependencies:
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *2160445520
80
+ version_requirements: *2152995000
81
81
  description: Client for Badgeville's Core API v2. http://rules.badgeville.com
82
82
  email:
83
83
  executables: []
@@ -118,7 +118,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
118
  version: '0'
119
119
  segments:
120
120
  - 0
121
- hash: 832478503767646954
121
+ hash: 1071652726597665833
122
122
  required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  none: false
124
124
  requirements: