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/.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..a8ba3ab 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,26 @@ -# 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 @@ -21,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 ) @@ -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. diff --git a/lib/yo_client.rb b/lib/yo_client.rb index e18138c..e6930d9 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| @@ -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 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/spec_helper.rb b/spec/spec_helper.rb index 044ba13..e21eb4c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 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 diff --git a/yo_client.gemspec b/yo_client.gemspec index eddb630..cc884a4 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 '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