open_badges 0.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,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ /bin
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in open_badges.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Eric Allam
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # OpenBadges
2
+
3
+ Provides some objects to make it easy to work with the OpenBadges [issuer API](https://github.com/mozilla/openbadges/wiki/Issuer-API). Right
4
+ now this only includes the ability to create the [Assertion JSON](https://github.com/mozilla/openbadges/wiki/Assertions) necessary when responding
5
+ to a assertion request from OpenBadges.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'open_badges'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install open_badges
20
+
21
+ ## Usage
22
+
23
+ Using this in a Rails app is really simple. Let's say you have a `BadgesController`
24
+ with a `assertion` action that will serve up the assertion JSON. Here is how you would use it:
25
+
26
+ ```ruby
27
+ class BadgesController < ApplicationController
28
+ def assertion
29
+ assertion = OpenBadges::Assertion.new
30
+ # You'd probably fill in these values from some `badges` table and
31
+ # user information
32
+ assertion.email = "<email>"
33
+ assertion.issued_on = Time.now
34
+ assertion.name = "Completed Level 1 of Something"
35
+ assertion.description = "Completed Level 1 of Something"
36
+ assertion.badge_url = "http://www.codeschool.com/badges/1"
37
+ assertion.criteria_url = "http://www.codeschool.com/badges/1"
38
+
39
+ render json: assertion
40
+ end
41
+ end
42
+ ```
43
+
44
+ And then setup a couple of pieces of information about you, the issuer, in the respective environment:
45
+
46
+ ```ruby
47
+ # in config/environments/production.rb
48
+ config.after_initialize do
49
+ OpenBadges.issuer_url = "http://www.codeschool.com"
50
+ OpenBadges.issuer_name = "Code School"
51
+ end
52
+ ```
53
+
54
+ This would create JSON that looked like this:
55
+
56
+ ```javascript
57
+ {
58
+ :recipient=>"sha256$56ad781803f326ddab2f5eaa270964d40d43a11abd193fdb121334f1e653263a",
59
+ :salt=>"c1be0242548c455c654e720ae304e5b6",
60
+ :issued_on=>"2012-08-26",
61
+ :badge=>{
62
+ :version=>"0.5.0",
63
+ :name=>"Completed Level 1 of Something",
64
+ :image=>"http://www.codeschool.com/badges/1",
65
+ :description=>"Completed Level 1 of Something",
66
+ :criteria=>"http://www.codeschool.com/badges/1/criteria",
67
+ :issuer=>{
68
+ :origin=>"http://www.codeschool.com",
69
+ :name=>"Code School",
70
+ :org=>nil,
71
+ :contact=>nil
72
+ }
73
+ }
74
+ }
75
+ ```
76
+
77
+ ## Contributing
78
+
79
+ 1. Fork it
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
82
+ 4. Push to the branch (`git push origin my-new-feature`)
83
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,44 @@
1
+ require 'digest'
2
+ require 'securerandom'
3
+
4
+ module OpenBadges
5
+ class Assertion
6
+ attr_accessor :email, :issued_on, :name, :description,
7
+ :badge_url, :criteria_url
8
+
9
+ def as_json options={}
10
+ {
11
+ recipient: recipient,
12
+ salt: salt,
13
+ issued_on: issued_on,
14
+ badge: {
15
+ version: OpenBadges.version,
16
+ name: self.name,
17
+ image: self.badge_url,
18
+ description: self.description,
19
+ criteria: criteria_url,
20
+ issuer: {
21
+ origin: OpenBadges.issuer_url,
22
+ name: OpenBadges.issuer_name,
23
+ org: OpenBadges.issuer_org,
24
+ contact: OpenBadges.issuer_contact
25
+ }
26
+ }
27
+ }
28
+ end
29
+
30
+ private
31
+
32
+ def recipient
33
+ "sha256$#{Digest::SHA256.hexdigest(self.email + salt)}"
34
+ end
35
+
36
+ def salt
37
+ @salt ||= SecureRandom.hex
38
+ end
39
+
40
+ def issued_on
41
+ @issued_on.strftime("%Y-%m-%d")
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module OpenBadges
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ require "open_badges/version"
2
+ require "open_badges/assertion"
3
+ require "active_support/core_ext"
4
+
5
+ module OpenBadges
6
+ mattr_accessor :issuer_url
7
+ mattr_accessor :issuer_name
8
+ mattr_accessor :issuer_org
9
+ mattr_accessor :issuer_contact
10
+ mattr_accessor :version
11
+ self.version = "0.5.0"
12
+ mattr_accessor :salt
13
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'open_badges/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "open_badges"
8
+ gem.version = OpenBadges::VERSION
9
+ gem.authors = ["Eric Allam"]
10
+ gem.email = ["rubymaverick@gmail.com"]
11
+ gem.description = %q{Work with OpenBadges Issuer API}
12
+ gem.summary = %q{Work with OpenBadges Issuer API}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "activesupport", "~> 3.2.0"
21
+
22
+ gem.add_development_dependency "rspec", "2.11.0"
23
+ gem.add_development_dependency "timecop", "0.4.5"
24
+ end
@@ -0,0 +1,48 @@
1
+ require_relative '../lib/open_badges'
2
+ require 'timecop'
3
+ require 'digest'
4
+
5
+ describe OpenBadges::Assertion do
6
+ # Timecop freezes time
7
+ before(:all) { Timecop.freeze(Time.local(2012, 8, 26, 12, 0, 0)) }
8
+ after(:all) { Timecop.return }
9
+
10
+ def digest message
11
+ Digest::SHA256.hexdigest(message)
12
+ end
13
+
14
+ let(:assertion) { described_class.new }
15
+
16
+ context "#as_json" do
17
+ subject { assertion.as_json }
18
+
19
+ before do
20
+ OpenBadges.issuer_url = "http://www.codeschool.com"
21
+ OpenBadges.issuer_name = "Code School"
22
+
23
+ assertion.email = "rubymaverick@gmail.com"
24
+ assertion.issued_on = Time.now
25
+ assertion.name = "Completed Level 1 of Ruby Bits"
26
+ assertion.description = "Completed Level 1 of Ruby Bits"
27
+ assertion.badge_url = "http://www.codeschool.com/badges/1"
28
+ assertion.criteria_url = "http://www.codeschool.com/badges/1/criteria"
29
+ end
30
+
31
+ it "should produce valid json" do
32
+ subject[:recipient].split("$").first.should == "sha256"
33
+ subject[:recipient].split("$").last.should == digest(assertion.email + subject[:salt])
34
+ subject[:issued_on].should == "2012-08-26"
35
+
36
+ badge = subject[:badge]
37
+ badge[:version].should == OpenBadges.version
38
+ badge[:name].should == assertion.name
39
+ badge[:description].should == assertion.description
40
+ badge[:image].should == assertion.badge_url
41
+ badge[:criteria].should == assertion.criteria_url
42
+
43
+ issuer = badge[:issuer]
44
+ issuer[:origin].should == OpenBadges.issuer_url
45
+ issuer[:name].should == OpenBadges.issuer_name
46
+ end
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_badges
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eric Allam
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.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: 3.2.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 2.11.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: 2.11.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: timecop
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '='
52
+ - !ruby/object:Gem::Version
53
+ version: 0.4.5
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.4.5
62
+ description: Work with OpenBadges Issuer API
63
+ email:
64
+ - rubymaverick@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/open_badges.rb
75
+ - lib/open_badges/assertion.rb
76
+ - lib/open_badges/version.rb
77
+ - open_badges.gemspec
78
+ - spec/assertion_spec.rb
79
+ homepage: ''
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.23
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Work with OpenBadges Issuer API
103
+ test_files:
104
+ - spec/assertion_spec.rb