Class: Moneybird::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/moneybird/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bearer_token) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/moneybird/client.rb', line 9

def initialize(bearer_token)
  @bearer_token = bearer_token
end

Instance Attribute Details

#bearer_tokenObject (readonly)

Returns the value of attribute bearer_token.



5
6
7
# File 'lib/moneybird/client.rb', line 5

def bearer_token
  @bearer_token
end

#errorsObject

Returns the value of attribute errors.



6
7
8
# File 'lib/moneybird/client.rb', line 6

def errors
  @errors
end

#faraday_adapterObject



21
22
23
# File 'lib/moneybird/client.rb', line 21

def faraday_adapter
  @faraday_adapter ||= Faraday.default_adapter
end

#httpObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/moneybird/client.rb', line 25

def http
  @http ||= Faraday.new(url: base_url) do |faraday|
    faraday.headers = faraday_headers
    faraday.request :url_encoded
    faraday.response :json
    faraday.use Moneybird::Middleware::ErrorHandling
    faraday.use Moneybird::Middleware::Pagination

    faraday.adapter faraday_adapter
  end
end

Instance Method Details

#administrationsObject



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

def administrations
  Moneybird::Service::Administration.new(self).all
end

#base_urlObject



13
14
15
# File 'lib/moneybird/client.rb', line 13

def base_url
  "https://moneybird.com/"
end

#get_all_pages(path, options = {}) ⇒ Object



48
49
50
51
52
# File 'lib/moneybird/client.rb', line 48

def get_all_pages(path, options = {})
  get_each_page(path, options).inject([]) do |array, objects|
    array += objects
  end
end

#get_each_page(path, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/moneybird/client.rb', line 54

def get_each_page(path, options = {})
  return enum_for(:get_each_page, path, options) unless block_given?
  path = "/api/#{version}/#{path}"
  while path
    response = http.get(path, options)
    yield response.body
    path = (response[:pagination_links].next if response[:pagination_links])
  end
end

#versionObject



17
18
19
# File 'lib/moneybird/client.rb', line 17

def version
  @version ||= 'v2'
end