coder_wally 0.0.7 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: defc173143b93f0b93406ef1fbde0900e59cdd76
4
- data.tar.gz: ee23f5c2797c6b113101b36eb5d5bcfa5be40186
3
+ metadata.gz: ec3f117479a41d224184d23682f03c5756e9c312
4
+ data.tar.gz: b3902c02004878ad333b805bbbdae7022f3e451f
5
5
  SHA512:
6
- metadata.gz: 2152878afffe135df1d5c128098ff2237a8b261d86884d443f01304986742d9f5fba42272b28db9bc9d77770dfb996b2ffbefa5c604d82c44ff33d75f1de6ec9
7
- data.tar.gz: 9a683a6c26b92ed98881dd96f3193fd2c843d5bf205909a4d83a2659a6a9ad1c80e07bbca9c3ced983b6bd2fe4bce68674a57ccac9b7dcb786e10d2163bc8a46
6
+ metadata.gz: 24434fbdf0747f6b3ccb0173ded350e5b41649604f8d01ccf320da0c6112f9cf770328a1cce5e00c8928db4874eb61be64757e8e1d7edeaa75e1f8b62840a0fa
7
+ data.tar.gz: 65372cdbe3b2eb38bea2500af500253ee1a643beda3547c050882776fcf84d30f5400d4348765eea5bfec44e74c958088af5697059d9f6a2a2c74c29aee59b72
@@ -3,5 +3,7 @@ require 'coder_wally/badge'
3
3
  require 'coder_wally/user'
4
4
  require 'coder_wally/account'
5
5
  require 'coder_wally/coder_wall'
6
+ require 'coder_wally/api'
7
+ require 'coder_wally/builder'
6
8
  require 'coder_wally/client'
7
9
 
@@ -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
@@ -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
- # The URL of the API we'll use.
8
- def api_url
9
- 'https://coderwall.com/%s.json'
10
- end
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 = build_coder_wall(username)
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 = build_coder_wall(username)
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
- build_coder_wall(username)
24
+ build_coder_wall_from_response(username)
44
25
  end
45
26
 
46
27
  # Builds a CoderWall object
47
- def build_coder_wall(username)
48
- json_response = JSON.load(send_request(uri_for_user(username)))
28
+ def build_coder_wall_from_response(username)
29
+ json_response = @api.fetch(username)
49
30
 
50
- badges = json_response['badges'].map { |badge| Badge.new(badge) }
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
@@ -1,4 +1,4 @@
1
1
  module CoderWally
2
2
  # The current version of CoderWally.
3
- VERSION = '0.0.7'
3
+ VERSION = '0.1.0'
4
4
  end
@@ -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 user is not found' do
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.7
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-02 00:00:00.000000000 Z
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