Class: Vault::InvoiceBuilder::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/vault-invoice-builder-client.rb,
lib/vault-invoice-builder-client/client.rb,
lib/vault-invoice-builder-client/version.rb

Overview

Client for the Vault::InvoiceBuilder HTTP API.

Constant Summary collapse

VERSION =

The Vault::InvoiceBuilder::Client gem version.

'0.0.7'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ Client

Instantiate a client.

Parameters:

  • url (String) (defaults to: nil)

    The URL to connect to. Include the username and password to use when connecting. Defaults to the URL defined in the VAULT_INVOICE_BUILDER_URL environment variable if one isn't explicitly provided.



13
14
15
# File 'lib/vault-invoice-builder-client/client.rb', line 13

def initialize(url = nil)
  @url = url || ENV['VAULT_INVOICE_BUILDER_URL']
end

Instance Attribute Details

#urlObject (readonly)

The Vault::InvoiceBuilder HTTP API URL.



5
6
7
# File 'lib/vault-invoice-builder-client/client.rb', line 5

def url
  @url
end

Instance Method Details

#post(statement) ⇒ Excon::Response

Deprecated.

Please use #post_url instead

POST a statement to to the proxy-able /statement/:id endpoint

Parameters:

  • statement (Hash)

    An object matching the statement format described in the Vault::InvoiceBuilder README.

Returns:

  • (Excon::Response)

    The response object.

Raises:

  • (Excon::Errors::HTTPStatusError)

    Raised if the server returns an unsuccessful HTTP status code.



62
63
64
65
66
67
68
69
70
# File 'lib/vault-invoice-builder-client/client.rb', line 62

def post(statement)
  connection = Excon.new(@url)
  id = statement[:id] || statement['id']
  response = connection.post(
    path: "/statement/#{id}",
    headers: {'Content-Type' => 'application/json'},
    body: JSON.generate(statement),
    expects: [200])
end

#post_url(statement_url, json_size = nil, options = {}) ⇒ Excon::Response

POST an encrypted statement URL to to the proxy-able /statement/:id endpoint

Parameters:

  • statement_url (String)

    An encrypted URL to an object matching the statement format described in the Vault::InvoiceBuilder README.

  • json_size (Integer) (defaults to: nil)

    The size of the json statement in bytes

Returns:

  • (Excon::Response)

    The response object.

Raises:

  • (Excon::Errors::HTTPStatusError)

    Raised if the server returns an unsuccessful HTTP status code.



43
44
45
46
47
48
49
50
51
52
# File 'lib/vault-invoice-builder-client/client.rb', line 43

def post_url(statement_url, json_size=nil, options={})
  connection = Excon.new(@url)
  params =  { url: statement_url, json_size: json_size }.merge(options)
  connection.post(
    path: "/statements",
    headers: {'Content-Type' => 'application/json'},
    body: JSON.generate(params),
    expects: [202]
  )
end

#render_html(statement) ⇒ String

Render a statement into an HTML invoice.

Parameters:

  • statement (Hash)

    An object matching the statement format described in the Vault::InvoiceBuilder README.

Returns:

  • (String)

    The rendered HTML invoice.

Raises:

  • (Excon::Errors::HTTPStatusError)

    Raised if the server returns an unsuccessful HTTP status code.



24
25
26
27
28
29
30
31
32
# File 'lib/vault-invoice-builder-client/client.rb', line 24

def render_html(statement)
  connection = Excon.new(@url)
  response = connection.post(
    path: "/invoice.html",
    headers: {'Content-Type' => 'application/json'},
    body: JSON.generate(statement),
    expects: [200])
  response.body
end

#store(statement) ⇒ Excon::Response

Deprecated.

Returns The response object.

Parameters:

  • statement (Hash)

    An object matching the statement format described in the Vault::InvoiceBuilder README.

Returns:

  • (Excon::Response)

    The response object.

Raises:

  • (Excon::Errors::HTTPStatusError)

    Raised if the server returns an unsuccessful HTTP status code.



79
80
81
82
83
84
85
86
# File 'lib/vault-invoice-builder-client/client.rb', line 79

def store(statement)
  connection = Excon.new(@url)
  response = connection.post(
    path: '/store',
    headers: {'Content-Type' => 'application/json'},
    body: JSON.generate(statement),
    expects: [200])
end