Sha256: 8cbf81fc5374a2422becb5faf18c7f5d58d84f1e456228b58c37c39dfbae830f
Contents?: true
Size: 1.37 KB
Versions: 1
Compression:
Stored size: 1.37 KB
Contents
module Pinoccio class Client attr_reader :token def initialize(email_or_token, password=nil) @connection = Faraday.new(:url => 'https://api.pinocc.io/') if password.nil? @token = email_or_token else login(email_or_token, password) end end def login(email, password) response = post("login", email: email, password: password) @token = response.token end def account get "account" end def troops TroopCollection.new(self) end def get(path, data={}) data = {token:@token}.merge(data) if @token response = @connection.get("/v1/#{path}", data) handle_response(response) end def post(path, data={}) data = {token:@token}.merge(data) if @token response = @connection.post("/v1/#{path}", data) handle_response(response) end private def handle_response(response) hsh = JSON.parse(response.body) handle_error(hsh['error']) if hsh['error'] data = hsh['data'] if data.is_a? Array data.map {|d| OpenStruct.new(d) } else OpenStruct.new(data) end end def handle_error(err) if err['message'] && (err['message'] =~ /timeout/ || err['message'] =~ /timed out/) raise Pinoccio::TimeoutError.new(err) else raise Pinoccio::Error.new(err) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
pinoccio-0.1.0 | lib/client.rb |