Class: BadBill
- Inherits:
-
Object
- Object
- BadBill
- Defined in:
- lib/badbill.rb,
lib/badbill/client.rb,
lib/badbill/invoice.rb,
lib/badbill/resource.rb,
lib/badbill/recurring.rb,
lib/badbill/invoice_item.rb,
lib/badbill/base_resource.rb,
lib/badbill/forward_methods.rb,
lib/badbill/invoice_comment.rb,
lib/badbill/invoice_payment.rb
Overview
Handles the connection and requests to the Billomat API.
This class can be used for direct API access and is used for connections from resource classes.
If a API resource is not yet implemented as a Ruby class, easy access is possible here.
Examples:
billo = BadBill.new 'ruby', '1234568'
# => #<BadBill:0x00000002825710 ...>
billo.get 'clients'
# => {"clients"=>{"client"=>[...]}}
Defined Under Namespace
Modules: ForwardMethods, Resource Classes: BaseResource, Client, Invoice, InvoiceComment, InvoiceItem, InvoicePayment, MissingConfiguration, NoConnection, NotAllowedException, Recurring
Constant Summary collapse
- VERSION =
'0.1.0'
- API_URL =
The API url used for all connections.
'http%s://%s.billomat.net/'
- ALLOWED_METHODS =
Allowed HTTP methods.
[:get, :post, :put, :delete]
Class Method Summary collapse
- .clear_connection ⇒ Object
-
.connection ⇒ BadBill?
Get the global connection object.
-
.connection=(connection) ⇒ Object
Assign global BadBill connection object.
Instance Method Summary collapse
-
#call(resource, id = '', options = nil, method = :get) ⇒ Hashie::Mash
Call the specified resource.
-
#delete(resource, id = '', options = nil) ⇒ Hashie::Mash
Send a DELETE request.
-
#get(resource, id = '', options = nil) ⇒ Hashie::Mash
Send a GET request.
-
#initialize(billomat_id, api_key, ssl = false) ⇒ BadBill
constructor
Create new Billomat connection.
-
#post(resource, id = '', options = nil) ⇒ Hashie::Mash
Send a POST request.
-
#put(resource, id = '', options = nil) ⇒ Hashie::Mash
Send a PUT request.
Constructor Details
#initialize(billomat_id, api_key, ssl = false) ⇒ BadBill
Create new Billomat connection.
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/badbill.rb', line 57 def initialize billomat_id, api_key, ssl=false @billomat_id = billomat_id @api_key = api_key raise MissingConfiguration.new("Billomat ID missing") if @billomat_id.nil? raise MissingConfiguration.new("API Key missing") if @api_key.nil? @ssl = ssl @http_adapter = connection BadBill.connection = self end |
Class Method Details
.clear_connection ⇒ Object
84 85 86 |
# File 'lib/badbill.rb', line 84 def self.clear_connection @connection = nil end |
.connection ⇒ BadBill?
Get the global connection object.
80 81 82 |
# File 'lib/badbill.rb', line 80 def self.connection @connection end |
.connection=(connection) ⇒ Object
Assign global BadBill connection object.
73 74 75 |
# File 'lib/badbill.rb', line 73 def self.connection= connection @connection = connection end |
Instance Method Details
#call(resource, id = '', options = nil, method = :get) ⇒ Hashie::Mash
Call the specified resource.
It sets the X-BillomatApiKey header, the Content-Type header and the Accept header.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/badbill.rb', line 101 def call resource, id='', =nil, method=:get raise NotAllowedException.new("#{method.inspect} is not allowed. Use one of [:#{ALLOWED_METHODS*', :'}]") unless ALLOWED_METHODS.include?(method) if id.kind_of? Hash = id id = '' end #no_accept = options.delete :no_accept @http_adapter.__send__(method) { |req| if method == :get && && !.empty? req.url "/api/#{resource}/#{id}", else req.url "/api/#{resource}/#{id}" end req.headers['X-BillomatApiKey'] = @api_key req.headers['Accept'] = 'application/json' req.headers['Content-Type'] = 'application/json' if [:post, :put].include?(method) req.body = if method != :get && && !.empty? }.body rescue Faraday::Error::ClientError => error if error.response && error.response.has_key?(:body) && error.response[:body] body = error.response[:body] if !body.kind_of?(Hash) && ['{', '[', '"'].include?(body[0]) body = JSON.parse body end Hashie::Mash.new error: error, body: body else Hashie::Mash.new error: error end end |
#delete(resource, id = '', options = nil) ⇒ Hashie::Mash
Send a DELETE request.
161 162 163 |
# File 'lib/badbill.rb', line 161 def delete resource, id='', =nil call resource, id, , :delete end |
#get(resource, id = '', options = nil) ⇒ Hashie::Mash
Send a GET request.
137 138 139 |
# File 'lib/badbill.rb', line 137 def get resource, id='', =nil call resource, id, , :get end |
#post(resource, id = '', options = nil) ⇒ Hashie::Mash
Send a POST request.
145 146 147 |
# File 'lib/badbill.rb', line 145 def post resource, id='', =nil call resource, id, , :post end |
#put(resource, id = '', options = nil) ⇒ Hashie::Mash
Send a PUT request.
153 154 155 |
# File 'lib/badbill.rb', line 153 def put resource, id='', =nil call resource, id, , :put end |