class Easybill::Api::Base
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
Constants
- HEADERS
Public Class Methods
Set the authorization header by providing your easybill api_key
# File lib/easybill/api/base.rb, line 36 def authenticate(api_key) headers("authorization" => "Bearer #{api_key}") end
Creates a resource. Provide the data
as a hash. e.g. data = {
first_name: "Lars", last_name: "Bar", ...
} api.create(data)
# File lib/easybill/api/base.rb, line 65 def create(data) handle(post resource_path, body: data.to_json) end
Destroys a resource. Provide the id
. api.destroy(id)
# File lib/easybill/api/base.rb, line 88 def destroy(id) handle(delete "#{resource_path}/#{id}") end
Fetches a resource by its id
. You can set custom query
parameters. api.find(id, query: {group: 1})
# File lib/easybill/api/base.rb, line 51 def find(id, query: {}) handle(get "#{resource_path}/#{id}", query: query) end
Fetches all resources. You can set custom query
parameters.
# File lib/easybill/api/base.rb, line 43 def list(query: {}) handle(get resource_path, query: query) end
Updates a resource. Provide the id
and data
. e.g. id = 123456 data = {
first_name: "Lars", last_name: "Bar", ...
} api.update(id, data)
# File lib/easybill/api/base.rb, line 80 def update(id, data) handle(put "#{resource_path}/#{id}", body: data.to_json) end
Protected Class Methods
# File lib/easybill/api/base.rb, line 94 def custom(method:, path:, query: {}, data: {}, headers: {}, format: :json) request_options = {} request_options[:body] = data.to_json unless data.empty? request_options[:query] = query unless query.empty? request_options[:headers] = headers unless headers.empty? request_options[:format] = format handle(public_send method, path, request_options) end
Private Class Methods
# File lib/easybill/api/base.rb, line 107 def base_path "/rest/v1" end
TODO-schnika: move this out of here Oo
# File lib/easybill/api/base.rb, line 116 def dasherize(camel_cased_word) camel_cased_word.to_s.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). gsub("_", "-"). downcase end
# File lib/easybill/api/base.rb, line 111 def resource_path "#{base_path}/#{dasherize(self.name.split("::").last)}" end