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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 94ba37e390392c87e71b7e0e7c149fbf5abefcda
4
- data.tar.gz: 713f593501a9d8413debf985c3a4429ce0fcc8d1
3
+ metadata.gz: 83abc324aa59bc81fa5a5912c571241635939a07
4
+ data.tar.gz: f8f5c32225e1dcf6475782a66f6b74dc272eda3d
5
5
  SHA512:
6
- metadata.gz: 55447da027202aa56d5650054071ef5650be9e21a6a0a0a5da7e950d6f4b6d2474ed1483defdfa270cee33967ae87be398a264102a11ff882069630dfca6b7fa
7
- data.tar.gz: c9f49465423566edf0a80df3a4008689283dc32f21acb4e5d1dcd6266f77599cafa2914a9eb939fe1855603dfb733554f222bbc198a248ae39e36cd230b32076
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 example:
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
- codeschool.response
37
- => nil # if false username
38
-
39
- codeschool.username
40
- => "username"
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.avatar_url
46
- => "http://example.com/avatar.jpg"
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.courses_in_progress
55
- => [{"title"=>"Course", "url"=>"http://example.com", "badge"=>"http://example.com/badge.png"}]
55
+ codeschool.code
56
+ => 200
56
57
 
57
- codeschool.member_since
58
- => "2011-03-22T06:54:21Z"
58
+ codeschool.message
59
+ => "OK"
59
60
 
60
- codeschool.total_score
61
- => "<b>114225</b>"
61
+ Any error in the request will yield a message in the console or log.
62
62
 
63
- Team Treehouse example:
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
- treehouse = Badges::Treehouse.new('profile_name')
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
- treehouse.badges
80
- => [{"id"=>49, "name"=>"Badge", "url"=>"http://example.com", "icon_url"=>"http://example.com/badge.png", "earned_date"=>"2012-09-11T18:41:13Z", "courses"=>[]}]
81
-
82
- treehouse.full_name
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
- ## Rails helpers
76
+ #### Badges::Treehouse
124
77
 
125
- Create new badge object in your controller.
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 class methods directly, or the list_badges helper to create a list of badges. Params left out are assumed to be false.
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.avatar_url %>
140
- <%= list_badges @codeschool, codeschool_badges: {image: true, link: true, text: true}, id: 'test', class: 'test' %>
141
- <%= list_badges @codeschool, codeschool_courses_completed: {image: true, link: true, text: true}, id: 'test', class: 'test' %>
142
- <%= list_badges @codeschool, codeschool_courses_in_progress: {image: true, link: true, text: true}, id: 'test', class: 'test' %>
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.avatar_url %>
145
- <%= list_badges @treehouse, treehouse_badges: {image: true, link: true, text: true}, id: 'test', class: 'test' %>
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
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_dependency 'activesupport'
24
24
  spec.add_dependency 'httparty'
25
+ spec.add_dependency 'dish'
25
26
  spec.add_dependency 'markaby'
26
27
 
27
28
  spec.add_development_dependency 'bundler', '~> 1.3'
@@ -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 Badge
7
+ class Request
7
8
  include HTTParty
8
- attr_reader :user_id, :profile_url, :response
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
- @response = get(profile_url + '.json')
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 get(url)
19
- begin
20
- JSON.parse HTTParty.get(url).response.body
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 < Badge
34
- def initialize(user_id)
35
- super(user_id, 'http://www.codeschool.com/users/')
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 < Badge
71
- def initialize(user_id)
72
- super(user_id, 'http://teamtreehouse.com/')
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
@@ -1,3 +1,3 @@
1
1
  module Badges
2
- VERSION = '1.0.0'
2
+ VERSION = '2.0.0'
3
3
  end
@@ -3,25 +3,25 @@ require 'markaby'
3
3
  module Badges
4
4
  module ViewHelpers
5
5
  def list_badges(obj, options={})
6
- return if obj.response.blank?
6
+ return unless obj.code == 200
7
7
 
8
8
  # Code School
9
-
9
+
10
10
  # badges
11
- if !options[:codeschool_badges].blank? && !obj.badges.blank?
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['course_url'] do
19
- img src: course['badge'] if options[:codeschool_badges][:image]
20
- span course['name'] if options[:codeschool_badges][:text]
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['badge'] if options[:codeschool_badges][:image]
24
- span course['name'] if options[:codeschool_badges][:text]
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
- if !options[:codeschool_courses_completed].blank? && !obj.courses_completed.blank?
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.courses_completed.each do |course|
38
+ obj.body.courses.completed.each do |course|
39
39
  li do
40
40
  if options[:codeschool_courses_completed][:link]
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]
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['badge'] if options[:codeschool_courses_completed][:image]
47
- span course['title'] if options[:codeschool_courses_completed][:text]
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
- if !options[:codeschool_courses_in_progress].blank? && !obj.courses_in_progress.blank?
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.courses_in_progress.each do |course|
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['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]
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['badge'] if options[:codeschool_courses_in_progress][:image]
70
- span course['title'] if options[:codeschool_courses_in_progress][:text]
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
- if !options[:treehouse_badges].blank? && !obj.badges.blank?
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['url'] do
90
- img src: course['icon_url'] if options[:treehouse_badges][:image]
91
- span course['name'] if options[:treehouse_badges][:text]
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['icon_url'] if options[:treehouse_badges][:image]
95
- span course['name'] if options[:treehouse_badges][:text]
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: 1.0.0
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-01-15 00:00:00.000000000 Z
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