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/base_resource.rb,
lib/badbill/forward_methods.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, NoConnection, NotAllowedException
Constant Summary collapse
- VERSION =
'0.0.1'
- 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
-
.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.
45 46 47 48 49 50 51 52 |
# File 'lib/badbill.rb', line 45 def initialize billomat_id, api_key, ssl=false @billomat_id = billomat_id @api_key = api_key @ssl = ssl @http_adapter = connection BadBill.connection = self end |
Class Method Details
.connection ⇒ BadBill?
Get the global connection object.
64 65 66 |
# File 'lib/badbill.rb', line 64 def self.connection @connection end |
.connection=(connection) ⇒ Object
Assign global BadBill connection object.
57 58 59 |
# File 'lib/badbill.rb', line 57 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.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/badbill.rb', line 80 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 Hashie::Mash.new :error => error end |
#delete(resource, id = '', options = nil) ⇒ Hashie::Mash
Send a DELETE request.
132 133 134 |
# File 'lib/badbill.rb', line 132 def delete resource, id='', =nil call resource, id, , :delete end |
#get(resource, id = '', options = nil) ⇒ Hashie::Mash
Send a GET request.
108 109 110 |
# File 'lib/badbill.rb', line 108 def get resource, id='', =nil call resource, id, , :get end |
#post(resource, id = '', options = nil) ⇒ Hashie::Mash
Send a POST request.
116 117 118 |
# File 'lib/badbill.rb', line 116 def post resource, id='', =nil call resource, id, , :post end |
#put(resource, id = '', options = nil) ⇒ Hashie::Mash
Send a PUT request.
124 125 126 |
# File 'lib/badbill.rb', line 124 def put resource, id='', =nil call resource, id, , :put end |