Skip to content

Commit

Permalink
Merge pull request #3 from youcune/feature/error_handling
Browse files Browse the repository at this point in the history
Error handling enhancement
  • Loading branch information
youcune committed Jul 13, 2014
2 parents 56cfcd6 + c4160cc commit 8f60dd8
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 24 deletions.
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
service_name: travis-ci
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
language: ruby
rvm:
- 1.9.3
- 2.0.0
- 2.1.2
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,51 @@
# YoClient [![Build Status](https://travis-ci.org/youcune/yo_client.svg?branch=master)](https://travis-ci.org/youcune/yo_client)
# YoClient

is a Ruby client of [Yo](http://www.justyo.co/).
[![Build Status](https://travis-ci.org/youcune/yo_client.svg?branch=master)](https://travis-ci.org/youcune/yo_client)
[![Coverage Status](https://coveralls.io/repos/youcune/yo_client/badge.png)](https://coveralls.io/r/youcune/yo_client)

## Requirements

* Ruby 1.9+ (Tested on Ruby 1.9.3, 2.0.0, 2.1.2)
* [Yo API Account](http://dev.justyo.co/)

## Installation

Add this line to your application's Gemfile:

```
gem 'yo_client', github: 'youcune/yo_client'
gem 'yo_client'
```

And then execute:

```
$ bundle
$ bundle install
```

## Usage

```
client = YoClient::Client.new(API_TOKEN)
# Send A Yo To All Subscribers
# Yo all subscribers
client.yoall
# Yo Individual Usernames
# Note that USERNAME will be upcased and sent to API
# Yo specific user
# Note that USERNAME will be upcased before sending to API
client.yo(USERNAME)
# Count Total Subscribers
client.subscribers_count # -> 5
```

### Error Handling

* `YoClient::ConnectionError` is risen if the connection has failed.
* `YoClient::ClientError` is risen if the connection has succeeded but API returned error.

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.

## Contributing

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

## Yo the author

[Yo YOUCUNE](http://justyo.co/YOUCUNE), author of YoClient, if you ...

* like YoClient
* dislike YoClient
* have any ideas about YoClient

Thanks.
42 changes: 37 additions & 5 deletions lib/yo_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module YoClient
class Client
# Constructor
# @param [String] token Yo API Token
# @param [String] api_token Yo API Token
def initialize(api_token)
@api_token = api_token
@faraday = Faraday.new(url: 'http://api.justyo.co') do |faraday|
Expand All @@ -16,26 +16,58 @@ def initialize(api_token)
end

# Yo to all subscribers
# @return [Boolean] if request has succeed
def yoall
@faraday.post '/yoall/', token_hash
response = connection_wrapper {
@faraday.post '/yoall/', token_hash
}
response.success?
end

# Yo to specific user
# @param [String] username
# @param [String] username usename to send yo
# @return [Boolean] if request has succeed
def yo(username)
@faraday.post '/yo/', token_hash.merge(username: username.upcase)
response = connection_wrapper {
@faraday.post '/yo/', token_hash.merge(username: username.upcase)
}
response.success?
end

# Get a number of subscribers
# @return [Integer] number of subscribers
def subscribers_count
response = @faraday.get '/subscribers_count/', token_hash
response = connection_wrapper {
@faraday.get '/subscribers_count/', token_hash
}
response.body['result']
end

private
# Connect with error handling
# @param [Proc] block
def connection_wrapper(&block)
begin
response = block.call
raise ClientError.new(response.body['error']) if response.body.has_key?('error')
rescue Faraday::ParsingError => e
# Has gotten a response, but it is not formatted with JSON
raise ClientError.new(e.message)
rescue Faraday::ClientError => e
# Failed to build a connection
raise ConnectionError.new(e.message)
end

response
end

# Returns hash for every request
# @return [Hash] hash for every request
def token_hash
{ api_token: @api_token }
end
end

class ConnectionError < StandardError; end
class ClientError < StandardError; end
end
2 changes: 1 addition & 1 deletion lib/yo_client/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module YoClient
VERSION = '0.0.2'
VERSION = '0.0.3'
end
6 changes: 5 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'yo_client'
require 'pry'

if ENV['TRAVIS']
require 'coveralls'
Coveralls.wear!
end
9 changes: 4 additions & 5 deletions spec/yo_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
it 'hooks POST /yoall/' do
expect_any_instance_of(Faraday::Connection).to(
receive(:post)
.with('/yoall/', { api_token: 'test' })
.and_return(double('yo', body: {}))
.with('/yoall/', { api_token: 'test' })
.and_return(double('yo', body: {}, success?: true))
)
@client.yoall
end
Expand All @@ -29,8 +29,8 @@
it 'hooks POST /yo/' do
expect_any_instance_of(Faraday::Connection).to(
receive(:post)
.with('/yo/', { api_token: 'test', username: 'YOUCUNE' })
.and_return(double('yo', body: {}))
.with('/yo/', { api_token: 'test', username: 'YOUCUNE' })
.and_return(double('yo', body: {}, success?: true))
)
@client.yo('youcune')
end
Expand All @@ -40,7 +40,6 @@
it 'hooks GET /subscribers_count/' do
expect_any_instance_of(Faraday::Connection).to(
receive(:get)
.with('/subscribers_count/', { api_token: 'test' })
.and_return(double('subscribers_count', body: { 'result' => 5 }))
)
expect(@client.subscribers_count).to eq 5
Expand Down
13 changes: 7 additions & 6 deletions yo_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
spec.version = YoClient::VERSION
spec.authors = ['Yu Nakanishi']
spec.email = ['[email protected]']
spec.summary = 'Yo Client Library'
spec.description = 'Yo Client Library'
spec.summary = 'Yo (http://www.justyo.co/) Ruby Client'
spec.description = 'Yo (http://www.justyo.co/) Ruby Client'
spec.homepage = 'https://github.com/youcune/yo_client'
spec.license = 'MIT'

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

spec.add_development_dependency 'bundler', '~> 1.6'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'pry-byebug'
spec.add_dependency 'faraday'
spec.add_dependency 'faraday_middleware'
spec.add_development_dependency 'rspec', '~> 3.0.0'
spec.add_development_dependency 'coveralls'
spec.add_dependency 'faraday', '~> 0.9.0'
spec.add_dependency 'faraday_middleware', '~> 0.9.0'
spec.required_ruby_version = '>= 1.9'
end

0 comments on commit 8f60dd8

Please sign in to comment.