coder_wally 0.0.7 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/coder_wally.rb +2 -0
- data/lib/coder_wally/api.rb +67 -0
- data/lib/coder_wally/builder.rb +33 -0
- data/lib/coder_wally/client.rb +10 -42
- data/lib/coder_wally/version.rb +1 -1
- data/test/coder_wally_test.rb +15 -5
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec3f117479a41d224184d23682f03c5756e9c312
|
4
|
+
data.tar.gz: b3902c02004878ad333b805bbbdae7022f3e451f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24434fbdf0747f6b3ccb0173ded350e5b41649604f8d01ccf320da0c6112f9cf770328a1cce5e00c8928db4874eb61be64757e8e1d7edeaa75e1f8b62840a0fa
|
7
|
+
data.tar.gz: 65372cdbe3b2eb38bea2500af500253ee1a643beda3547c050882776fcf84d30f5400d4348765eea5bfec44e74c958088af5697059d9f6a2a2c74c29aee59b72
|
data/lib/coder_wally.rb
CHANGED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module CoderWally
|
5
|
+
# API Class
|
6
|
+
class API
|
7
|
+
# Fetch data from CoderWall
|
8
|
+
def fetch(username)
|
9
|
+
uri = uri_for_user(username)
|
10
|
+
json = send_request(uri)
|
11
|
+
|
12
|
+
begin
|
13
|
+
JSON.parse(json.read)
|
14
|
+
rescue JSON::ParserError => error
|
15
|
+
raise InvalidJson, 'Received invalid json in response'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
# Dispatch the request
|
21
|
+
def send_request(url)
|
22
|
+
begin
|
23
|
+
open(url)
|
24
|
+
rescue OpenURI::HTTPError => error
|
25
|
+
handle_user_not_found_error(error)
|
26
|
+
handle_server_error(error)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Parse status code from error
|
31
|
+
def get_status_code(error)
|
32
|
+
error.io.status[0]
|
33
|
+
end
|
34
|
+
|
35
|
+
# Raise server error
|
36
|
+
def handle_server_error(error)
|
37
|
+
raise ServerError, 'Server error' if get_status_code(error) == '500'
|
38
|
+
end
|
39
|
+
|
40
|
+
# Raise user not found error
|
41
|
+
def handle_user_not_found_error(error)
|
42
|
+
raise UserNotFoundError, 'User not found' if get_status_code(error) == '404'
|
43
|
+
end
|
44
|
+
|
45
|
+
# Build user URI from username and api url
|
46
|
+
def uri_for_user(username)
|
47
|
+
raise(ArgumentError, 'Please provide a username') if username.empty?
|
48
|
+
|
49
|
+
URI.parse(api_url % username)
|
50
|
+
end
|
51
|
+
|
52
|
+
# The URL of the API we'll use.
|
53
|
+
def api_url
|
54
|
+
'https://coderwall.com/%s.json'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
# Handles user not found exception
|
59
|
+
class UserNotFoundError < StandardError
|
60
|
+
end
|
61
|
+
|
62
|
+
# Handles server exception
|
63
|
+
class ServerError < StandardError
|
64
|
+
end
|
65
|
+
# Handles bad JSON
|
66
|
+
class InvalidJson < StandardError
|
67
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module CoderWally
|
2
|
+
# Builds the CoderWall object from the response
|
3
|
+
class Builder
|
4
|
+
# Instantiate class
|
5
|
+
def initialize
|
6
|
+
end
|
7
|
+
|
8
|
+
# parse badges from data
|
9
|
+
def parse_badges(data)
|
10
|
+
data['badges'].map { |badge| Badge.new(badge) } if data['badges']
|
11
|
+
end
|
12
|
+
|
13
|
+
# parse account information from data
|
14
|
+
def parse_accounts(data)
|
15
|
+
Account.new(data['accounts']) if data['accounts']
|
16
|
+
end
|
17
|
+
|
18
|
+
# parse user information from data
|
19
|
+
def parse_user(data)
|
20
|
+
User.new(data['name'], data['username'],
|
21
|
+
data['location'], data['team'], data['endorsements'])
|
22
|
+
end
|
23
|
+
|
24
|
+
# build CoderWall object from API response
|
25
|
+
def build response
|
26
|
+
badges = parse_badges(response)
|
27
|
+
accounts = parse_accounts(response)
|
28
|
+
user = parse_user(response)
|
29
|
+
|
30
|
+
CoderWall.new badges, user, accounts
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/coder_wally/client.rb
CHANGED
@@ -1,66 +1,34 @@
|
|
1
|
-
require 'open-uri'
|
2
|
-
require 'json'
|
3
|
-
|
4
1
|
module CoderWally
|
5
2
|
# Client to access the API
|
6
3
|
class Client
|
7
|
-
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
# Build user URI from username and apii url
|
13
|
-
def uri_for_user username
|
14
|
-
raise(ArgumentError, 'Please provide a username') if username.empty?
|
15
|
-
|
16
|
-
URI.parse(api_url % username)
|
17
|
-
end
|
18
|
-
|
19
|
-
# Dispatch the request
|
20
|
-
def send_request url
|
21
|
-
begin
|
22
|
-
open(url)
|
23
|
-
rescue OpenURI::HTTPError => error
|
24
|
-
raise UserNotFoundError, 'User not found' if error.io.status[0] == '404'
|
25
|
-
raise ServerError, 'Server error' if error.io.status[0] == '500'
|
26
|
-
end
|
4
|
+
# Instantiate class
|
5
|
+
def initialize
|
6
|
+
@api = API.new
|
7
|
+
@builder = Builder.new
|
27
8
|
end
|
28
9
|
|
29
10
|
# Get badges for given user and return has collection of `Badge`s
|
30
11
|
def get_badges_for username
|
31
|
-
coder_wall =
|
12
|
+
coder_wall = build_coder_wall_from_response(username)
|
32
13
|
coder_wall.badges
|
33
14
|
end
|
34
15
|
|
35
16
|
# Get user details for given user and return a `User` object
|
36
17
|
def get_details_for username
|
37
|
-
coder_wall =
|
18
|
+
coder_wall = build_coder_wall_from_response(username)
|
38
19
|
coder_wall.user
|
39
20
|
end
|
40
21
|
|
41
22
|
# Get all the information available for a given user, returns a `CoderWall` object
|
42
23
|
def get_everything_for username
|
43
|
-
|
24
|
+
build_coder_wall_from_response(username)
|
44
25
|
end
|
45
26
|
|
46
27
|
# Builds a CoderWall object
|
47
|
-
def
|
48
|
-
json_response =
|
28
|
+
def build_coder_wall_from_response(username)
|
29
|
+
json_response = @api.fetch(username)
|
49
30
|
|
50
|
-
|
51
|
-
accounts = Account.new(json_response['accounts'])
|
52
|
-
user = User.new(json_response['name'], json_response['username'],
|
53
|
-
json_response['location'], json_response['team'], json_response['endorsements'])
|
54
|
-
|
55
|
-
CoderWall.new badges, user, accounts
|
31
|
+
@builder.build(json_response)
|
56
32
|
end
|
57
33
|
end
|
58
34
|
end
|
59
|
-
|
60
|
-
# Handles user not found exception
|
61
|
-
class UserNotFoundError < StandardError
|
62
|
-
end
|
63
|
-
|
64
|
-
# Handles server exception
|
65
|
-
class ServerError < StandardError
|
66
|
-
end
|
data/lib/coder_wally/version.rb
CHANGED
data/test/coder_wally_test.rb
CHANGED
@@ -6,10 +6,6 @@ describe 'Coder Wally' do
|
|
6
6
|
@client = CoderWally::Client.new
|
7
7
|
end
|
8
8
|
|
9
|
-
it 'has a default api url' do
|
10
|
-
'https://coderwall.com/%s.json'.must_equal @client.api_url
|
11
|
-
end
|
12
|
-
|
13
9
|
describe 'user badges' do
|
14
10
|
describe 'valid user' do
|
15
11
|
before do
|
@@ -64,11 +60,25 @@ describe 'Coder Wally' do
|
|
64
60
|
to_return(:status => 500, :body => server_error, :headers => {})
|
65
61
|
end
|
66
62
|
|
67
|
-
it 'throws a ServerError when the
|
63
|
+
it 'throws a ServerError when the server response is 500' do
|
68
64
|
err = -> { @client.get_badges_for('me') }.must_raise ServerError
|
69
65
|
err.message.must_match /Server error/
|
70
66
|
end
|
71
67
|
end
|
68
|
+
|
69
|
+
describe 'bad data' do
|
70
|
+
before do
|
71
|
+
bad_data = open(File.expand_path(File.dirname(__FILE__) + '/./fixtures/bad.json')).read
|
72
|
+
stub_request(:get, 'https://coderwall.com/me.json').
|
73
|
+
with(:headers => {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
74
|
+
to_return(:status => 200, :body => bad_data, :headers => {})
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'throws a InvalidJson exception when bad JSON is returned' do
|
78
|
+
err = -> { @client.get_badges_for('me') }.must_raise InvalidJson
|
79
|
+
err.message.must_match /invalid json/
|
80
|
+
end
|
81
|
+
end
|
72
82
|
end
|
73
83
|
|
74
84
|
describe 'user details' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coder_wally
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Greg Stewart
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,7 +86,9 @@ files:
|
|
86
86
|
- coder_wally.gemspec
|
87
87
|
- lib/coder_wally.rb
|
88
88
|
- lib/coder_wally/account.rb
|
89
|
+
- lib/coder_wally/api.rb
|
89
90
|
- lib/coder_wally/badge.rb
|
91
|
+
- lib/coder_wally/builder.rb
|
90
92
|
- lib/coder_wally/client.rb
|
91
93
|
- lib/coder_wally/coder_wall.rb
|
92
94
|
- lib/coder_wally/user.rb
|