badgeville 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ * 0.0.7
2
+
3
+ * support pagination and get all pages for rewards and rewards definitions
4
+ * misc bug fixes
5
+
1
6
  * 0.0.6
2
7
 
3
8
  * change set player to use new players/info end point
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- Nitro API
1
+ Badgeville API
2
2
  ============
3
3
 
4
4
  Client for Badgeville API http://rules.badgeville.com/
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
data/badgeville.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{badgeville}
8
- s.version = "0.0.5"
8
+ s.version = "0.0.6"
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-10}
12
+ s.date = %q{2011-11-30}
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"
@@ -45,24 +45,24 @@ Gem::Specification.new do |s|
45
45
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
46
  s.add_runtime_dependency(%q<json>, [">= 0"])
47
47
  s.add_runtime_dependency(%q<rest-client>, [">= 0"])
48
- s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
49
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
50
- s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
48
+ s.add_development_dependency(%q<rspec>, [">= 0"])
49
+ s.add_development_dependency(%q<bundler>, [">= 0"])
50
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
51
51
  s.add_development_dependency(%q<rcov>, [">= 0"])
52
52
  else
53
53
  s.add_dependency(%q<json>, [">= 0"])
54
54
  s.add_dependency(%q<rest-client>, [">= 0"])
55
- s.add_dependency(%q<rspec>, ["~> 2.3.0"])
56
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
57
- s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
55
+ s.add_dependency(%q<rspec>, [">= 0"])
56
+ s.add_dependency(%q<bundler>, [">= 0"])
57
+ s.add_dependency(%q<jeweler>, [">= 0"])
58
58
  s.add_dependency(%q<rcov>, [">= 0"])
59
59
  end
60
60
  else
61
61
  s.add_dependency(%q<json>, [">= 0"])
62
62
  s.add_dependency(%q<rest-client>, [">= 0"])
63
- s.add_dependency(%q<rspec>, ["~> 2.3.0"])
64
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
65
- s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
63
+ s.add_dependency(%q<rspec>, [">= 0"])
64
+ s.add_dependency(%q<bundler>, [">= 0"])
65
+ s.add_dependency(%q<jeweler>, [">= 0"])
66
66
  s.add_dependency(%q<rcov>, [">= 0"])
67
67
  end
68
68
  end
data/lib/badgeville.rb CHANGED
@@ -6,8 +6,8 @@ require 'badgeville/reward'
6
6
 
7
7
  module Badgeville
8
8
  TIMEOUT_SECS = 3
9
- HOST = "sandbox.v2.badgeville.com"
10
- PROTOCOL = "http"
9
+ HOST = "sandbox.v2.badgeville.com"
10
+ PROTOCOL = "http"
11
11
 
12
12
  class BadgevilleError < StandardError
13
13
  attr_accessor :code, :data
@@ -35,6 +35,7 @@ module Badgeville
35
35
  @host = opts['host'] || HOST
36
36
  @protocol = opts['protocol'] || PROTOCOL
37
37
  @user = email
38
+ @per_page = opts['per_page']
38
39
  end
39
40
 
40
41
  def log_activity(activity, opts={})
@@ -58,15 +59,15 @@ module Badgeville
58
59
  end
59
60
 
60
61
  def count_activities(opts={})
61
- response = make_call(:get, :activities, opts)
62
+ response = make_call(:get, :activities, opts.merge(:include_totals => true))
62
63
  response["paging"]["total_entries"].to_i
63
64
  end
64
65
 
65
66
  def reward_definitions
66
67
  unless @reward_definitions
67
- response = make_call(:get, :reward_definitions)
68
- @reward_definitions = response["data"].map do |reward_json|
69
- Reward.new(reward_json)
68
+ pages = all_pages_for(:reward_definitions)
69
+ @reward_definitions = pages.inject([]) do |definitions, page|
70
+ definitions += rewards_from_response(page)
70
71
  end
71
72
  end
72
73
  @reward_definitions
@@ -74,9 +75,9 @@ module Badgeville
74
75
 
75
76
  def get_rewards
76
77
  begin
77
- response = make_call(:get, :rewards)
78
- response["data"].map do |reward_json|
79
- Reward.new(reward_json)
78
+ pages = all_pages_for(:rewards)
79
+ pages.inject([]) do |rewards, page|
80
+ rewards += rewards_from_response(page)
80
81
  end
81
82
  rescue BadgevilleError => e
82
83
  raise e unless e.code == 404
@@ -166,6 +167,9 @@ module Badgeville
166
167
  begin
167
168
  case method
168
169
  when :get
170
+ unless @per_page.nil? || params.has_key?(:per_page)
171
+ params[:per_page] = @per_page
172
+ end
169
173
  response = session[end_point].send(method, :params => params)
170
174
  when :post, :put, :delete
171
175
  response = session[end_point].send(method, to_query(params))
@@ -193,5 +197,33 @@ module Badgeville
193
197
  def to_query params
194
198
  URI.escape(params.map { |k,v| "#{k.to_s}=#{v.to_s}" }.join("&"))
195
199
  end
200
+
201
+ def rewards_from_response(response)
202
+ response["data"].map do |reward_json|
203
+ Reward.new(reward_json)
204
+ end
205
+ end
206
+
207
+ def get_page(action, page, params={})
208
+ make_call(:get, action, params.merge(page: page))
209
+ end
210
+
211
+ def all_pages_for(action, params={})
212
+ pages = []
213
+ current_page = 1
214
+ total_pages = nil
215
+ while total_pages.nil? || current_page <= total_pages
216
+ params[:include_totals] = true unless total_pages
217
+ response = get_page(:reward_definitions, current_page, params)
218
+ pages << response
219
+ if response["paging"]
220
+ current_page = response["paging"]["current_page"].to_i + 1
221
+ total_pages = response["paging"]["total_pages"].to_i if total_pages.nil?
222
+ else
223
+ total_pages = 0
224
+ end
225
+ end
226
+ pages
227
+ end
196
228
  end
197
229
  end
@@ -2,10 +2,12 @@
2
2
  module Badgeville
3
3
  class Reward
4
4
  include Badgeville::Helpers
5
- attr_accessor :name, :hint, :image_url, :active, :earned_at, :id
5
+ attr_accessor :name, :hint, :active, :earned_at, :id, :message
6
6
  attr_accessor :verb, :threshold, :tags, :definition_id
7
7
 
8
- # example:
8
+ attr_writer :image_url
9
+
10
+ # example:
9
11
  # {
10
12
  # "name": "A Way with Words",
11
13
  # "active_start_at": null,
@@ -29,7 +31,8 @@ module Badgeville
29
31
  # "assignable": false,
30
32
  # "allow_duplicates": false,
31
33
  # "site_id": "4e4d5bf5c47eed25a0000e8f",
32
- # "active": true
34
+ # "active": true,
35
+ # "message": "You won the internet!"
33
36
  # }
34
37
  def initialize(json=nil)
35
38
  if json
@@ -42,18 +45,25 @@ module Badgeville
42
45
  end
43
46
  end
44
47
 
48
+ def image_url(format = :original)
49
+ @image_url.sub('original', format.to_s)
50
+ end
51
+
52
+ # <b>DEPRECATED:</b> Please use <tt>image_url('grayscale')</tt> instead.
45
53
  def grayscale_url
46
- @image_url.sub('original.png', 'grayscale.png')
54
+ warn "[DEPRECATION] `grayscale_url` is deprecated. Please use `image_url(:grayscale)` instead."
55
+ image_url(:grayscale)
47
56
  end
48
57
 
49
58
  private
50
59
 
51
60
  def init_from(json)
52
- @name = json["name"]
53
- @active = json["active"]
54
- @hint = json["hint"]
55
- @image_url = json["image_url"]
61
+ ['name', 'active', 'hint', 'image_url', 'message'].each do |key|
62
+ send("#{key}=", json[key])
63
+ end
64
+
56
65
  @id = @definition_id = json["_id"]
66
+
57
67
  if json["data"] && json["type"] == "achievement"
58
68
  @verb = json["data"]["verb"]
59
69
  @threshold = json["data"]["threshold"].to_i
@@ -86,13 +86,13 @@ describe Badgeville do
86
86
  before do
87
87
  site = "example.com"
88
88
  base_url = "http://#{Badgeville::HOST}/api/berlin/api_key/activities.json"
89
- total_url = base_url + "?site=#{site}&user=#{@user}"
89
+ total_url = base_url + "?include_totals=true&site=#{site}&user=#{@user}"
90
90
  @total_count = 2
91
91
  total_response = {"data" => [],
92
92
  "paging" => {"total_entries" => @total_count}}
93
93
  stub_http_request(:get, total_url).to_return(:body => total_response.to_json)
94
94
  @verb_count = 1
95
- verb_url = base_url + "?site=#{site}&user=#{@user}&verb=verb"
95
+ verb_url = base_url + "?include_totals=true&site=#{site}&user=#{@user}&verb=verb"
96
96
  verb_response = {"data" => [],
97
97
  "paging" => {"total_entries" => @verb_count}}
98
98
  stub_http_request(:get, verb_url).
@@ -111,15 +111,32 @@ describe Badgeville do
111
111
  describe "#reward_definitions" do
112
112
  before do
113
113
  @url = /http:\/\/#{Badgeville::HOST}.*reward_definitions.json.*user=#{@user}.*/
114
- mock_rewards = {"data" => [{"name" => "Big Bang"}]}
115
- stub_http_request(:get, @url).to_return(:body => mock_rewards.to_json)
114
+ rewards_on_first_page = {
115
+ "data" => [{"name" => "Big Bang"}],
116
+ "paging" => {"current_page" => 1,"total_pages" => 2}
117
+ }
118
+ rewards_on_second_page = {
119
+ "data" => [{"name" => "Small Bang"}],
120
+ "paging" => {"current_page" => 2,"total_pages" => 2}
121
+ }
122
+
123
+ stub_http_request(:get, @url).to_return(
124
+ {:body => rewards_on_first_page.to_json},
125
+ {:body => rewards_on_second_page.to_json}
126
+ )
127
+
116
128
  @rewards = @badgeville.reward_definitions
117
129
  end
118
130
 
119
- it "should return an array of rewards" do
131
+ it "should return an array of all rewards" do
120
132
  @rewards.class.should be(Array)
133
+ @rewards.count.should == 2
134
+
121
135
  @rewards.first.class.should be(Badgeville::Reward)
122
136
  @rewards.first.name.should == "Big Bang"
137
+
138
+ @rewards[1].class.should be(Badgeville::Reward)
139
+ @rewards[1].name.should == "Small Bang"
123
140
  end
124
141
  end
125
142
 
data/spec/reward_spec.rb CHANGED
@@ -12,7 +12,7 @@ describe Badgeville::Reward do
12
12
  "threshold": 2
13
13
  },
14
14
  "created_at": "2011-08-18T22:55:03-07:00",
15
- "image_url": "http://s3.amazonaws.com/badgeville-production-reward-definitions/images/someid/original.png?1313733302",
15
+ "image_url": "http://s3.amazon.com/original.png?1",
16
16
  "components": "[{\"command\":\"count\",\"comparator\":{\"$gte\":2},\"config\":{},\"where\":{\"verb\":\"commented\",\"player_id\":\"%player_id\"}}]",
17
17
  "reward_template": {
18
18
  "message": ""
@@ -26,7 +26,8 @@ describe Badgeville::Reward do
26
26
  "assignable": false,
27
27
  "allow_duplicates": false,
28
28
  "site_id": "4e4d5bf5c47eed25a0000e8f",
29
- "active": true
29
+ "active": true,
30
+ "message": "You won the internet!"
30
31
  }')
31
32
  end
32
33
 
@@ -37,15 +38,23 @@ describe Badgeville::Reward do
37
38
 
38
39
  it "initialize based on json" do
39
40
  @reward.name.should == "A Way with Words"
40
- @reward.image_url.should =~ /original.png/
41
41
  @reward.active.should be_true
42
42
  @reward.id.should == "4e4dfab6c47eed727b005c38"
43
43
  @reward.tags.should == []
44
44
  @reward.definition_id.should == '4e4dfab6c47eed727b005c38'
45
+ @reward.message.should == 'You won the internet!'
45
46
  end
46
47
 
47
- it "has an grayscale image" do
48
- @reward.grayscale_url.should == @reward.image_url.sub('original','grayscale')
48
+ describe "image_url" do
49
+ it "returns the original url by default" do
50
+ @reward.image_url.should == 'http://s3.amazon.com/original.png?1'
51
+ end
52
+
53
+ it "allows to request specific formats" do
54
+ [:original, :large, :medium, :grayscale, :grayscale_small].each do |format|
55
+ @reward.image_url(format).should == "http://s3.amazon.com/#{format}.png?1"
56
+ end
57
+ end
49
58
  end
50
59
 
51
60
  context "has one tag" do
@@ -82,6 +91,7 @@ describe Badgeville::Reward do
82
91
  @reward.earned_at.iso8601.should == Time.parse(@parsed_json["created_at"]).iso8601
83
92
  @reward.id.should == "new_id"
84
93
  @reward.definition_id.should == '4e4dfab6c47eed727b005c38'
94
+ @reward.message.should == 'You won the internet!'
85
95
  end
86
96
  end
87
97
 
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.6
4
+ version: 0.0.7
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-11-15 00:00:00.000000000 -08:00
12
+ date: 2011-11-30 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: &2152997400 !ruby/object:Gem::Requirement
17
+ requirement: &2160373920 !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: *2152997400
25
+ version_requirements: *2160373920
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rest-client
28
- requirement: &2152996920 !ruby/object:Gem::Requirement
28
+ requirement: &2160372140 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *2152996920
36
+ version_requirements: *2160372140
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rspec
39
- requirement: &2152996440 !ruby/object:Gem::Requirement
39
+ requirement: &2160370280 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *2152996440
47
+ version_requirements: *2160370280
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: bundler
50
- requirement: &2152995960 !ruby/object:Gem::Requirement
50
+ requirement: &2160368540 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2152995960
58
+ version_requirements: *2160368540
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: jeweler
61
- requirement: &2152995480 !ruby/object:Gem::Requirement
61
+ requirement: &2160363980 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *2152995480
69
+ version_requirements: *2160363980
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rcov
72
- requirement: &2152995000 !ruby/object:Gem::Requirement
72
+ requirement: &2160362220 !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: *2152995000
80
+ version_requirements: *2160362220
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: 1071652726597665833
121
+ hash: 4192287598569772720
122
122
  required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  none: false
124
124
  requirements: