badgeville 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Please keep me alphabetized!
4
+ gem 'json'
5
+ gem 'rest-client'
6
+ # Please keep me alphabetized!
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "rspec", "~> 2.3.0"
12
+ gem "bundler", "~> 1.0.0"
13
+ gem "jeweler", "~> 1.6.4"
14
+ gem "rcov", ">= 0"
15
+ end
16
+
17
+ group :test do
18
+ gem "webmock"
19
+ end
@@ -0,0 +1,40 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.2.6)
5
+ crack (0.1.8)
6
+ diff-lcs (1.1.2)
7
+ git (1.2.5)
8
+ jeweler (1.6.4)
9
+ bundler (~> 1.0)
10
+ git (>= 1.2.5)
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)
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)
24
+ diff-lcs (~> 1.1.2)
25
+ rspec-mocks (2.3.0)
26
+ webmock (1.6.4)
27
+ addressable (~> 2.2, > 2.2.5)
28
+ crack (>= 0.1.7)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bundler (~> 1.0.0)
35
+ jeweler (~> 1.6.4)
36
+ json
37
+ rcov
38
+ rest-client
39
+ rspec (~> 2.3.0)
40
+ webmock
@@ -0,0 +1,35 @@
1
+ Nitro API
2
+ ============
3
+
4
+ Client for Badgeville API http://rules.badgeville.com/
5
+
6
+ (not complete)
7
+
8
+ Usage
9
+ --------
10
+ First, checkout the examples folder. TL;DR:
11
+
12
+ * create connection object with your api_key, and secret.
13
+
14
+ ```ruby
15
+ require 'badgeville'
16
+ settings = JSON.parse(File.read('keys.json.example'))
17
+ badgeville = Badgeville::Client.new(email, settings)
18
+ ```
19
+
20
+ * log actions
21
+
22
+ ```ruby
23
+ badgeville.log_activity "commented"
24
+ ```
25
+
26
+ Installing
27
+ ----------
28
+ 1) add to your Gemfile
29
+
30
+ ```
31
+ gem 'badgeville'
32
+ ```
33
+
34
+ 2) Create a keys.json file with your api_key and secret (see keys.json.example file)
35
+
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "badgeville"
18
+ gem.homepage = "http://github.com/Keasinc/badgeville"
19
+ gem.license = "MIT"
20
+ gem.summary = "Client for Badgeville's Core API"
21
+ gem.description = "Client for Badgeville's Core API v2. http://rules.badgeville.com"
22
+ gem.authors = ["Gilad Buchman"]
23
+ # dependencies defined in Gemfile
24
+ end
25
+ Jeweler::RubygemsDotOrgTasks.new
26
+
27
+ require 'rspec/core'
28
+ require 'rspec/core/rake_task'
29
+ RSpec::Core::RakeTask.new(:spec) do |spec|
30
+ spec.pattern = FileList['spec/**/*_spec.rb']
31
+ end
32
+
33
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
34
+ spec.pattern = 'spec/**/*_spec.rb'
35
+ spec.rcov = true
36
+ end
37
+
38
+ task :default => :spec
39
+
40
+ require 'rake/rdoctask'
41
+ Rake::RDocTask.new do |rdoc|
42
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "badgeville #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'badgeville'
4
+ require 'pp'
5
+
6
+ settings = JSON.parse(File.read('keys.json'))
7
+ badgeville = Badgeville::Client.new('4@keas.com', settings)
8
+
9
+ pp badgeville.log_activity "commented"
@@ -0,0 +1,7 @@
1
+ {
2
+ "private_key" : "8aab2065219a35ddf7fbd277140fc44f",
3
+ "public_key" : "6e221fe1358f6999cd778fb46c08344b",
4
+ "host" : "sandbox.v2.badgeville.com",
5
+ "protocol" : "http",
6
+ "site" : "example.com"
7
+ }
@@ -0,0 +1,77 @@
1
+ require 'json'
2
+ require 'rest_client'
3
+ require 'badgeville/activity'
4
+
5
+ module Badgeville
6
+ TIMEOUT = 1000
7
+ HOST = "sandbox.v2.badgeville.com"
8
+ PROTOCOL = "http"
9
+
10
+ class BadgevilleError < StandardError
11
+ attr_accessor :code
12
+
13
+ def initialize (err_code=nil)
14
+ @code = err_code
15
+ end
16
+ end
17
+
18
+ class Client
19
+
20
+ def initialize (email, opts={})
21
+ # Required Parameters
22
+ @site = opts['site']
23
+ @private_key = opts['private_key']
24
+ @public_key = opts['public_key']
25
+ @host = opts['host'] || HOST
26
+ @protocol = opts['protocol'] || PROTOCOL
27
+ @user = email
28
+ end
29
+
30
+ def log_activity(activity, opts={})
31
+ params = {
32
+ "activity[verb]" => activity,
33
+ }
34
+ response = make_call(:post, :activities, params)
35
+ Activity.new(response)
36
+ end
37
+
38
+ private
39
+
40
+ def valid_response?(obj)
41
+ obj.is_a?(Array) || obj.is_a?(Hash)
42
+ end
43
+
44
+ def ensure_array(items)
45
+ items.is_a?(Array) ? items : [items]
46
+ end
47
+
48
+ def session
49
+ if @session.nil?
50
+ base_url = "#{@protocol}://#{@host}/api/berlin/#{@private_key}"
51
+ @session = RestClient::Resource.new base_url
52
+ end
53
+ @session
54
+ end
55
+
56
+ def make_call(method, action, params)
57
+ end_point = "#{action.to_s}.json"
58
+ params.merge!(:user => @user, :site => @site)
59
+ begin
60
+ response = session[end_point].send(method, to_query(params))
61
+ data = response.body
62
+ json = JSON.parse(data)
63
+ rescue => e
64
+ if e.respond_to? :response
65
+ data = JSON.parse(e.response)
66
+ raise BadgevilleError.new(e.code, data["error"])
67
+ else
68
+ raise e
69
+ end
70
+ end
71
+ end
72
+
73
+ def to_query params
74
+ URI.escape(params.map { |k,v| "#{k.to_s}=#{v.to_s}" }.join("&"))
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,19 @@
1
+ require 'date'
2
+ require 'badgeville/reward'
3
+
4
+ module Badgeville
5
+ class Activity
6
+ attr_accessor :verb, :player_id, :user_id, :points, :rewards, :created_at, :meta
7
+
8
+ def initialize(json)
9
+ @verb = json.delete("verb")
10
+ @player_id = json.delete("player_id")
11
+ @user_id = json.delete("user_id")
12
+ @points = json.delete("points").to_i
13
+ @created_at = DateTime.parse(json.delete("created_at")).to_time
14
+ @rewards = json.delete("rewards").inject([]) do |rewards, award|
15
+ rewards << Reward.new(award["definition"])
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,38 @@
1
+ module Badgeville
2
+ class Reward
3
+ attr_accessor :name, :hint, :image_url, :active
4
+ end
5
+
6
+ # example:
7
+ # {
8
+ # "name": "A Way with Words",
9
+ # "active_start_at": null,
10
+ # "image_file_name": "9fQ2IU6-0.ftbs4fsoogp3c8fr.png",
11
+ # "data": {
12
+ # "verb": "commented",
13
+ # "threshold": 2
14
+ # },
15
+ # "created_at": "2011-08-18T22:55:03-07:00",
16
+ # "image_url": "http://s3.amazonaws.com/badgeville-production-reward-definitions/images/4e4dfab6c47eed727b005c38/original.png?1313733302",
17
+ # "components": "[{\"command\":\"count\",\"comparator\":{\"$gte\":2},\"config\":{},\"where\":{\"verb\":\"commented\",\"player_id\":\"%player_id\"}}]",
18
+ # "reward_template": {
19
+ # "message": ""
20
+ # },
21
+ # "_id": "4e4dfab6c47eed727b005c38",
22
+ # "tags": null,
23
+ # "id": "4e4dfab6c47eed727b005c38",
24
+ # "active_end_at": null,
25
+ # "type": "achievement",
26
+ # "hint": "Reply to 25 Comments",
27
+ # "assignable": false,
28
+ # "allow_duplicates": false,
29
+ # "site_id": "4e4d5bf5c47eed25a0000e8f",
30
+ # "active": true
31
+ # }
32
+ def initialize(json)
33
+ @name = json["name"]
34
+ @active = json["active"]
35
+ @hint = json["hint"]
36
+ @image_url = json["image_url"]
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Badgeville do
4
+ before do
5
+ @user = "user"
6
+ @badgeville = Badgeville::Client.new @user, 'private_key' => "api_key",
7
+ 'site' => 'example.com'
8
+ end
9
+
10
+ describe "#log_activity" do
11
+ before do
12
+ action_name = "action"
13
+ @result = {"verb" => "commented",
14
+ "created_at" => "2011-08-30T12:27:47-07:00",
15
+ "points" => 0,
16
+ "player_id" => "4e5d3992c47eed202d00148c",
17
+ "user_id" => "4e5d3992c47eed202d00148b",
18
+ "rewards" =>[]
19
+ }
20
+
21
+ body = "activity[verb]=#{action_name}&user=#{@user}&site=example.com"
22
+ url = "http://" + Badgeville::HOST + ".*activities.json"
23
+
24
+ stub_http_request(:post, Regexp.new(url)).with(:body => body).
25
+ to_return(:body => @result.to_json)
26
+
27
+ @activity = @badgeville.log_activity action_name
28
+ end
29
+
30
+ it "parses response from activity record api call" do
31
+ @activity.verb.should == "commented"
32
+ @activity.created_at.iso8601.should == @result["created_at"]
33
+ @activity.points.should == 0
34
+ @activity.rewards.should be_empty
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'badgeville'
5
+ require 'webmock/rspec'
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: badgeville
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gilad Buchman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-30 00:00:00.000000000 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ requirement: &2160484700 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2160484700
26
+ - !ruby/object:Gem::Dependency
27
+ name: rest-client
28
+ requirement: &2160483340 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *2160483340
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &2160482360 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 2.3.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *2160482360
48
+ - !ruby/object:Gem::Dependency
49
+ name: bundler
50
+ requirement: &2160481280 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.0.0
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *2160481280
59
+ - !ruby/object:Gem::Dependency
60
+ name: jeweler
61
+ requirement: &2160480100 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 1.6.4
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *2160480100
70
+ - !ruby/object:Gem::Dependency
71
+ name: rcov
72
+ requirement: &2160479240 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *2160479240
81
+ description: Client for Badgeville's Core API v2. http://rules.badgeville.com
82
+ email:
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files:
86
+ - README.md
87
+ files:
88
+ - .document
89
+ - Gemfile
90
+ - Gemfile.lock
91
+ - README.md
92
+ - Rakefile
93
+ - VERSION
94
+ - examples/log_and_check_status.rb
95
+ - keys.json.example
96
+ - lib/badgeville.rb
97
+ - lib/badgeville/activity.rb
98
+ - lib/badgeville/reward.rb
99
+ - spec/badgeville_spec.rb
100
+ - spec/spec_helper.rb
101
+ has_rdoc: true
102
+ homepage: http://github.com/Keasinc/badgeville
103
+ licenses:
104
+ - MIT
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ segments:
116
+ - 0
117
+ hash: -517773847151955177
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 1.6.2
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: Client for Badgeville's Core API
130
+ test_files: []