Class: BadBill

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(billomat_id, api_key, ssl = false) ⇒ BadBill

Create new Billomat connection.

Parameters:

  • billomat_id (String)

    Used as your BillomatID

  • api_key (String)

    API Key used to authenticate to the API.

  • ssl (Boolean) (defaults to: false)

    Wether to use SSL or not (only possible for paying customers)



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

.connectionBadBill?

Get the global connection object.

Returns:

  • (BadBill, nil)

    The global connection object or nil if not set.



64
65
66
# File 'lib/badbill.rb', line 64

def self.connection
  @connection
end

.connection=(connection) ⇒ Object

Assign global BadBill connection object.

Parameters:

  • connection (BadBill)

    The 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.

Parameters:

  • resource (String)

    The String resource name (gets prepended with /api/).

  • id (String, Integer) (defaults to: '')

    The ID for the resource.

  • options (Hash) (defaults to: nil)

    All parameters for this request. Exact parameters depend on the resource.

  • method (Symbol) (defaults to: :get)

    One of ALLOWED_METHODS.

Returns:

  • (Hashie::Mash)

    The response body. On error the return value only includes the key :error.



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='', options=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
    options = id
    id = ''
  end

  #no_accept = options.delete :no_accept
  @http_adapter.__send__(method) { |req|
    if method == :get && options && !options.empty?
      req.url "/api/#{resource}/#{id}", options
    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 = options if method != :get && options && !options.empty?
  }.body
rescue Faraday::Error::ClientError => error
  Hashie::Mash.new :error => error
end

#delete(resource, id = '', options = nil) ⇒ Hashie::Mash

Send a DELETE request.

Parameters:

  • resource (String)

    The String resource name (gets prepended with /api/).

  • id (String, Integer) (defaults to: '')

    The ID for the resource.

  • options (Hash) (defaults to: nil)

    All parameters for this request. Exact parameters depend on the resource.

  • method (Symbol)

    One of ALLOWED_METHODS.

Returns:

  • (Hashie::Mash)

    The response body. On error the return value only includes the key :error.



132
133
134
# File 'lib/badbill.rb', line 132

def delete resource, id='', options=nil
  call resource, id, options, :delete
end

#get(resource, id = '', options = nil) ⇒ Hashie::Mash

Send a GET request.

Parameters:

  • resource (String)

    The String resource name (gets prepended with /api/).

  • id (String, Integer) (defaults to: '')

    The ID for the resource.

  • options (Hash) (defaults to: nil)

    All parameters for this request. Exact parameters depend on the resource.

  • method (Symbol)

    One of ALLOWED_METHODS.

Returns:

  • (Hashie::Mash)

    The response body. On error the return value only includes the key :error.



108
109
110
# File 'lib/badbill.rb', line 108

def get resource, id='', options=nil
  call resource, id, options, :get
end

#post(resource, id = '', options = nil) ⇒ Hashie::Mash

Send a POST request.

Parameters:

  • resource (String)

    The String resource name (gets prepended with /api/).

  • id (String, Integer) (defaults to: '')

    The ID for the resource.

  • options (Hash) (defaults to: nil)

    All parameters for this request. Exact parameters depend on the resource.

  • method (Symbol)

    One of ALLOWED_METHODS.

Returns:

  • (Hashie::Mash)

    The response body. On error the return value only includes the key :error.



116
117
118
# File 'lib/badbill.rb', line 116

def post resource, id='', options=nil
  call resource, id, options, :post
end

#put(resource, id = '', options = nil) ⇒ Hashie::Mash

Send a PUT request.

Parameters:

  • resource (String)

    The String resource name (gets prepended with /api/).

  • id (String, Integer) (defaults to: '')

    The ID for the resource.

  • options (Hash) (defaults to: nil)

    All parameters for this request. Exact parameters depend on the resource.

  • method (Symbol)

    One of ALLOWED_METHODS.

Returns:

  • (Hashie::Mash)

    The response body. On error the return value only includes the key :error.



124
125
126
# File 'lib/badbill.rb', line 124

def put resource, id='', options=nil
  call resource, id, options, :put
end