WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 8f60dd8

Browse files
committed
Merge pull request #3 from youcune/feature/error_handling
Error handling enhancement
2 parents 56cfcd6 + c4160cc commit 8f60dd8

File tree

8 files changed

+86
-24
lines changed

8 files changed

+86
-24
lines changed

.coveralls.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
service_name: travis-ci

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
language: ruby
22
rvm:
3+
- 1.9.3
4+
- 2.0.0
35
- 2.1.2

README.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,51 @@
1-
# YoClient [![Build Status](https://travis-ci.org/youcune/yo_client.svg?branch=master)](https://travis-ci.org/youcune/yo_client)
1+
# YoClient
22

33
is a Ruby client of [Yo](http://www.justyo.co/).
4+
[![Build Status](https://travis-ci.org/youcune/yo_client.svg?branch=master)](https://travis-ci.org/youcune/yo_client)
5+
[![Coverage Status](https://coveralls.io/repos/youcune/yo_client/badge.png)](https://coveralls.io/r/youcune/yo_client)
6+
7+
## Requirements
8+
9+
* Ruby 1.9+ (Tested on Ruby 1.9.3, 2.0.0, 2.1.2)
10+
* [Yo API Account](http://dev.justyo.co/)
411

512
## Installation
613

714
Add this line to your application's Gemfile:
815

916
```
10-
gem 'yo_client', github: 'youcune/yo_client'
17+
gem 'yo_client'
1118
```
1219

1320
And then execute:
1421

1522
```
16-
$ bundle
23+
$ bundle install
1724
```
1825

1926
## Usage
2027

2128
```
2229
client = YoClient::Client.new(API_TOKEN)
2330
24-
# Send A Yo To All Subscribers
31+
# Yo all subscribers
2532
client.yoall
2633
27-
# Yo Individual Usernames
28-
# Note that USERNAME will be upcased and sent to API
34+
# Yo specific user
35+
# Note that USERNAME will be upcased before sending to API
2936
client.yo(USERNAME)
3037
3138
# Count Total Subscribers
3239
client.subscribers_count # -> 5
3340
```
3441

42+
### Error Handling
43+
44+
* `YoClient::ConnectionError` is risen if the connection has failed.
45+
* `YoClient::ClientError` is risen if the connection has succeeded but API returned error.
46+
47+
At the date of 13th July, even if API results in failure, it sometimes behaves as if it succeed. In this case, YoClient cannot tell succeeded or not.
48+
3549
## Contributing
3650

3751
1. Fork it ( https://github.com/youcune/yo_client/fork )
@@ -40,3 +54,12 @@ client.subscribers_count # -> 5
4054
4. Push to the branch (`git push origin my-new-feature`)
4155
5. Create a new Pull Request
4256

57+
## Yo the author
58+
59+
[Yo YOUCUNE](http://justyo.co/YOUCUNE), author of YoClient, if you ...
60+
61+
* like YoClient
62+
* dislike YoClient
63+
* have any ideas about YoClient
64+
65+
Thanks.

lib/yo_client.rb

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
module YoClient
66
class Client
77
# Constructor
8-
# @param [String] token Yo API Token
8+
# @param [String] api_token Yo API Token
99
def initialize(api_token)
1010
@api_token = api_token
1111
@faraday = Faraday.new(url: 'http://api.justyo.co') do |faraday|
@@ -16,26 +16,58 @@ def initialize(api_token)
1616
end
1717

1818
# Yo to all subscribers
19+
# @return [Boolean] if request has succeed
1920
def yoall
20-
@faraday.post '/yoall/', token_hash
21+
response = connection_wrapper {
22+
@faraday.post '/yoall/', token_hash
23+
}
24+
response.success?
2125
end
2226

2327
# Yo to specific user
24-
# @param [String] username
28+
# @param [String] username usename to send yo
29+
# @return [Boolean] if request has succeed
2530
def yo(username)
26-
@faraday.post '/yo/', token_hash.merge(username: username.upcase)
31+
response = connection_wrapper {
32+
@faraday.post '/yo/', token_hash.merge(username: username.upcase)
33+
}
34+
response.success?
2735
end
2836

2937
# Get a number of subscribers
3038
# @return [Integer] number of subscribers
3139
def subscribers_count
32-
response = @faraday.get '/subscribers_count/', token_hash
40+
response = connection_wrapper {
41+
@faraday.get '/subscribers_count/', token_hash
42+
}
3343
response.body['result']
3444
end
3545

3646
private
47+
# Connect with error handling
48+
# @param [Proc] block
49+
def connection_wrapper(&block)
50+
begin
51+
response = block.call
52+
raise ClientError.new(response.body['error']) if response.body.has_key?('error')
53+
rescue Faraday::ParsingError => e
54+
# Has gotten a response, but it is not formatted with JSON
55+
raise ClientError.new(e.message)
56+
rescue Faraday::ClientError => e
57+
# Failed to build a connection
58+
raise ConnectionError.new(e.message)
59+
end
60+
61+
response
62+
end
63+
64+
# Returns hash for every request
65+
# @return [Hash] hash for every request
3766
def token_hash
3867
{ api_token: @api_token }
3968
end
4069
end
70+
71+
class ConnectionError < StandardError; end
72+
class ClientError < StandardError; end
4173
end

lib/yo_client/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module YoClient
2-
VERSION = '0.0.2'
2+
VERSION = '0.0.3'
33
end

spec/spec_helper.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
22
require 'yo_client'
3-
require 'pry'
3+
4+
if ENV['TRAVIS']
5+
require 'coveralls'
6+
Coveralls.wear!
7+
end

spec/yo_client_spec.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
it 'hooks POST /yoall/' do
1919
expect_any_instance_of(Faraday::Connection).to(
2020
receive(:post)
21-
.with('/yoall/', { api_token: 'test' })
22-
.and_return(double('yo', body: {}))
21+
.with('/yoall/', { api_token: 'test' })
22+
.and_return(double('yo', body: {}, success?: true))
2323
)
2424
@client.yoall
2525
end
@@ -29,8 +29,8 @@
2929
it 'hooks POST /yo/' do
3030
expect_any_instance_of(Faraday::Connection).to(
3131
receive(:post)
32-
.with('/yo/', { api_token: 'test', username: 'YOUCUNE' })
33-
.and_return(double('yo', body: {}))
32+
.with('/yo/', { api_token: 'test', username: 'YOUCUNE' })
33+
.and_return(double('yo', body: {}, success?: true))
3434
)
3535
@client.yo('youcune')
3636
end
@@ -40,7 +40,6 @@
4040
it 'hooks GET /subscribers_count/' do
4141
expect_any_instance_of(Faraday::Connection).to(
4242
receive(:get)
43-
.with('/subscribers_count/', { api_token: 'test' })
4443
.and_return(double('subscribers_count', body: { 'result' => 5 }))
4544
)
4645
expect(@client.subscribers_count).to eq 5

yo_client.gemspec

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
88
spec.version = YoClient::VERSION
99
spec.authors = ['Yu Nakanishi']
1010
spec.email = ['[email protected]']
11-
spec.summary = 'Yo Client Library'
12-
spec.description = 'Yo Client Library'
11+
spec.summary = 'Yo (http://www.justyo.co/) Ruby Client'
12+
spec.description = 'Yo (http://www.justyo.co/) Ruby Client'
1313
spec.homepage = 'https://github.com/youcune/yo_client'
1414
spec.license = 'MIT'
1515

@@ -20,8 +20,9 @@ Gem::Specification.new do |spec|
2020

2121
spec.add_development_dependency 'bundler', '~> 1.6'
2222
spec.add_development_dependency 'rake'
23-
spec.add_development_dependency 'rspec'
24-
spec.add_development_dependency 'pry-byebug'
25-
spec.add_dependency 'faraday'
26-
spec.add_dependency 'faraday_middleware'
23+
spec.add_development_dependency 'rspec', '~> 3.0.0'
24+
spec.add_development_dependency 'coveralls'
25+
spec.add_dependency 'faraday', '~> 0.9.0'
26+
spec.add_dependency 'faraday_middleware', '~> 0.9.0'
27+
spec.required_ruby_version = '>= 1.9'
2728
end

0 commit comments

Comments
 (0)