class YoClient::Client

Public Class Methods

new(api_token) click to toggle source

Constructor @param [String] api_token Yo API Token

# File lib/yo_client.rb, line 9
def initialize(api_token)
  @api_token = api_token
  @faraday = Faraday.new(url: 'http://api.justyo.co') do |faraday|
    faraday.request  :url_encoded
    faraday.response :json
    faraday.adapter  :net_http
  end
end

Public Instance Methods

subscribers_count() click to toggle source

Get a number of subscribers @return [Integer] number of subscribers

# File lib/yo_client.rb, line 42
def subscribers_count
  response = connection_wrapper {
    @faraday.get '/subscribers_count/', token_hash
  }
  response.body['result']
end
yo(username, options = {}) click to toggle source

Yo to specific user @param [String] username usename to send yo @param [Hash] options allowed only link for now @return [Boolean] if request has succeed

# File lib/yo_client.rb, line 32
def yo(username, options = {})
  options.merge!(username: username.upcase)
  response = connection_wrapper {
    @faraday.post '/yo/', token_hash.merge(options)
  }
  response.success?
end
yoall(options = {}) click to toggle source

Yo to all subscribers @param [Hash] options allowed only link for now @return [Boolean] if request has succeed

# File lib/yo_client.rb, line 21
def yoall(options = {})
  response = connection_wrapper {
    @faraday.post '/yoall/', token_hash.merge(options)
  }
  response.success?
end

Private Instance Methods

connection_wrapper(&block) click to toggle source

Connect with error handling @param [Proc] block

# File lib/yo_client.rb, line 52
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
token_hash() click to toggle source

Returns hash for every request @return [Hash] hash for every request

# File lib/yo_client.rb, line 69
def token_hash
  { api_token: @api_token }
end