badgeville 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,5 +1,12 @@
1
+ * 0.0.4
2
+
3
+ * count activities
4
+ * better error handling
5
+ * add tags to rewards
6
+
7
+
1
8
  * 0.0.3
2
9
 
3
10
  * Activity.rewards includes reward objects not reward definitions
4
11
  * Reward has verb and threshold data for achievments type rewards
5
- * Raise non json errors
12
+ * Raise non json errors
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/badgeville.gemspec CHANGED
@@ -5,17 +5,18 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{badgeville}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.4"
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-09-06}
12
+ s.date = %q{2011-09-18}
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"
16
16
  ]
17
17
  s.files = [
18
18
  ".document",
19
+ "CHANGELOG",
19
20
  "Gemfile",
20
21
  "Gemfile.lock",
21
22
  "README.md",
@@ -26,6 +27,7 @@ Gem::Specification.new do |s|
26
27
  "keys.json.example",
27
28
  "lib/badgeville.rb",
28
29
  "lib/badgeville/activity.rb",
30
+ "lib/badgeville/helpers.rb",
29
31
  "lib/badgeville/reward.rb",
30
32
  "spec/badgeville_spec.rb",
31
33
  "spec/reward_spec.rb",
@@ -12,7 +12,12 @@ pp badgeville.log_activity "commented"
12
12
 
13
13
  pp badgeville.log_activity "join_team", :team => "myteam"
14
14
 
15
+ #check histoy for user
15
16
  pp badgeville.get_activities
17
+ pp badgeville.count_activities
18
+ pp badgeville.count_activities :verb => "commented"
19
+ pp badgeville.count_activities :verb => "junk"
20
+
16
21
  pp badgeville.get_rewards #earned for user
17
22
 
18
23
  badgeville.award "Best User Ever"
data/lib/badgeville.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'json'
2
2
  require 'rest_client'
3
+ require 'badgeville/helpers'
3
4
  require 'badgeville/activity'
4
5
  require 'badgeville/reward'
5
6
 
@@ -11,7 +12,8 @@ module Badgeville
11
12
  class BadgevilleError < StandardError
12
13
  attr_accessor :code
13
14
 
14
- def initialize (err_code=nil)
15
+ def initialize (err_code=nil, error_msg="")
16
+ super error_msg
15
17
  @code = err_code
16
18
  end
17
19
  end
@@ -42,30 +44,37 @@ module Badgeville
42
44
  Activity.new(response)
43
45
  end
44
46
 
45
- def get_activities
46
- response = make_call(:get, :activities)
47
+ def get_activities(opts={})
48
+ response = make_call(:get, :activities, opts)
47
49
  response["data"].map do |activity_json|
48
50
  Activity.new(activity_json)
49
51
  end
50
52
  end
51
53
 
54
+ def count_activities(opts={})
55
+ response = make_call(:get, :activities, opts)
56
+ response["paging"]["total_entries"].to_i
57
+ end
58
+
52
59
  def reward_definitions
53
60
  unless @reward_definitions
54
61
  response = make_call(:get, :reward_definitions)
55
- @reward_definitions = response["data"].inject([]) do
56
- |rewards, reward_json|
57
- rewards<< Reward.new(reward_json)
58
- rewards
62
+ @reward_definitions = response["data"].map do |reward_json|
63
+ Reward.new(reward_json)
59
64
  end
60
65
  end
61
66
  @reward_definitions
62
67
  end
63
68
 
64
69
  def get_rewards
65
- response = make_call(:get, :rewards)
66
- response["data"].inject([]) do |rewards, reward_json|
67
- rewards<< Reward.new(reward_json)
68
- rewards
70
+ begin
71
+ response = make_call(:get, :rewards)
72
+ response["data"].map do |reward_json|
73
+ Reward.new(reward_json)
74
+ end
75
+ rescue BadgevilleError => e
76
+ raise e unless e.code == 404
77
+ []
69
78
  end
70
79
  end
71
80
 
@@ -78,7 +87,7 @@ module Badgeville
78
87
  'reward[site_id]' => site_id,
79
88
  'reward[definition_id]' => reward.id,
80
89
  }
81
- make_call(:post, :rewards, params)
90
+ Reward.new(make_call(:post, :rewards, params))
82
91
  end
83
92
 
84
93
  def set_player
@@ -120,10 +129,6 @@ module Badgeville
120
129
  obj.is_a?(Array) || obj.is_a?(Hash)
121
130
  end
122
131
 
123
- def ensure_array(items)
124
- items.is_a?(Array) ? items : [items]
125
- end
126
-
127
132
  def session
128
133
  if @session.nil?
129
134
  base_url = "#{@protocol}://#{@host}/api/berlin/#{@private_key}"
@@ -150,9 +155,9 @@ module Badgeville
150
155
  if e.respond_to? :response
151
156
  begin
152
157
  data = JSON.parse(e.response)
153
- raise BadgevilleError.new(e.code, data["error"])
158
+ raise BadgevilleError.new(e.http_code, data["error"])
154
159
  rescue TypeError
155
- raise e
160
+ raise BadgevilleError.new(e.http_code, data)
156
161
  end
157
162
  else
158
163
  raise e
@@ -0,0 +1,7 @@
1
+ module Badgeville
2
+ module Helpers
3
+ def ensure_array(items)
4
+ items.is_a?(Array) ? items : [items]
5
+ end
6
+ end
7
+ end
@@ -1,7 +1,9 @@
1
+
1
2
  module Badgeville
2
3
  class Reward
4
+ include Badgeville::Helpers
3
5
  attr_accessor :name, :hint, :image_url, :active, :earned_at, :id
4
- attr_accessor :verb, :threshold
6
+ attr_accessor :verb, :threshold, :tags
5
7
 
6
8
  # example:
7
9
  # {
@@ -55,6 +57,10 @@ module Badgeville
55
57
  @verb = json["data"]["verb"]
56
58
  @threshold = json["data"]["threshold"].to_i
57
59
  end
60
+ @tags = []
61
+ if json["tags"]
62
+ @tags = json["tags"].split(',').map(&:strip)
63
+ end
58
64
  end
59
65
  end
60
66
  end
@@ -82,6 +82,32 @@ describe Badgeville do
82
82
  end
83
83
  end
84
84
 
85
+ describe "#count_activities" do
86
+ before do
87
+ site = "example.com"
88
+ base_url = "http://#{Badgeville::HOST}/api/berlin/api_key/activities.json"
89
+ total_url = base_url + "?site=#{site}&user=#{@user}"
90
+ @total_count = 2
91
+ total_response = {"data" => [],
92
+ "paging" => {"total_entries" => @total_count}}
93
+ stub_http_request(:get, total_url).to_return(:body => total_response.to_json)
94
+ @verb_count = 1
95
+ verb_url = base_url + "?site=#{site}&user=#{@user}&verb=verb"
96
+ verb_response = {"data" => [],
97
+ "paging" => {"total_entries" => @verb_count}}
98
+ stub_http_request(:get, verb_url).
99
+ to_return(:body => verb_response.to_json)
100
+ end
101
+
102
+ it "returns total count when a verb is not specified" do
103
+ @badgeville.count_activities.should == @total_count
104
+ end
105
+
106
+ it "returns num of activities per verb when specified" do
107
+ @badgeville.count_activities(:verb => "verb").should == @verb_count
108
+ end
109
+ end
110
+
85
111
  describe "#reward_definitions" do
86
112
  before do
87
113
  @url = /http:\/\/#{Badgeville::HOST}.*reward_definitions.json.*user=#{@user}.*/
@@ -93,6 +119,7 @@ describe Badgeville do
93
119
  it "should return an array of rewards" do
94
120
  @rewards.class.should be(Array)
95
121
  @rewards.first.class.should be(Badgeville::Reward)
122
+ @rewards.first.name.should == "Big Bang"
96
123
  end
97
124
  end
98
125
 
data/spec/reward_spec.rb CHANGED
@@ -40,11 +40,30 @@ describe Badgeville::Reward do
40
40
  @reward.image_url.should =~ /original.png/
41
41
  @reward.active.should be_true
42
42
  @reward.id.should == "4e4dfab6c47eed727b005c38"
43
+ @reward.tags.should == []
43
44
  end
44
45
 
45
46
  it "has an grayscale image" do
46
47
  @reward.grayscale_url.should == @reward.image_url.sub('original','grayscale')
47
48
  end
49
+
50
+ context "has one tag" do
51
+ before do
52
+ @parsed_json["tags"] = "tag"
53
+ @reward = Badgeville::Reward.new(@parsed_json)
54
+ end
55
+
56
+ it {@reward.tags.should == ["tag"]}
57
+ end
58
+
59
+ context "has multiple tags" do
60
+ before do
61
+ @parsed_json["tags"] = "tag1, tag2"
62
+ @reward = Badgeville::Reward.new(@parsed_json)
63
+ end
64
+
65
+ it {@reward.tags.should == ["tag1", "tag2"]}
66
+ end
48
67
  end
49
68
 
50
69
  context "when json is a reward earned by user" do
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.3
4
+ version: 0.0.4
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-09-12 00:00:00.000000000 -07:00
12
+ date: 2011-09-18 00:00:00.000000000 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json
17
- requirement: &2160633160 !ruby/object:Gem::Requirement
17
+ requirement: &2157019100 !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: *2160633160
25
+ version_requirements: *2157019100
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rest-client
28
- requirement: &2160631280 !ruby/object:Gem::Requirement
28
+ requirement: &2157018180 !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: *2160631280
36
+ version_requirements: *2157018180
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rspec
39
- requirement: &2160629440 !ruby/object:Gem::Requirement
39
+ requirement: &2157017140 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 2.3.0
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *2160629440
47
+ version_requirements: *2157017140
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: bundler
50
- requirement: &2160627640 !ruby/object:Gem::Requirement
50
+ requirement: &2157016440 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 1.0.0
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2160627640
58
+ version_requirements: *2157016440
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: jeweler
61
- requirement: &2160625140 !ruby/object:Gem::Requirement
61
+ requirement: &2157015620 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ~>
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 1.6.4
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *2160625140
69
+ version_requirements: *2157015620
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rcov
72
- requirement: &2160622960 !ruby/object:Gem::Requirement
72
+ requirement: &2157014620 !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: *2160622960
80
+ version_requirements: *2157014620
81
81
  description: Client for Badgeville's Core API v2. http://rules.badgeville.com
82
82
  email:
83
83
  executables: []
@@ -97,6 +97,7 @@ files:
97
97
  - keys.json.example
98
98
  - lib/badgeville.rb
99
99
  - lib/badgeville/activity.rb
100
+ - lib/badgeville/helpers.rb
100
101
  - lib/badgeville/reward.rb
101
102
  - spec/badgeville_spec.rb
102
103
  - spec/reward_spec.rb
@@ -117,7 +118,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
118
  version: '0'
118
119
  segments:
119
120
  - 0
120
- hash: -2793547173798113775
121
+ hash: -1038648532873041495
121
122
  required_rubygems_version: !ruby/object:Gem::Requirement
122
123
  none: false
123
124
  requirements: