Class: Easybill::Api::Base

Inherits:
Object
  • Object
show all
Includes:
ResponseHandler, HTTParty
Defined in:
lib/easybill/api/base.rb

Overview

The base class for all Easybill entities. Set the authorization header before using it via ‘.authenticate`

Sets the default headers: “User-Agent” => “Ruby.Easybill.Api”, “Accept” => “application/json”, “Content-Type” => “application/json”

Provides the basic interface. Such as list, find, create, update, destroy

Constant Summary collapse

HEADERS =
{
  "User-Agent"   => "Ruby.Easybill.Api",
  "Accept"       => "application/json",
  "Content-Type" => "application/json"
}

Class Method Summary collapse

Methods included from ResponseHandler

included

Class Method Details

.authenticate(api_key) ⇒ Object

Set the authorization header by providing your easybill api_key



36
37
38
# File 'lib/easybill/api/base.rb', line 36

def authenticate(api_key)
  headers("authorization" => "Bearer #{api_key}")
end

.create(data) ⇒ Object

Creates a resource. Provide the data as a hash. e.g. data =

first_name: "Lars",
last_name: "Bar",
...

api.create(data)



65
66
67
# File 'lib/easybill/api/base.rb', line 65

def create(data)
  handle(post resource_path, body: data.to_json)
end

.destroy(id) ⇒ Object

Destroys a resource. Provide the id. api.destroy(id)



88
89
90
# File 'lib/easybill/api/base.rb', line 88

def destroy(id)
  handle(delete "#{resource_path}/#{id}")
end

.find(id, query: {}) ⇒ Object

Fetches a resource by its id. You can set custom query parameters. api.find(id, query: 1)



51
52
53
# File 'lib/easybill/api/base.rb', line 51

def find(id, query: {})
  handle(get "#{resource_path}/#{id}", query: query)
end

.list(query: {}) ⇒ Object

Fetches all resources. You can set custom query parameters.



43
44
45
# File 'lib/easybill/api/base.rb', line 43

def list(query: {})
  handle(get resource_path, query: query)
end

.update(id, data) ⇒ Object

Updates a resource. Provide the id and data. e.g. id = 123456 data =

first_name: "Lars",
last_name: "Bar",
...

api.update(id, data)



80
81
82
# File 'lib/easybill/api/base.rb', line 80

def update(id, data)
  handle(put "#{resource_path}/#{id}", body: data.to_json)
end