badges 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +46 -85
- data/badges.gemspec +1 -0
- data/lib/badges.rb +24 -127
- data/lib/badges/version.rb +1 -1
- data/lib/badges/view_helpers.rb +30 -30
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83abc324aa59bc81fa5a5912c571241635939a07
|
4
|
+
data.tar.gz: f8f5c32225e1dcf6475782a66f6b74dc272eda3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6eedbf1ba5cb9dd7c916f2f0ce9e42d1d567144622e2ca7dee5c52018b8d88a376dc576416a401c9c5419061d161776cf2cb3b40e38d70c189a10269d941a90d
|
7
|
+
data.tar.gz: e0770b7b2945a0cfecdfb918b25ec1a03555c2940de0428fe92bbb10bb032eea591f799dc2c3f4a8823960e7c0b1941d3586eae6575c9c65f09d6a9ca8a6e42e
|
data/README.md
CHANGED
@@ -29,100 +29,59 @@ Or install it yourself as:
|
|
29
29
|
|
30
30
|
## Class usage
|
31
31
|
|
32
|
-
CodeSchool
|
32
|
+
Both *Badges::CodeSchool* and *Badges::Treehouse* are a subclass of *Request*. Because of this, they share the same methods and variables, and function in exactly the same way. Examples following the first will be considerably shorter.
|
33
|
+
|
34
|
+
#### Badges::CodeSchool
|
35
|
+
|
36
|
+
Create a new instance of a CodeSchool badge request.
|
33
37
|
|
34
38
|
codeschool = Badges::CodeSchool.new('username')
|
35
39
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
40
|
+
Badges gem knows the proper url for the request. If the base url for the request happens to change one day, you can optionally pass in a different base url and the gem will use that instead.
|
41
|
+
|
42
|
+
codeschool = Badges::CodeSchool.new('username', 'http://www.codeschool.com/users/')
|
43
|
+
|
44
|
+
CodeSchool is a subclass of Request, so it inherits these useful methods and variables pertaining to your api request.
|
45
|
+
|
46
|
+
codeschool.user_id
|
47
|
+
=> "user"
|
41
48
|
|
42
49
|
codeschool.profile_url
|
43
50
|
=> "http://www.codeschool.com/users/username"
|
44
51
|
|
45
|
-
codeschool.
|
46
|
-
=>
|
47
|
-
|
48
|
-
codeschool.badges
|
49
|
-
=> [{"name"=>"Badge", "badge"=>"http://example.com/badge.png", "course_url"=>"http://example.com"}]
|
50
|
-
|
51
|
-
codeschool.courses_completed
|
52
|
-
=> [{"title"=>"Course", "url"=>"http://example.com", "badge"=>"http://example.com/badge.png"}]
|
52
|
+
codeschool.valid?
|
53
|
+
=> true
|
53
54
|
|
54
|
-
codeschool.
|
55
|
-
=>
|
55
|
+
codeschool.code
|
56
|
+
=> 200
|
56
57
|
|
57
|
-
codeschool.
|
58
|
-
=> "
|
58
|
+
codeschool.message
|
59
|
+
=> "OK"
|
59
60
|
|
60
|
-
|
61
|
-
=> "<b>114225</b>"
|
61
|
+
Any error in the request will yield a message in the console or log.
|
62
62
|
|
63
|
-
|
63
|
+
Retrieved JSON is stored as a plain Ruby object inside the variable *codeschool.body*. Here's an example of proper usage:
|
64
64
|
|
65
|
-
|
66
|
-
|
67
|
-
treehouse.response
|
68
|
-
=> nil # if false username
|
69
|
-
|
70
|
-
treehouse.profile_name
|
71
|
-
=> "profile_name"
|
72
|
-
|
73
|
-
treehouse.profile_url
|
74
|
-
=> "http://teamtreehouse.com/profile_name"
|
75
|
-
|
76
|
-
treehouse.avatar_url
|
65
|
+
codeschool.body.user.avatar
|
77
66
|
=> "http://example.com/avatar.jpg"
|
67
|
+
|
68
|
+
codeschool.body.courses.completed.count
|
69
|
+
=> 11
|
78
70
|
|
79
|
-
|
80
|
-
=>
|
81
|
-
|
82
|
-
|
83
|
-
=> "First Last"
|
84
|
-
|
85
|
-
treehouse.points
|
86
|
-
=> {"total"=>1740, "html"=>447, "css"=>483, "javascript"=>0, "ruby"=>0, "ios"=>0, "business"=>0, "android"=>0, "php"=>0, "wordpress"=>0, "design"=>180, "dev tools"=>0}
|
87
|
-
|
88
|
-
treehouse.points_android
|
89
|
-
=> 1
|
90
|
-
|
91
|
-
treehouse.points_business
|
92
|
-
=> 1
|
93
|
-
|
94
|
-
treehouse.points_css
|
95
|
-
=> 1
|
96
|
-
|
97
|
-
treehouse.points_design
|
98
|
-
=> 1
|
99
|
-
|
100
|
-
treehouse.points_dev_tools
|
101
|
-
=> 1
|
102
|
-
|
103
|
-
treehouse.points_html
|
104
|
-
=> 1
|
105
|
-
|
106
|
-
treehouse.points_ios
|
107
|
-
=> 1
|
108
|
-
|
109
|
-
treehouse.points_javascript
|
110
|
-
=> 1
|
111
|
-
|
112
|
-
treehouse.points_php
|
113
|
-
=> 1
|
114
|
-
|
115
|
-
treehouse.points_ruby
|
116
|
-
=> 1
|
117
|
-
|
118
|
-
treehouse.points_total
|
119
|
-
=> 10
|
120
|
-
|
121
|
-
treehouse.points_wordpress
|
71
|
+
codeschool.body.courses.completed[0].title
|
72
|
+
=> "Rails for Zombies"
|
73
|
+
|
74
|
+
You can view all the attributes of the JSON request online in a nice tree view gui by using the [Json Editor Online](http://www.jsoneditoronline.org).
|
122
75
|
|
123
|
-
|
76
|
+
#### Badges::Treehouse
|
124
77
|
|
125
|
-
|
78
|
+
treehouse = Badges::Treehouse.new('username')
|
79
|
+
|
80
|
+
## Rails view helpers
|
81
|
+
|
82
|
+
#### list_badges
|
83
|
+
|
84
|
+
Create a new instance of a badge request in the controller.
|
126
85
|
|
127
86
|
class BadgesController < ApplicationController
|
128
87
|
def index
|
@@ -131,21 +90,23 @@ Create new badge object in your controller.
|
|
131
90
|
end
|
132
91
|
end
|
133
92
|
|
134
|
-
Inside your view, use the
|
93
|
+
Inside your view, use the list_badges view helper to create a list of badges. Params left out are assumed to be false. You can also use class methods and variables inside the view.
|
135
94
|
|
136
95
|
<h1>Badges#index</h1>
|
137
96
|
<p>Find me in app/views/badges/index.html.erb</p>
|
138
97
|
|
139
|
-
<%= image_tag @codeschool.
|
140
|
-
<%= list_badges @codeschool, codeschool_badges: {image: true, link: true, text: true}, id: '
|
141
|
-
<%= list_badges @codeschool, codeschool_courses_completed: {image: true, link: true, text: true}, id: '
|
142
|
-
<%= list_badges @codeschool, codeschool_courses_in_progress: {image: true, link: true, text: true}, id: '
|
98
|
+
<%= image_tag @codeschool.body.user.avatar %>
|
99
|
+
<%= list_badges @codeschool, codeschool_badges: {image: true, link: true, text: true}, id: 'codeschool_badges', class: 'badges' %>
|
100
|
+
<%= list_badges @codeschool, codeschool_courses_completed: {image: true, link: true, text: true}, id: 'codeschool_courses_completed', class: 'badges' %>
|
101
|
+
<%= list_badges @codeschool, codeschool_courses_in_progress: {image: true, link: true, text: true}, id: 'codeschool_courses_in_progress', class: 'badges' %>
|
143
102
|
|
144
|
-
<%= @treehouse.
|
145
|
-
<%= list_badges @treehouse, treehouse_badges: {image: true, link: true, text: true}, id: '
|
103
|
+
<%= image_tag @treehouse.body.gravatar_url %>
|
104
|
+
<%= list_badges @treehouse, treehouse_badges: {image: true, link: true, text: true}, id: 'treehouse_badges', class: 'badges' %>
|
146
105
|
|
147
106
|
Tip: Use css/sass afterwards to style this list appropriately.
|
148
107
|
|
108
|
+
*list_badges* will render nothing if there is an error in the request. Check the console or log for an error message.
|
109
|
+
|
149
110
|
## Contributing
|
150
111
|
|
151
112
|
1. Fork it
|
data/badges.gemspec
CHANGED
data/lib/badges.rb
CHANGED
@@ -1,151 +1,48 @@
|
|
1
1
|
require 'badges/version'
|
2
2
|
require 'active_support/core_ext/object/blank'
|
3
3
|
require 'httparty'
|
4
|
+
require 'dish'
|
4
5
|
|
5
6
|
module Badges
|
6
|
-
class
|
7
|
+
class Request
|
7
8
|
include HTTParty
|
8
|
-
attr_reader :user_id, :profile_url, :
|
9
|
+
attr_reader :user_id, :profile_url, :code, :message, :body
|
9
10
|
|
10
11
|
def initialize(user_id, base_url)
|
11
12
|
@user_id = user_id
|
12
13
|
@profile_url = base_url + user_id
|
13
|
-
|
14
|
+
|
15
|
+
response = HTTParty.get(@profile_url + '.json')
|
16
|
+
|
17
|
+
@code = response.code
|
18
|
+
@message = response.message
|
19
|
+
@body = Dish parse(response.body)
|
20
|
+
|
21
|
+
puts "#{self.class.name}: Response Error #{@code}, #{@message}" unless valid?
|
22
|
+
end
|
23
|
+
|
24
|
+
def valid?
|
25
|
+
@code == 200 ? true : false
|
14
26
|
end
|
15
27
|
|
16
28
|
private
|
17
29
|
|
18
|
-
def
|
19
|
-
|
20
|
-
|
21
|
-
rescue
|
30
|
+
def parse(body)
|
31
|
+
JSON.parse body
|
32
|
+
rescue JSON::ParserError
|
22
33
|
nil
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def obtain_key(hash, key)
|
27
|
-
if hash.has_key?(key)
|
28
|
-
hash[key]
|
29
|
-
end
|
30
34
|
end
|
31
35
|
end
|
32
36
|
|
33
|
-
class CodeSchool <
|
34
|
-
def initialize(user_id)
|
35
|
-
super(user_id,
|
36
|
-
|
37
|
-
@response = nil if obtain_key(@response, 'error')
|
38
|
-
end
|
39
|
-
|
40
|
-
def avatar_url
|
41
|
-
user = obtain_key @response, 'user' if @response
|
42
|
-
obtain_key user, 'avatar' if !user.blank?
|
43
|
-
end
|
44
|
-
|
45
|
-
def badges
|
46
|
-
obtain_key @response, 'badges' if @response
|
47
|
-
end
|
48
|
-
|
49
|
-
def courses_completed
|
50
|
-
courses = obtain_key @response, 'courses' if @response
|
51
|
-
obtain_key courses, 'completed' if !courses.blank?
|
52
|
-
end
|
53
|
-
|
54
|
-
def courses_in_progress
|
55
|
-
courses = obtain_key @response, 'courses' if @response
|
56
|
-
obtain_key courses, 'in_progress' if !courses.blank?
|
57
|
-
end
|
58
|
-
|
59
|
-
def member_since
|
60
|
-
user = obtain_key @response, 'user' if @response
|
61
|
-
obtain_key user, 'member_since' if !user.blank?
|
62
|
-
end
|
63
|
-
|
64
|
-
def total_score
|
65
|
-
user = obtain_key @response, 'user' if @response
|
66
|
-
obtain_key user, 'total_score' if !user.blank?
|
37
|
+
class CodeSchool < Request
|
38
|
+
def initialize(user_id, base_url='http://www.codeschool.com/users/')
|
39
|
+
super(user_id, base_url)
|
67
40
|
end
|
68
41
|
end
|
69
42
|
|
70
|
-
class Treehouse <
|
71
|
-
def initialize(user_id)
|
72
|
-
super(user_id,
|
73
|
-
end
|
74
|
-
|
75
|
-
def avatar_url
|
76
|
-
obtain_key @response, 'gravatar_url' if @response
|
77
|
-
end
|
78
|
-
|
79
|
-
def badges
|
80
|
-
obtain_key @response, 'badges' if @response
|
81
|
-
end
|
82
|
-
|
83
|
-
def full_name
|
84
|
-
obtain_key @response, 'name' if @response
|
85
|
-
end
|
86
|
-
|
87
|
-
def points
|
88
|
-
obtain_key @response, 'points' if @response
|
89
|
-
end
|
90
|
-
|
91
|
-
def points_android
|
92
|
-
points = obtain_key @response, 'points' if @response
|
93
|
-
obtain_key points, 'android' if !points.blank?
|
94
|
-
end
|
95
|
-
|
96
|
-
def points_business
|
97
|
-
points = obtain_key @response, 'points' if @response
|
98
|
-
obtain_key points, 'business' if !points.blank?
|
99
|
-
end
|
100
|
-
|
101
|
-
def points_css
|
102
|
-
points = obtain_key @response, 'points' if @response
|
103
|
-
obtain_key points, 'css' if !points.blank?
|
104
|
-
end
|
105
|
-
|
106
|
-
def points_design
|
107
|
-
points = obtain_key @response, 'points' if @response
|
108
|
-
obtain_key points, 'design' if !points.blank?
|
109
|
-
end
|
110
|
-
|
111
|
-
def points_dev_tools
|
112
|
-
points = obtain_key @response, 'points' if @response
|
113
|
-
obtain_key points, 'dev tools' if !points.blank?
|
114
|
-
end
|
115
|
-
|
116
|
-
def points_html
|
117
|
-
points = obtain_key @response, 'points' if @response
|
118
|
-
obtain_key points, 'html' if !points.blank?
|
119
|
-
end
|
120
|
-
|
121
|
-
def points_ios
|
122
|
-
points = obtain_key @response, 'points' if @response
|
123
|
-
obtain_key points, 'ios' if !points.blank?
|
124
|
-
end
|
125
|
-
|
126
|
-
def points_javascript
|
127
|
-
points = obtain_key @response, 'points' if @response
|
128
|
-
obtain_key points, 'javascript' if !points.blank?
|
129
|
-
end
|
130
|
-
|
131
|
-
def points_php
|
132
|
-
points = obtain_key @response, 'points' if @response
|
133
|
-
obtain_key points, 'php' if !points.blank?
|
134
|
-
end
|
135
|
-
|
136
|
-
def points_ruby
|
137
|
-
points = obtain_key @response, 'points' if @response
|
138
|
-
obtain_key points, 'ruby' if !points.blank?
|
139
|
-
end
|
140
|
-
|
141
|
-
def points_total
|
142
|
-
points = obtain_key @response, 'points' if @response
|
143
|
-
obtain_key points, 'total' if !points.blank?
|
144
|
-
end
|
145
|
-
|
146
|
-
def points_wordpress
|
147
|
-
points = obtain_key @response, 'points' if @response
|
148
|
-
obtain_key points, 'wordpress' if !points.blank?
|
43
|
+
class Treehouse < Request
|
44
|
+
def initialize(user_id, base_url='http://teamtreehouse.com/')
|
45
|
+
super(user_id, base_url)
|
149
46
|
end
|
150
47
|
end
|
151
48
|
end
|
data/lib/badges/version.rb
CHANGED
data/lib/badges/view_helpers.rb
CHANGED
@@ -3,25 +3,25 @@ require 'markaby'
|
|
3
3
|
module Badges
|
4
4
|
module ViewHelpers
|
5
5
|
def list_badges(obj, options={})
|
6
|
-
return
|
6
|
+
return unless obj.code == 200
|
7
7
|
|
8
8
|
# Code School
|
9
|
-
|
9
|
+
|
10
10
|
# badges
|
11
|
-
|
11
|
+
unless options[:codeschool_badges].blank? || obj.body.badges.blank?
|
12
12
|
html = Markaby::Builder.new
|
13
13
|
|
14
14
|
html.send :ul, ({id: options[:id]} if options[:id]), ({class: options[:class]} if options[:class]) do
|
15
|
-
obj.badges.each do |course|
|
15
|
+
obj.body.badges.each do |course|
|
16
16
|
li do
|
17
17
|
if options[:codeschool_badges][:link]
|
18
|
-
a href: course
|
19
|
-
img src: course
|
20
|
-
span course
|
18
|
+
a href: course.course_url do
|
19
|
+
img src: course.badge if options[:codeschool_badges][:image]
|
20
|
+
span course.name if options[:codeschool_badges][:text]
|
21
21
|
end
|
22
22
|
else
|
23
|
-
img src: course
|
24
|
-
span course
|
23
|
+
img src: course.badge if options[:codeschool_badges][:image]
|
24
|
+
span course.name if options[:codeschool_badges][:text]
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
@@ -31,20 +31,20 @@ module Badges
|
|
31
31
|
end
|
32
32
|
|
33
33
|
# courses completed
|
34
|
-
|
34
|
+
unless options[:codeschool_courses_completed].blank? || obj.body.courses.completed.blank?
|
35
35
|
html = Markaby::Builder.new
|
36
36
|
|
37
37
|
html.send :ul, ({id: options[:id]} if options[:id]), ({class: options[:class]} if options[:class]) do
|
38
|
-
obj.
|
38
|
+
obj.body.courses.completed.each do |course|
|
39
39
|
li do
|
40
40
|
if options[:codeschool_courses_completed][:link]
|
41
|
-
a href: course
|
42
|
-
img src: course
|
43
|
-
span course
|
41
|
+
a href: course.url do
|
42
|
+
img src: course.badge if options[:codeschool_courses_completed][:image]
|
43
|
+
span course.title if options[:codeschool_courses_completed][:text]
|
44
44
|
end
|
45
45
|
else
|
46
|
-
img src: course
|
47
|
-
span course
|
46
|
+
img src: course.badge if options[:codeschool_courses_completed][:image]
|
47
|
+
span course.title if options[:codeschool_courses_completed][:text]
|
48
48
|
end
|
49
49
|
end
|
50
50
|
end
|
@@ -54,20 +54,20 @@ module Badges
|
|
54
54
|
end
|
55
55
|
|
56
56
|
# courses in progress
|
57
|
-
|
57
|
+
unless options[:codeschool_courses_in_progress].blank? || obj.body.courses.in_progress.blank?
|
58
58
|
html = Markaby::Builder.new
|
59
59
|
|
60
60
|
html.send :ul, ({id: options[:id]} if options[:id]), ({class: options[:class]} if options[:class]) do
|
61
|
-
obj.
|
61
|
+
obj.body.courses.in_progress.each do |course|
|
62
62
|
li do
|
63
63
|
if options[:codeschool_courses_in_progress][:link]
|
64
|
-
a href: course
|
65
|
-
img src: course
|
66
|
-
span course
|
64
|
+
a href: course.url do
|
65
|
+
img src: course.badge if options[:codeschool_courses_in_progress][:image]
|
66
|
+
span course.title if options[:codeschool_courses_in_progress][:text]
|
67
67
|
end
|
68
68
|
else
|
69
|
-
img src: course
|
70
|
-
span course
|
69
|
+
img src: course.badge if options[:codeschool_courses_in_progress][:image]
|
70
|
+
span course.title if options[:codeschool_courses_in_progress][:text]
|
71
71
|
end
|
72
72
|
end
|
73
73
|
end
|
@@ -79,20 +79,20 @@ module Badges
|
|
79
79
|
# Treehouse
|
80
80
|
|
81
81
|
# badges
|
82
|
-
|
82
|
+
unless options[:treehouse_badges].blank? || obj.body.badges.blank?
|
83
83
|
html = Markaby::Builder.new
|
84
84
|
|
85
85
|
html.send :ul, ({id: options[:id]} if options[:id]), ({class: options[:class]} if options[:class]) do
|
86
|
-
obj.badges.each do |course|
|
86
|
+
obj.body.badges.each do |course|
|
87
87
|
li do
|
88
88
|
if options[:treehouse_badges][:link]
|
89
|
-
a href: course
|
90
|
-
img src: course
|
91
|
-
span course
|
89
|
+
a href: course.url do
|
90
|
+
img src: course.icon_url if options[:treehouse_badges][:image]
|
91
|
+
span course.name if options[:treehouse_badges][:text]
|
92
92
|
end
|
93
93
|
else
|
94
|
-
img src: course
|
95
|
-
span course
|
94
|
+
img src: course.icon_url if options[:treehouse_badges][:image]
|
95
|
+
span course.name if options[:treehouse_badges][:text]
|
96
96
|
end
|
97
97
|
end
|
98
98
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: badges
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Keil Miller Jr
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dish
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: markaby
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|