gamifier 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,8 +5,8 @@ Gamifier.set :uri => "http://sandbox.v2.badgeville.com/api/berlin/", :key => ENV
5
5
  Gamifier.logger.level = Logger::DEBUG
6
6
 
7
7
  network = Gamifier.dsl do
8
- site 'dev2.domain-test.crohr.me' do |site|
9
- set :url, 'dev2.domain-test.crohr.me'
8
+ site 'dev.domain-test.crohr.me' do |site|
9
+ set :url, 'dev.domain-test.crohr.me'
10
10
 
11
11
  unit :karma do
12
12
  set :label, 'Karma'
@@ -58,7 +58,6 @@ network = Gamifier.dsl do
58
58
  set :bucket_drain_rate, bucket_max_capacity/1.5
59
59
 
60
60
  set :hide_in_widgets, false
61
- set :icon, "http://s3.amazonaws.com/icons/icon1.png"
62
61
  end
63
62
 
64
63
  # behavior 'comment_databases' do
@@ -174,6 +173,7 @@ network = Gamifier.dsl do
174
173
  # # the first player who performs at least twice the 'new_question' behaviour
175
174
  # {:command => "count", :comparator => {"$gt" => 2}, :where => {:verb => 'new_question', :site_id => "%site_id"}}
176
175
  # ]
176
+ # set :conjunction, "and"
177
177
  # set :adjustment, {
178
178
  # :points => 5
179
179
  # }
@@ -15,9 +15,9 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Gamifier::VERSION
17
17
 
18
- gem.add_dependency "faraday", "~> 0.4.1"
18
+ gem.add_dependency "faraday", "~> 0.8"
19
19
  gem.add_dependency "json"
20
- gem.add_dependency "rack"
20
+ gem.add_dependency "faraday_middleware", "~> 0.8"
21
21
 
22
22
  gem.add_development_dependency('rspec', '~> 2')
23
23
  gem.add_development_dependency('webmock')
@@ -22,5 +22,14 @@ module Gamifier
22
22
  instance.engine = engine
23
23
  instance
24
24
  end
25
+
26
+ def reset!(params = {})
27
+ unless params.has_key?(:site)
28
+ raise ArgumentError, "A :site parameter is required. Pass nil if you really want to delete all the objects of this kind, on all the sites."
29
+ end
30
+ all(params) do |entry|
31
+ entry.destroy
32
+ end
33
+ end
25
34
  end
26
35
  end
@@ -1,7 +1,6 @@
1
1
  require 'faraday'
2
+ require 'faraday_middleware'
2
3
  require 'gamifier/collection'
3
- require 'json'
4
- require 'rack/utils'
5
4
 
6
5
  module Gamifier
7
6
  class Engine
@@ -13,8 +12,11 @@ module Gamifier
13
12
  def initialize(opts = {})
14
13
  opts.each{|k,v| config[k.to_sym] = v}
15
14
  raise ArgumentError, "Please configure a :uri and :key first." unless ok?
16
- @connection = Faraday::Connection.new(:url => uri_to) do |builder|
15
+ @connection = Faraday.new(:url => uri_to) do |builder|
16
+ builder.use Faraday::Request::UrlEncoded # convert request params as "www-form-urlencoded"
17
+ builder.use Faraday::Response::Logger, Gamifier.logger
17
18
  builder.use Faraday::Adapter::NetHttp # make http requests with Net::HTTP
19
+ builder.use Faraday::Response::ParseJson, :content_type => /\bjson$/
18
20
  end
19
21
  end
20
22
 
@@ -39,28 +41,26 @@ module Gamifier
39
41
  (opts[:head] || {}).each do |k,v|
40
42
  req.headers[k] = v
41
43
  end
42
- req.headers['Content-Type'] ||= 'application/x-www-form-urlencoded'
43
- req.body = self.class.encode_www_form(opts[:body]) unless opts[:body].nil?
44
+ req.body = opts[:body] unless opts[:body].nil?
44
45
  Gamifier.logger.debug "#{method.to_s.upcase} #{req.inspect}"
45
46
  end
46
47
  Gamifier.logger.debug "#{res.inspect}"
47
- body = JSON.parse(res.body) rescue res.body
48
48
  case res.status
49
49
  when 204
50
50
  true
51
51
  when 200...300
52
- body
52
+ res.body
53
53
  when 404
54
54
  nil
55
55
  when 422
56
56
  # Badgeville returns 422 when an entry already exists or is not valid
57
57
  [:get, :head].include?(method) ? nil : false
58
58
  when 400...500
59
- raise HTTPClientError, body
59
+ raise HTTPClientError, res.body
60
60
  when 500...600
61
- raise HTTPServerError, body
61
+ raise HTTPServerError, res.body
62
62
  else
63
- raise HTTPError, body
63
+ raise HTTPError, res.body
64
64
  end
65
65
  end
66
66
 
@@ -76,9 +76,5 @@ module Gamifier
76
76
  Collection.new(self, klass)
77
77
  end
78
78
  end
79
-
80
- def self.encode_www_form(enum)
81
- Rack::Utils.build_nested_query(enum)
82
- end
83
79
  end
84
- end
80
+ end
@@ -99,7 +99,7 @@ module Gamifier
99
99
  params[:per_page] ||= 50
100
100
  res = engine.transmit(:get, path, :query => params)
101
101
  if res.kind_of?(Hash)
102
- entries = res['data'].map{|h| build(h)}
102
+ entries = map_to_models(res)
103
103
  if block
104
104
  # Lazy load all the pages
105
105
  entries.each{|entry| yield(entry)}
@@ -148,6 +148,10 @@ module Gamifier
148
148
  response
149
149
  end
150
150
  end
151
+
152
+ def map_to_models(response)
153
+ response['data'].map{|h| build(h)}
154
+ end
151
155
  end
152
156
 
153
157
  module ClassMethods
@@ -1,3 +1,3 @@
1
1
  module Gamifier
2
- VERSION = "1.0.3" unless defined?(Gamifier::VERSION)
2
+ VERSION = "1.0.4" unless defined?(Gamifier::VERSION)
3
3
  end
@@ -0,0 +1,20 @@
1
+ require 'spec_integration_helper'
2
+
3
+ describe "managing collections" do
4
+ before :all do
5
+ @site = ENGINE.sites.find("ibadgedyou.dev")
6
+ @site.should_not be_nil
7
+ ENGINE.activity_definitions.reset!(:site => "ibadgedyou.dev")
8
+ end
9
+
10
+ it "should list all players" do
11
+ 3.times do |i|
12
+ behavior = ENGINE.activity_definitions.build({:selector => {:verb => "visit#{i}"}, :name => "visit#{i}", :site_id => @site._id}).save
13
+ behavior.should_not be_nil
14
+ end
15
+ ENGINE.activity_definitions.all(:site => "ibadgedyou.dev").length.should == 3
16
+ ENGINE.activity_definitions.reset!(:site => "ibadgedyou.dev")
17
+ ENGINE.activity_definitions.all(:site => "ibadgedyou.dev").length.should == 0
18
+ end
19
+
20
+ end
@@ -23,7 +23,7 @@ describe "managing players" do
23
23
  end
24
24
 
25
25
  it "should credit an activity for the player" do
26
- activity = @player.credit("visit", :contents_attributes => [{:content_url => "http://somwhere.ltd", :content_type => "page", :title => "Test1"}])
26
+ activity = @player.credit("visit", :contents_attributes => [{:content_url => "http://dimelo.com", :content_type => "page", :title => "Test1"}])
27
27
  activity.should_not be_false
28
28
  activity.contents.first["title"].should == "Test1"
29
29
  activity.rewards.should be_empty
@@ -38,4 +38,4 @@ describe "managing players" do
38
38
  end
39
39
  end
40
40
 
41
- end
41
+ end
@@ -4,4 +4,4 @@ require 'webmock/rspec'
4
4
  $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
5
5
  require 'gamifier'
6
6
 
7
- WebMock.disable_net_connect!
7
+ WebMock.disable_net_connect!
@@ -11,8 +11,7 @@ RSpec.configure do |config|
11
11
  config.before :all do
12
12
  Gamifier.set :uri, ENV.fetch("BADGEVILLE_URI") { "http://sandbox.v2.badgeville.com/api/berlin/" }
13
13
  Gamifier.set :key, ENV.fetch("BADGEVILLE_KEY") { "1234" }
14
- ENGINE = Gamifier.engine
15
- Gamifier.logger.level = Logger::DEBUG
14
+ ENGINE = Gamifier.engine if !defined?(ENGINE)
16
15
  end
17
16
 
18
17
  def player_attributes
@@ -18,4 +18,16 @@ describe Gamifier::Collection do
18
18
  object.key.should == "value"
19
19
  end
20
20
 
21
+ it "should raise an error if no site is given when calling #reset!" do
22
+ expect{
23
+ @collection.reset!
24
+ }.to raise_error(ArgumentError)
25
+ end
26
+
27
+ it "should call destroy on each entry of the collection when calling #reset!" do
28
+ model = mock(Gamifier::Model)
29
+ model.should_receive(:destroy)
30
+ @collection.should_receive(:all).with(:site => "xyz").and_yield model
31
+ @collection.reset!(:site => "xyz")
32
+ end
21
33
  end
@@ -62,10 +62,18 @@ describe Gamifier::Engine do
62
62
  @engine.connection.should be_a(Object)
63
63
  end
64
64
 
65
+ it "should output to the given logger" do
66
+ @logger.should_receive(:info).at_least(1).times
67
+
68
+ stub_request(:get, "http://somewhere.ltd/path/1234/resource.json").
69
+ with(:query => {"limit" => "5"})
70
+ @engine.transmit :get, "resource", :query => {"limit" => "5"}
71
+ end
72
+
65
73
  describe "#transmit" do
66
74
  it "transmits POST requests to the API" do
67
75
  stub_request(:post, "http://somewhere.ltd/path/1234/resource.json").
68
- with(:body => Gamifier::Engine.encode_www_form({:a => "b", :c => ["d", "e"]}), :headers => { 'Content-Type' => 'application/x-www-form-urlencoded' })
76
+ with(:body => {:a => "b", :c => ["d", "e"]}, :headers => { 'Content-Type' => 'application/x-www-form-urlencoded' })
69
77
  @engine.transmit :post, "resource", :body => {"a" => "b", :c => ["d", "e"]}
70
78
  end
71
79
  it "transmits GET requests to the API" do
metadata CHANGED
@@ -1,103 +1,104 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: gamifier
3
- version: !ruby/object:Gem::Version
4
- version: 1.0.3
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
5
  prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 4
10
+ version: 1.0.4
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Cyril Rohr
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-07-13 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2012-07-20 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: faraday
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: 0.4.1
22
- type: :runtime
23
22
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
23
+ requirement: &id001 !ruby/object:Gem::Requirement
25
24
  none: false
26
- requirements:
25
+ requirements:
27
26
  - - ~>
28
- - !ruby/object:Gem::Version
29
- version: 0.4.1
30
- - !ruby/object:Gem::Dependency
31
- name: json
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: '0'
27
+ - !ruby/object:Gem::Version
28
+ hash: 27
29
+ segments:
30
+ - 0
31
+ - 8
32
+ version: "0.8"
38
33
  type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: json
39
37
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
38
+ requirement: &id002 !ruby/object:Gem::Requirement
41
39
  none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
46
- - !ruby/object:Gem::Dependency
47
- name: rack
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: '0'
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
54
47
  type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: faraday_middleware
55
51
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- - !ruby/object:Gem::Dependency
63
- name: rspec
64
- requirement: !ruby/object:Gem::Requirement
52
+ requirement: &id003 !ruby/object:Gem::Requirement
65
53
  none: false
66
- requirements:
54
+ requirements:
67
55
  - - ~>
68
- - !ruby/object:Gem::Version
69
- version: '2'
70
- type: :development
56
+ - !ruby/object:Gem::Version
57
+ hash: 27
58
+ segments:
59
+ - 0
60
+ - 8
61
+ version: "0.8"
62
+ type: :runtime
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ name: rspec
71
66
  prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
67
+ requirement: &id004 !ruby/object:Gem::Requirement
73
68
  none: false
74
- requirements:
69
+ requirements:
75
70
  - - ~>
76
- - !ruby/object:Gem::Version
77
- version: '2'
78
- - !ruby/object:Gem::Dependency
79
- name: webmock
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - ! '>='
84
- - !ruby/object:Gem::Version
85
- version: '0'
71
+ - !ruby/object:Gem::Version
72
+ hash: 7
73
+ segments:
74
+ - 2
75
+ version: "2"
86
76
  type: :development
77
+ version_requirements: *id004
78
+ - !ruby/object:Gem::Dependency
79
+ name: webmock
87
80
  prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
81
+ requirement: &id005 !ruby/object:Gem::Requirement
89
82
  none: false
90
- requirements:
91
- - - ! '>='
92
- - !ruby/object:Gem::Version
93
- version: '0'
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ type: :development
91
+ version_requirements: *id005
94
92
  description: Gem for accessing game engine APIs
95
- email:
93
+ email:
96
94
  - cyril.rohr@gmail.com
97
95
  executables: []
96
+
98
97
  extensions: []
98
+
99
99
  extra_rdoc_files: []
100
- files:
100
+
101
+ files:
101
102
  - .gitignore
102
103
  - Gemfile
103
104
  - LICENSE
@@ -125,6 +126,7 @@ files:
125
126
  - lib/gamifier/models/unit.rb
126
127
  - lib/gamifier/models/user.rb
127
128
  - lib/gamifier/version.rb
129
+ - spec/integration/collection_integration_spec.rb
128
130
  - spec/integration/dsl_integration_spec.rb
129
131
  - spec/integration/player_integration_spec.rb
130
132
  - spec/integration/user_integration_spec.rb
@@ -138,31 +140,41 @@ files:
138
140
  - spec/unit/gamifier_spec.rb
139
141
  - spec/unit/model_spec.rb
140
142
  - spec/unit/models/player_spec.rb
141
- homepage: ''
143
+ homepage: ""
142
144
  licenses: []
145
+
143
146
  post_install_message:
144
147
  rdoc_options: []
145
- require_paths:
148
+
149
+ require_paths:
146
150
  - lib
147
- required_ruby_version: !ruby/object:Gem::Requirement
151
+ required_ruby_version: !ruby/object:Gem::Requirement
148
152
  none: false
149
- requirements:
150
- - - ! '>='
151
- - !ruby/object:Gem::Version
152
- version: '0'
153
- required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ hash: 3
157
+ segments:
158
+ - 0
159
+ version: "0"
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
161
  none: false
155
- requirements:
156
- - - ! '>='
157
- - !ruby/object:Gem::Version
158
- version: '0'
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ hash: 3
166
+ segments:
167
+ - 0
168
+ version: "0"
159
169
  requirements: []
170
+
160
171
  rubyforge_project:
161
- rubygems_version: 1.8.21
172
+ rubygems_version: 1.8.17
162
173
  signing_key:
163
174
  specification_version: 3
164
175
  summary: Currently supports Badgeville Core API
165
- test_files:
176
+ test_files:
177
+ - spec/integration/collection_integration_spec.rb
166
178
  - spec/integration/dsl_integration_spec.rb
167
179
  - spec/integration/player_integration_spec.rb
168
180
  - spec/integration/user_integration_spec.rb
@@ -176,4 +188,3 @@ test_files:
176
188
  - spec/unit/gamifier_spec.rb
177
189
  - spec/unit/model_spec.rb
178
190
  - spec/unit/models/player_spec.rb
179
- has_rdoc: