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.



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

def initialize(bearer_token)
  @bearer_token = bearer_token
end

Instance Attribute Details

#_last_responseObject (readonly)

Returns the value of attribute _last_response.



3
4
5
# File 'lib/moneybird/client.rb', line 3

def _last_response
  @_last_response
end

#bearer_tokenObject (readonly)

Returns the value of attribute bearer_token.



3
4
5
# File 'lib/moneybird/client.rb', line 3

def bearer_token
  @bearer_token
end

#errorsObject

Returns the value of attribute errors.



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

def errors
  @errors
end

#faraday_adapterObject



19
20
21
# File 'lib/moneybird/client.rb', line 19

def faraday_adapter
  @faraday_adapter ||= Faraday.default_adapter
end

#httpObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/moneybird/client.rb', line 23

def http
  @http ||= begin
    uri = uri_for_path(base_url)
    http = Faraday.new(:url => uri) do |faraday|
      faraday.request  :url_encoded
      faraday.adapter  faraday_adapter
    end
    http
  end
end

Instance Method Details

#administrationsObject



66
67
68
# File 'lib/moneybird/client.rb', line 66

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

#base_urlObject



11
12
13
# File 'lib/moneybird/client.rb', line 11

def base_url
  "https://moneybird.com/api/#{version}/"
end

#delete(path, headers = {}) ⇒ Object



61
62
63
64
# File 'lib/moneybird/client.rb', line 61

def delete(path, headers={})
  uri = uri_for_path(path)
  @_last_response = http.delete(uri.request_uri, nil, self.headers.merge(headers))
end

#get(path, headers = {}) ⇒ Object



46
47
48
49
# File 'lib/moneybird/client.rb', line 46

def get(path, headers={})
  uri = uri_for_path(path)
  @_last_response = http.get(uri.request_uri, nil, self.headers.merge(headers))
end

#headersObject



34
35
36
37
38
39
40
# File 'lib/moneybird/client.rb', line 34

def headers
  {
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
    'Authorization' => "Bearer #{bearer_token}"
  }
end

#patch(path, body = nil, headers = {}) ⇒ Object



51
52
53
54
# File 'lib/moneybird/client.rb', line 51

def patch(path, body=nil, headers={})
  uri = uri_for_path(path)
  @_last_response = http.patch(uri.request_uri, body, self.headers.merge(headers))
end

#post(path, body = nil, headers = {}) ⇒ Object



56
57
58
59
# File 'lib/moneybird/client.rb', line 56

def post(path, body=nil, headers={})
  uri = uri_for_path(path)
  @_last_response = http.post(uri.request_uri, body, self.headers.merge(headers))
end

#uri_for_path(path) ⇒ Object



42
43
44
# File 'lib/moneybird/client.rb', line 42

def uri_for_path(path)
  URI.parse(File.join(base_url, path))
end

#versionObject



15
16
17
# File 'lib/moneybird/client.rb', line 15

def version
  @version ||= 'v2'
end