coder_wally 0.1.1 → 0.1.2

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: 09158bbcb126bc9fbb9fcfd4eaff98d646017548
4
- data.tar.gz: 7dc0e8d397038d0f315263dcd530a7ace464775d
3
+ metadata.gz: 1ce08332a669f0fab248e145033366fab6f89a89
4
+ data.tar.gz: b4cc4a66611195652695244724152dfddbfc45c8
5
5
  SHA512:
6
- metadata.gz: e0b7f1da6275413792bd7d23d20d430c803d191ce6643b0d00eba7c7b03ba72e06bef77a546bf06be64be0582591f1209c0eae552bd344c348cfe48eb4244e22
7
- data.tar.gz: cf18c3a4a70e483dabb092eca36a8f5ba7005fb633db642ffd139d81c5579f5fa662044a7225d27b484cc5c98962d8bb7f91fa9442bb19390f70ce65abe4c7c0
6
+ metadata.gz: 6837d0ad987b1689ec808bdea5f1a076b633d253bc72246d87fe5ef51dcdcb27ab49b174c71adc00b247c61dda9ab55dc1c12b6430758c512faa677c70527882
7
+ data.tar.gz: c17ddb445161f3c4bce8b491a6f8ea7e9d46ddd1a6038ca37da31bcf5c13cb406c85239b5af35945305f0e8afd1237f43190cc68bcc9d292ba5a528e9e0ff2f6
data/README.md CHANGED
@@ -1,12 +1,13 @@
1
1
  # CoderWally
2
2
 
3
- [![Build Status](https://circleci.com/gh/gregstewart/coder_wally.svg?style=badge)](https://circleci.com/gh/gregstewart/coder_wally)
3
+ [![Build Status](https://circleci.com/gh/gregstewart/coder_wally.svg?style=badge)](https://circleci.com/gh/gregstewart/coder_wally)
4
4
 
5
5
  [![Gem Version](https://badge.fury.io/rb/coder_wally.svg)](http://badge.fury.io/rb/coder_wally)
6
6
  [![Coverage Status](https://coveralls.io/repos/gregstewart/coder_wally/badge.svg)](https://coveralls.io/r/gregstewart/coder_wally)
7
7
  [![Code Climate](https://codeclimate.com/github/gregstewart/coder_wally/badges/gpa.svg)](https://codeclimate.com/github/gregstewart/coder_wally)
8
8
  [![Dependency Status](https://gemnasium.com/gregstewart/coder_wally.svg)](https://gemnasium.com/gregstewart/coder_wally)
9
9
  [![Inline docs](https://inch-ci.org/github/gregstewart/coder_wally.svg?branch=master)](https://inch-ci.org/github/gregstewart/coder_wally)
10
+ [![coder_wally API Documentation](https://www.omniref.com/ruby/gems/coder_wally.png)](https://www.omniref.com/ruby/gems/coder_wally)
10
11
 
11
12
  A very very simple Gem to fetch user badges from Coder Wall
12
13
 
data/coder_wally.gemspec CHANGED
@@ -4,23 +4,23 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'coder_wally/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "coder_wally"
7
+ spec.name = 'coder_wally'
8
8
  spec.version = CoderWally::VERSION
9
- spec.authors = ["Greg Stewart"]
10
- spec.email = ["gregs@tcias.co.uk"]
9
+ spec.authors = ['Greg Stewart']
10
+ spec.email = ['gregs@tcias.co.uk']
11
11
  spec.summary = %q{A simple gem to fetch user badges from Coder Wall}
12
12
  spec.description = %q{A simple gem to fetch user badges from Coder Wall}
13
- spec.homepage = "https://github.com/gregstewart/coder_wally"
14
- spec.license = "MIT"
13
+ spec.homepage = 'https://github.com/gregstewart/coder_wally'
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.7"
22
- spec.add_development_dependency "rake", "~> 10.0"
23
- spec.add_development_dependency "webmock", "~>1.20.4"
24
- spec.add_development_dependency "coveralls"
25
- spec.add_development_dependency "rubocop"
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'webmock', '~>1.20.4'
24
+ spec.add_development_dependency 'coveralls', '~>0.7.9'
25
+ spec.add_development_dependency 'rubocop', '~>0.29.0'
26
26
  end
@@ -5,9 +5,17 @@ module CoderWally
5
5
  # Dynamically create account properties
6
6
  def initialize(collection_of_accounts)
7
7
  collection_of_accounts.each do |account, value|
8
- singleton_class.class_eval { attr_accessor "#{account}" }
9
- instance_variable_set("@#{account}", value)
8
+ create_accessor account
9
+ set_accessor_value account, value
10
10
  end
11
11
  end
12
+ # create the attribute accessor
13
+ def create_accessor(name)
14
+ singleton_class.class_eval { attr_accessor "#{name}" }
15
+ end
16
+ # assign the value to the accessor
17
+ def set_accessor_value(name, value)
18
+ instance_variable_set("@#{name}", value)
19
+ end
12
20
  end
13
21
  end
@@ -4,15 +4,22 @@ require 'json'
4
4
  module CoderWally
5
5
  # API Class
6
6
  class API
7
+ attr_reader :response
8
+ def initialize
9
+ @response = {}
10
+ end
11
+
7
12
  # Fetch data from CoderWall
8
13
  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
15
- raise InvalidJson, 'Received invalid json in response'
14
+ @response.fetch(username) do
15
+ uri = uri_for_user(username)
16
+ json = send_request(uri)
17
+
18
+ begin
19
+ @response[username] = JSON.parse(json.read)
20
+ rescue JSON::ParserError
21
+ raise InvalidJson, 'Received invalid json in response'
22
+ end
16
23
  end
17
24
  end
18
25
 
@@ -22,23 +29,7 @@ module CoderWally
22
29
  def send_request(url)
23
30
  open(url)
24
31
  rescue OpenURI::HTTPError => error
25
- handle_user_not_found_error(error)
26
- handle_server_error(error)
27
- end
28
-
29
- # Parse status code from error
30
- def get_status_code(error)
31
- error.io.status[0]
32
- end
33
-
34
- # Raise server error
35
- def handle_server_error(error)
36
- fail ServerError, 'Server error' if get_status_code(error) == '500'
37
- end
38
-
39
- # Raise user not found error
40
- def handle_user_not_found_error(error)
41
- fail UserNotFoundError, 'User not found' if get_status_code(error) == '404'
32
+ ExceptionHandler.new(error)
42
33
  end
43
34
 
44
35
  # Build user URI from username and api url
@@ -55,13 +46,6 @@ module CoderWally
55
46
  end
56
47
  end
57
48
 
58
- # Handles user not found exception
59
- class UserNotFoundError < StandardError
60
- end
61
-
62
- # Handles server exception
63
- class ServerError < StandardError
64
- end
65
49
  # Handles bad JSON
66
50
  class InvalidJson < StandardError
67
51
  end
@@ -6,25 +6,29 @@ module CoderWally
6
6
  end
7
7
 
8
8
  # parse badges from data
9
- def parse_badges(data)
10
- data['badges'].map { |badge| Badge.new(badge) } if data['badges']
9
+ def parse_badges(badges)
10
+ badges.map { |badge| create_new_badge(badge) } if badges
11
+ end
12
+
13
+ # create a new badge object
14
+ def create_new_badge(badge)
15
+ Badge.new(badge)
11
16
  end
12
17
 
13
18
  # parse account information from data
14
- def parse_accounts(data)
15
- Account.new(data['accounts']) if data['accounts']
19
+ def parse_accounts(accounts)
20
+ Account.new(accounts) if accounts
16
21
  end
17
22
 
18
23
  # parse user information from data
19
24
  def parse_user(data)
20
- User.new(data['name'], data['username'],
21
- data['location'], data['team'], data['endorsements'])
25
+ User.new(data)
22
26
  end
23
27
 
24
28
  # build CoderWall object from API response
25
29
  def build(response)
26
- badges = parse_badges(response)
27
- accounts = parse_accounts(response)
30
+ badges = parse_badges(response['badges'])
31
+ accounts = parse_accounts(response['accounts'])
28
32
  user = parse_user(response)
29
33
 
30
34
  CoderWall.new badges, user, accounts
@@ -0,0 +1,18 @@
1
+ module CoderWally
2
+ # Raises errors from the service request
3
+ class ExceptionHandler
4
+ # Initialize with the error object
5
+ def initialize(error)
6
+ status_code = StatusCodeFromError.new(error).status_code
7
+ fail ServerError, 'Server error' if status_code == '500'
8
+ fail UserNotFoundError, 'User not found' if status_code == '404'
9
+ end
10
+ end
11
+ end
12
+ # Handles user not found exception
13
+ class UserNotFoundError < StandardError
14
+ end
15
+
16
+ # Handles server exception
17
+ class ServerError < StandardError
18
+ end
@@ -0,0 +1,30 @@
1
+ # All code in the gem is namespaced under this module.
2
+ module CoderWally
3
+ # return status code from error object
4
+ class StatusCodeFromError
5
+ # Initialise object
6
+ def initialize(error)
7
+ @error = error
8
+ end
9
+
10
+ # Get the status code
11
+ def status_code
12
+ @error.io.status[0] if error? && io? && status_code?
13
+ end
14
+
15
+ # Is there an error object?
16
+ def error?
17
+ true if @error
18
+ end
19
+
20
+ # Is there an io object?
21
+ def io?
22
+ true if @error.io
23
+ end
24
+
25
+ # Is there a status?
26
+ def status_code?
27
+ true if @error.io.status
28
+ end
29
+ end
30
+ end
@@ -6,12 +6,12 @@ module CoderWally
6
6
  attr_reader :name, :username, :location, :team, :endorsements
7
7
 
8
8
  # Initialise object with a hash of values
9
- def initialize(name, username, location, team, endorsements)
10
- @name = name
11
- @username = username
12
- @location = location
13
- @team = team
14
- @endorsements = endorsements
9
+ def initialize(data)
10
+ @name = data['name']
11
+ @username = data['username']
12
+ @location = data['location']
13
+ @team = data['team']
14
+ @endorsements = data['endorsements']
15
15
  end
16
16
  end
17
17
  end
@@ -1,5 +1,5 @@
1
1
  # All code in the gem is namespaced under this module.
2
2
  module CoderWally
3
3
  # The current version of CoderWally.
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  end
data/lib/coder_wally.rb CHANGED
@@ -4,5 +4,7 @@ require 'coder_wally/user'
4
4
  require 'coder_wally/account'
5
5
  require 'coder_wally/coder_wall'
6
6
  require 'coder_wally/api'
7
+ require 'coder_wally/status_code_from_error'
8
+ require 'coder_wally/exception_handler'
7
9
  require 'coder_wally/builder'
8
10
  require 'coder_wally/client'
data/test/api_test.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'minitest/autorun'
2
+ require 'coder_wally/api'
3
+
4
+ describe 'api' do
5
+ describe 'memoize result' do
6
+ before do
7
+ @accept_encoding = 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3'
8
+ @success_fixture = File.dirname(__FILE__) + '/./fixtures/200.json'
9
+ @success_response = open(File.expand_path(@success_fixture)).read
10
+
11
+ stub_request(:get, 'https://coderwall.com/me.json')
12
+ .with(headers: { 'Accept' => '*/*',
13
+ 'Accept-Encoding' => @accept_encoding,
14
+ 'User-Agent' => 'Ruby' })
15
+ .to_return(status: 200, body: @success_response, headers: {})
16
+ end
17
+
18
+ it 'calls the send_request method only the first time for any given username' do
19
+ api = CoderWally::API.new
20
+
21
+ api.fetch('me')
22
+ api.fetch('me')
23
+
24
+ api.response.count.must_equal 1
25
+
26
+ stub_request(:get, 'https://coderwall.com/foo.json')
27
+ .with(headers: { 'Accept' => '*/*',
28
+ 'Accept-Encoding' => @accept_encoding,
29
+ 'User-Agent' => 'Ruby' })
30
+ .to_return(status: 200, body: @success_response, headers: {})
31
+
32
+ api.fetch('foo')
33
+ api.response.count.must_equal 2
34
+ end
35
+
36
+
37
+ end
38
+ end
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.1.1
4
+ version: 0.1.2
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-06 00:00:00.000000000 Z
11
+ date: 2015-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,30 +56,30 @@ dependencies:
56
56
  name: coveralls
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 0.7.9
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: 0.7.9
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rubocop
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: 0.29.0
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: 0.29.0
83
83
  description: A simple gem to fetch user badges from Coder Wall
84
84
  email:
85
85
  - gregs@tcias.co.uk
@@ -108,9 +108,12 @@ files:
108
108
  - lib/coder_wally/builder.rb
109
109
  - lib/coder_wally/client.rb
110
110
  - lib/coder_wally/coder_wall.rb
111
+ - lib/coder_wally/exception_handler.rb
112
+ - lib/coder_wally/status_code_from_error.rb
111
113
  - lib/coder_wally/user.rb
112
114
  - lib/coder_wally/version.rb
113
115
  - test/account_test.rb
116
+ - test/api_test.rb
114
117
  - test/coder_wally_test.rb
115
118
  - test/fixtures/200.json
116
119
  - test/fixtures/404.json
@@ -143,6 +146,7 @@ specification_version: 4
143
146
  summary: A simple gem to fetch user badges from Coder Wall
144
147
  test_files:
145
148
  - test/account_test.rb
149
+ - test/api_test.rb
146
150
  - test/coder_wally_test.rb
147
151
  - test/fixtures/200.json
148
152
  - test/fixtures/404.json