uboost-client 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/LICENCE.txt ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2012 Gonzalo Rodríguez-Baltanás Díaz, Haiku Learning Systems
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,48 @@
1
+ # uBoost Client API
2
+
3
+ This is the unofficial ruby client for uBoost API. It is a wrapper for the REST interface described at [https://github.com/chriskk/uBoost-API-v2](https://github.com/chriskk/uBoost-API-v2)
4
+
5
+ ## Installing
6
+
7
+ ```bash
8
+ gem install 'uboost-client'
9
+ ```
10
+
11
+ ## API
12
+
13
+ ```ruby
14
+ require 'uboost-client'
15
+
16
+ client = UboostClient::Client.new(:subdomain => 'test_subdomain', :api_credentials =>
17
+ {:username => 'api_username', password => 'api_password'})
18
+
19
+ # Examples
20
+
21
+ client.account.create({ "user_name" => "test_user_2" })
22
+ =>
23
+ #<OpenStruct student={"id"=>921679358, "external_id"=>nil, "catalog_id"=>109, "site_id"=>98, "user_name"=>"test_user_2", "encrypted_password"=>"kabyle00", "points"=>0, "points_accumulated"=>0, ...}
24
+
25
+ client.account.select(921679358)
26
+
27
+ client.account.delete(921679358)
28
+
29
+ client.account.update(921679358, {:first_name => 'custom name'})
30
+
31
+ client.account.find(:user_name => 'isaacnewtonx')
32
+
33
+ client.account.find(:external_id => '3253466')
34
+
35
+ client.account.token(921679358)
36
+
37
+ client.points.point_transactions_for_account(921679358)
38
+
39
+ client.account.points_transactions(921679358)
40
+
41
+ client.points.add_points_to_account(921679359, 30, {:description => 'a description'})
42
+
43
+ client.badges.award(921679359, 1)
44
+
45
+ client.badges.unaward(921679359, 1)
46
+
47
+ client.widgets.profile(921679358)
48
+ ```
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require File.expand_path('./lib/uboost-client/version')
4
+
5
+ desc "Build gem"
6
+ task 'build' do
7
+ system("gem build uboost-client.gemspec")
8
+ end
9
+
10
+ desc "Publish gem"
11
+ task "release" do
12
+ system("gem push uboost-client-#{UboostClient::VERSION}.gem")
13
+ end
14
+
15
+ desc "Remove built gems"
16
+ task "clear" do
17
+ system("rm *.gem")
18
+ end
@@ -0,0 +1,208 @@
1
+ require 'rubygems'
2
+ require 'faraday'
3
+ require 'json'
4
+ require 'ostruct'
5
+
6
+ module UboostClient
7
+
8
+ class Client
9
+
10
+ attr_accessor :subdomain, :api_credentials
11
+
12
+ def initialize(options = Hash.new)
13
+ @subdomain = options[:subdomain]
14
+ @api_credentials = options[:api_credentials]
15
+ end
16
+
17
+ def connection
18
+ url = "https://#{@api_credentials[:username]}:#{@api_credentials[:password]}@#{@subdomain}.uboost.com"
19
+ Faraday.new(url) do |faraday|
20
+ faraday.request :url_encoded # form-encode POST params
21
+ faraday.response :logger # log requests to STDOUT
22
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
23
+ end
24
+ end
25
+
26
+ def account
27
+ @account ||= UboostClient::Account.new(self)
28
+ end
29
+
30
+ def points
31
+ @points ||= UboostClient::Points.new(self)
32
+ end
33
+
34
+ def badges
35
+ @badges ||= UboostClient::Badges.new(self)
36
+ end
37
+
38
+ def widgets
39
+ @widgets ||= UboostClient::Widgets.new(self)
40
+ end
41
+
42
+ end
43
+
44
+ class Account
45
+ attr_accessor :client, :url
46
+
47
+ def initialize(client)
48
+ @client = client
49
+ @url = '/api/accounts'
50
+ end
51
+
52
+ def create(data)
53
+ data = {'account' => data}
54
+ response = client.connection.post do |req|
55
+ req.url @url
56
+ req.headers['Content-Type'] = 'application/json'
57
+ req.body = JSON.generate(data)
58
+ end
59
+ OpenStruct.new(JSON.parse(response.body))
60
+ end
61
+
62
+ def select(account_id)
63
+ response = client.connection.get "#{@url}/#{account_id}"
64
+ OpenStruct.new(JSON.parse(response.body))
65
+ end
66
+
67
+ def update(account_id, hash)
68
+ data = {'account' => hash}
69
+ response = client.connection.put do |req|
70
+ req.url "#{@url}/#{account_id}"
71
+ req.headers['Content-Type'] = 'application/json'
72
+ req.body = JSON.generate(data)
73
+ end
74
+ OpenStruct.new(JSON.parse(response.body))
75
+ end
76
+
77
+ def delete(account_id)
78
+ response = client.connection.delete "#{@url}/#{account_id}"
79
+ OpenStruct.new(JSON.parse(response.body))
80
+ end
81
+
82
+ def find(query)
83
+ response = client.connection.get do |req|
84
+ req.url @url + "/find", query
85
+ end
86
+ OpenStruct.new(JSON.parse(response.body))
87
+ end
88
+
89
+ def token(id)
90
+ response = client.connection.get "#{@url}/#{id}/sign_in_user"
91
+ OpenStruct.new(JSON.parse(response.body))
92
+ end
93
+
94
+ def points_transactions(account_id, options = nil)
95
+ response = client.connection.get do |req|
96
+ req.url @url + "/#{account_id}/points_transactions", options
97
+ end
98
+ OpenStruct.new(JSON.parse(response.body))
99
+ end
100
+
101
+ end
102
+
103
+ class Points
104
+
105
+ attr_accessor :connection, :url
106
+
107
+ def initialize(client)
108
+ client.connection = client
109
+ @url = '/api/points_transactions'
110
+ end
111
+
112
+ def points_transactions_for_account(id_or_options)
113
+ options = id_or_options
114
+ options = {:account_id => id_or_options} if !id_or_options.is_a? Hash
115
+ response = client.connection.get do |req|
116
+ req.url @url, options
117
+ end
118
+ OpenStruct.new(JSON.parse(response.body))
119
+ end
120
+
121
+ def add_points_to_account(id, points, options = nil)
122
+ data = {:points_transaction => {:account_id => id, :points_change => points}}
123
+ data[:points_transaction].merge!(options) if options
124
+ response = client.connection.post do |req|
125
+ req.url @url
126
+ req.headers['Content-Type'] = 'application/json'
127
+ req.body = JSON.generate(data)
128
+ end
129
+ OpenStruct.new(JSON.parse(response.body))
130
+ end
131
+
132
+ end
133
+
134
+ class Badges
135
+
136
+ attr_accessor :client, :url
137
+
138
+ def initialize(client)
139
+ @client = client
140
+ @url = '/api/badges'
141
+ end
142
+
143
+ def award(account_id, badge_type_id, options = nil)
144
+ data = {:badge => {:account_id => account_id, :badge_type_id => badge_type_id}}
145
+ data[:badge].merge!(options) if options
146
+ response = client.connection.post do |req|
147
+ req.url @url
148
+ req.headers['Content-Type'] = 'application/json'
149
+ req.body = JSON.generate(data)
150
+ end
151
+ OpenStruct.new(JSON.parse(response.body))
152
+ end
153
+
154
+ def unaward(account_id, badge_type_id, options = nil)
155
+ data = {:badge => {:account_id => account_id, :badge_type_id => badge_type_id}}
156
+ data[:badge].merge!(options) if options
157
+ response = client.connection.delete do |req|
158
+ req.url @url + "/unaward"
159
+ req.headers['Content-Type'] = 'application/json'
160
+ req.body = JSON.generate(data)
161
+ end
162
+ OpenStruct.new(JSON.parse(response.body))
163
+ end
164
+
165
+ end
166
+
167
+ class Widgets
168
+ attr_accessor :url, :client
169
+
170
+ def initialize(client)
171
+ @client = client
172
+ @url = '/api/widgets'
173
+ end
174
+
175
+ def get_sso_token(account_id)
176
+ client.account.token(account_id).student["sso_token"]
177
+ end
178
+
179
+ def profile(account_id)
180
+ response = @client.connection.get @url + '/profile', :sso_token => get_sso_token(account_id)
181
+ OpenStruct.new(JSON.parse(response.body))
182
+ end
183
+
184
+ def ubar(account_id, options = Hash.new)
185
+ options = {:align => "", :bar_color => '', :div_id => ''}.merge(options)
186
+ token = get_sso_token(account_id)
187
+ subdomain_url = client.subdomain + ".uboost.com"
188
+
189
+ " <div style='position: absolute; bottom: 0px; z-index: 2; height: 36px;' id='#{options[:div_id]}'>
190
+ <object>
191
+ <param value='#{subdomain_url}/uBar.swf' name='movie'>
192
+ <param value='100%' name='width'>
193
+ <param value='100%' name='height'>
194
+ <param value='transparent' name='wmode'>
195
+ <param value='always' name='allowScriptAccess'>
196
+ <param value='url=#{subdomain_url}/ubar/&token=#{token}&align=#{options[:align]}&barColor=#{options[:bar_color]}&divId=#{options[:div_id]}'
197
+ name='flashvars'>
198
+ <param value='false' name='cacheBusting'>
199
+ <param value='true' name='allowFullScreen'>
200
+ <embed width='1280' height='400' wmode='transparent' allowfullscreen='true' quality='high'
201
+ cachebusting='false' flashvars='url=#{subdomain_url}/ubar/&token=#{token}&align=#{options[:align]}&barColor=#{options[:bar_color]}&divId=#{:div_id}'
202
+ allowscriptaccess='always' src='#{subdomain_url}/uBar.swf'>
203
+ </object>
204
+ </div>"
205
+ end
206
+ end
207
+
208
+ end
@@ -0,0 +1,3 @@
1
+ class UboostClient
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'uboost-client', 'uboost_client')
@@ -0,0 +1,20 @@
1
+ require File.expand_path('lib/uboost-client')
2
+
3
+ subdomain = ENV['UBOOST_SUBDOMAIN']
4
+ api_credentials = { :username => ENV['UBOOST_USERNAME'], :password => ENV['UBOOST_PASSWORD'] }
5
+
6
+ client = UboostClient::Client.new(:subdomain => subdomain, :api_credentials => api_credentials)
7
+
8
+ # puts client.account.create({ "user_name" => "test_user_2" })
9
+ puts client.account.select(921679358).inspect
10
+ # puts client.account.delete(921679358)
11
+ # puts client.account.update(921679358, {:first_name => 'custom name'})
12
+ # puts client.account.find(:user_name => 'isaacnewtonx')
13
+ # puts client.account.find(:external_id => '3253466')
14
+ # puts client.account.token(921679358)
15
+ # puts client.points.point_transactions_for_account(921679358)
16
+ # puts client.account.points_transactions(921679358)
17
+ # puts client.points.add_points_to_account(921679359, 30, {:description => 'whatps'})
18
+ # puts client.badges.award(921679359, 1)
19
+ # puts client.badges.unaward(921679359, 1)
20
+ # puts client.widgets.profile(921679358)
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('./lib/uboost-client/version')
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Gonzalo Rodríguez-Baltanás Díaz"]
6
+ gem.email = ["siotopo@gmail.com"]
7
+ gem.description = %q{uBoost ruby client}
8
+ gem.homepage = "https://github.com/haikulearning/uboost-client"
9
+ gem.summary = gem.description
10
+
11
+ gem.name = "uboost-client"
12
+ gem.require_paths = ["lib"]
13
+ gem.files = `git ls-files`.split("\n")
14
+ gem.version = UboostClient::VERSION
15
+
16
+ gem.add_dependency "faraday"
17
+ gem.add_development_dependency "bundler", ">= 1.0"
18
+ gem.add_development_dependency "rake"
19
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uboost-client
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gonzalo Rodríguez-Baltanás Díaz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '1.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '1.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: uBoost ruby client
63
+ email:
64
+ - siotopo@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - LICENCE.txt
71
+ - README.markdown
72
+ - Rakefile
73
+ - lib/uboost-client.rb
74
+ - lib/uboost-client/uboost_client.rb
75
+ - lib/uboost-client/version.rb
76
+ - spec/simple_test.rb
77
+ - uboost-client.gemspec
78
+ homepage: https://github.com/haikulearning/uboost-client
79
+ licenses: []
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 1.8.24
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: uBoost ruby client
102
+ test_files: []