From 9b96b4e8906ea8f095e85c26f5a92f26445741d2 Mon Sep 17 00:00:00 2001 From: youcune Date: Sun, 13 Jul 2014 08:53:40 +0900 Subject: [PATCH 1/7] add gem version information #2 --- yo_client.gemspec | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/yo_client.gemspec b/yo_client.gemspec index eddb630..d13ad34 100644 --- a/yo_client.gemspec +++ b/yo_client.gemspec @@ -8,8 +8,8 @@ Gem::Specification.new do |spec| spec.version = YoClient::VERSION spec.authors = ['Yu Nakanishi'] spec.email = ['you@nakanishi.email'] - 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' @@ -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 'rspec', '~> 3.0.0' spec.add_development_dependency 'pry-byebug' - spec.add_dependency 'faraday' - spec.add_dependency 'faraday_middleware' + spec.add_dependency 'faraday', '~> 0.9.0' + spec.add_dependency 'faraday_middleware', '~> 0.9.0' + spec.required_ruby_version = '>= 1.9' end From 85074374ca563000f1f8e13e40a93d8815b646a7 Mon Sep 17 00:00:00 2001 From: youcune Date: Sun, 13 Jul 2014 08:55:52 +0900 Subject: [PATCH 2/7] updated YARD --- lib/yo_client.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/yo_client.rb b/lib/yo_client.rb index e18138c..c254e79 100644 --- a/lib/yo_client.rb +++ b/lib/yo_client.rb @@ -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| @@ -21,7 +21,7 @@ def yoall end # Yo to specific user - # @param [String] username + # @param [String] username usename to send yo def yo(username) @faraday.post '/yo/', token_hash.merge(username: username.upcase) end From 12481d6c29b5a9251c821ebcf465449a35a4794d Mon Sep 17 00:00:00 2001 From: youcune Date: Sun, 13 Jul 2014 11:03:35 +0900 Subject: [PATCH 3/7] added Ruby versions to run tests --- .travis.yml | 2 ++ README.md | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/.travis.yml b/.travis.yml index 8a8ac56..7f4a405 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,5 @@ language: ruby rvm: + - 1.9.3 + - 2.0.0 - 2.1.2 diff --git a/README.md b/README.md index a9e3f47..ae345e9 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,11 @@ is a Ruby client of [Yo](http://www.justyo.co/). +## 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: From bb4db1e1eb255cae2cc8d89b73b834a629bca4ca Mon Sep 17 00:00:00 2001 From: youcune Date: Sun, 13 Jul 2014 11:27:33 +0900 Subject: [PATCH 4/7] added COVERALLS --- .coveralls.yml | 1 + README.md | 4 +++- spec/spec_helper.rb | 5 +++++ yo_client.gemspec | 1 + 4 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 .coveralls.yml diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..9160059 --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1 @@ +service_name: travis-ci diff --git a/README.md b/README.md index ae345e9..12dbd8c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ -# 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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 044ba13..b4099f6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,8 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) require 'yo_client' require 'pry' + +if ENV['TRAVIS'] + require 'coveralls' + Coveralls.wear! +end diff --git a/yo_client.gemspec b/yo_client.gemspec index d13ad34..d268481 100644 --- a/yo_client.gemspec +++ b/yo_client.gemspec @@ -22,6 +22,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rake' spec.add_development_dependency 'rspec', '~> 3.0.0' spec.add_development_dependency 'pry-byebug' + 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' From ce67c4330882b1c8c3b5edee11451aadde89abbb Mon Sep 17 00:00:00 2001 From: youcune Date: Sun, 13 Jul 2014 11:46:28 +0900 Subject: [PATCH 5/7] consider on error #1 --- README.md | 26 +++++++++++++++++++++----- lib/yo_client.rb | 38 +++++++++++++++++++++++++++++++++++--- lib/yo_client/version.rb | 2 +- spec/yo_client_spec.rb | 9 ++++----- 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 12dbd8c..a8ba3ab 100644 --- a/README.md +++ b/README.md @@ -14,13 +14,13 @@ is a Ruby client of [Yo](http://www.justyo.co/). 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 @@ -28,17 +28,24 @@ $ bundle ``` 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 ) @@ -47,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. diff --git a/lib/yo_client.rb b/lib/yo_client.rb index c254e79..e6930d9 100644 --- a/lib/yo_client.rb +++ b/lib/yo_client.rb @@ -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 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 diff --git a/lib/yo_client/version.rb b/lib/yo_client/version.rb index 1e5fcbc..3d41400 100644 --- a/lib/yo_client/version.rb +++ b/lib/yo_client/version.rb @@ -1,3 +1,3 @@ module YoClient - VERSION = '0.0.2' + VERSION = '0.0.3' end diff --git a/spec/yo_client_spec.rb b/spec/yo_client_spec.rb index 05daad4..1a147d6 100644 --- a/spec/yo_client_spec.rb +++ b/spec/yo_client_spec.rb @@ -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 @@ -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 @@ -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 From f70e71dd7462163b4e32d5a019a2ae20e99f15ca Mon Sep 17 00:00:00 2001 From: youcune Date: Sun, 13 Jul 2014 11:50:18 +0900 Subject: [PATCH 6/7] removed pry-byebug from development_dependency --- yo_client.gemspec | 1 - 1 file changed, 1 deletion(-) diff --git a/yo_client.gemspec b/yo_client.gemspec index d268481..cc884a4 100644 --- a/yo_client.gemspec +++ b/yo_client.gemspec @@ -21,7 +21,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'bundler', '~> 1.6' spec.add_development_dependency 'rake' spec.add_development_dependency 'rspec', '~> 3.0.0' - spec.add_development_dependency 'pry-byebug' spec.add_development_dependency 'coveralls' spec.add_dependency 'faraday', '~> 0.9.0' spec.add_dependency 'faraday_middleware', '~> 0.9.0' From c4160ccc02431dfa110dd34e42cd6c96cc86bfda Mon Sep 17 00:00:00 2001 From: youcune Date: Sun, 13 Jul 2014 11:52:49 +0900 Subject: [PATCH 7/7] removed pry --- spec/spec_helper.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b4099f6..e21eb4c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,5 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) require 'yo_client' -require 'pry' if ENV['TRAVIS'] require 'coveralls'