class Tinysou::Client

Client for the Tinysou API

Attributes

token[RW]

Public Class Methods

new(token) click to toggle source
# File lib/tinysou/client.rb, line 17
def initialize(token)
  @token = token
  @conn = Faraday.new(url: 'http://api.tinysou.com/v1/') do |faraday|
    faraday.adapter Faraday.default_adapter
  end
  @headers = { 'Content-type' => 'application/json',
               'Authorization' => "token #{@token}",
               'User-Agent' => "Tinysou-Ruby/#{Tinysou::VERSION}" }
end

Public Instance Methods

delete(path, params = nil) click to toggle source
# File lib/tinysou/client.rb, line 39
def delete(path, params = nil)
  request :delete, path, params, nil
end
get(path, params = nil) click to toggle source
# File lib/tinysou/client.rb, line 27
def get(path, params = nil)
  request :get, path, params, nil
end
post(path, data = nil) click to toggle source
# File lib/tinysou/client.rb, line 31
def post(path, data = nil)
  request :post, path, nil, data
end
put(path, data = nil) click to toggle source
# File lib/tinysou/client.rb, line 35
def put(path, data = nil)
  request :put, path, nil, data
end

Private Instance Methods

request(method, path, params, data) { |req| ... } click to toggle source
# File lib/tinysou/client.rb, line 45
def request(method, path, params, data)
  res = @conn.run_request(method,
                          path,
                          MultiJson.dump(data),
                          @headers) do |req|
    req.params.update(params) if params
    yield(req) if block_given?
  end
  if res.status == 204
    true
  elsif res.status > 400
    fail res.body
  elsif !(res.body.nil? || res.body.empty?)
    MultiJson.load(res.body, symbolize_keys: true)
  else
    nil
  end
end