class PeAccounting::Client

Public Class Methods

new(token, company_id, format = :json, endpoint = 'https://my.accounting.pe/api/v1/') click to toggle source

Initializes an API Client

@param token [String] The API token @param company_id [Integer] The company ID @param format [Symbol] The format by which the wrapper will interact with the API.

`:xml` or `:json - leave empty for :json by default

@param endpoint [String] 'my.accounting.pe/api/v1/' by default. This shouldn't change. @return [PeAccounting::Client] A PeAccounting::Client object

@example

api = PeAccounting::Client.new("your-api-token", 123)
# File lib/pe_accounting/client.rb, line 23
def initialize(token, company_id, format = :json, endpoint = 'https://my.accounting.pe/api/v1/')
  @company_id = company_id
  @format = format
  @token = token
  @endpoint = endpoint
end

Public Instance Methods

delete(request) click to toggle source

Performs a DELETE request

(see get)

@example

api.delete('client/123')
# File lib/pe_accounting/client.rb, line 83
def delete(request)
  unless request
    raise PeAccouningError, 'You must specify a request'
  end
  request(:delete, uri(request))
end
generate_payload(payload) click to toggle source

Public for debugging purposes

# File lib/pe_accounting/client.rb, line 91
def generate_payload(payload)
  if @format == :xml
    Gyoku.xml(payload)
  else
    MultiJson.dump(payload)
  end
end
get(request) click to toggle source

Performs a GET request

@param request [String] The endpoint to perform the request to, e.g. `client` @return [Hash, Array] A hash or an array, depending on the specs

@example

api.get('client')
# File lib/pe_accounting/client.rb, line 38
def get(request)
  unless request
    raise PeAccouningError, 'You must specify a request'
  end

  request(:get, uri(request))
end
post(request, payload = {}, root = nil) click to toggle source

Performs a POST request

(see put)

@example

api.post('client/123', {name: "John Doe"})
# File lib/pe_accounting/client.rb, line 69
def post(request, payload = {}, root = nil)
  unless request
    raise PeAccouningError, 'You must specify a request'
  end
  payload = (root ? {root => payload} : payload)
  request(:post, uri(request), payload)
end
put(request, payload = {}, root = nil) click to toggle source

Performs a PUT request

@param request [String] The options to pass to the method @param payload [Hash] A native Ruby hash describing the data to send @param root [String] The enclosing root of the object (useful for xml) @return [Hash] A hash containing the result of the request

@example

api.put('client', {name: "John Doe"})
# File lib/pe_accounting/client.rb, line 55
def put(request, payload = {}, root = nil)
  unless request
    raise PeAccouningError, 'You must specify a request'
  end
  payload = (root ? {root => payload} : payload)
  request(:put, uri(request), payload)
end

Private Instance Methods

denilize(h) click to toggle source

Unused for now, but can be useful for object editing if we go more high-level

# File lib/pe_accounting/client.rb, line 106
def denilize(h)
  h.each_with_object({}) { |(k,v),g|
    g[k] = (Hash === v) ?  denilize(v) : v ? v : '' }
end
handle_body(body) click to toggle source
# File lib/pe_accounting/client.rb, line 111
def handle_body(body)
  if @format == :xml
    parser = Nori.new(convert_dashes_to_underscores: false,
    empty_tag_value: "")
    hash = parser.parse(body)
  else
    hash = MultiJson.load(body)
  end

  # Try to get a more proper ruby object when doing JSON
  if @format == :json
    while hash.is_a?(Hash) && hash.length == 1
      hash = hash.values.first
    end
  end
  hash
end
request(method, uri, payload = nil) click to toggle source
# File lib/pe_accounting/client.rb, line 129
def request(method, uri, payload = nil)
  req = Net::HTTP.const_get(method.capitalize, false).new(uri)
  req.content_type = "application/#{@format.downcase}"
  req['X-Token'] = @token
  req.body = generate_payload(payload) if payload

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(req)
  end

  case res
  when Net::HTTPSuccess
    return handle_body(res.body)
  else
    raise PeAccouningError, (res.read_body ? handle_body(res.body) : res)
  end
end
uri(path) click to toggle source
# File lib/pe_accounting/client.rb, line 101
def uri(path)
  URI.parse("#{@endpoint}company/#{@company_id}/#{path}")
end