Class: DotloopApi::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/dotloop_api/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token:, application: 'dotloop') ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
# File 'lib/dotloop_api/client.rb', line 9

def initialize(access_token:, application: 'dotloop')
  @access_token = access_token
  @application = application
  @limit = 100
  @limit_remaining = 100
  @limit_reset = 0
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



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

def access_token
  @access_token
end

#applicationObject

Returns the value of attribute application.



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

def application
  @application
end

#limitObject

Returns the value of attribute limit.



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

def limit
  @limit
end

#limit_remainingObject

Returns the value of attribute limit_remaining.



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

def limit_remaining
  @limit_remaining
end

#limit_resetObject

Returns the value of attribute limit_reset.



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

def limit_reset
  @limit_reset
end

Class Method Details

.snakify(hash) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/dotloop_api/client.rb', line 73

def self.snakify(hash)
  if hash.is_a? Array
    hash.map { |item| item.to_snake_keys.symbolize_keys }
  else
    hash.to_snake_keys.symbolize_keys
  end
end

Instance Method Details

#accountObject



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

def 
  @account ||= DotloopApi::EndPoints::Account.new(client: self).find
end

#ContactObject

rubocop:disable Naming/MethodName



69
70
71
# File 'lib/dotloop_api/client.rb', line 69

def Contact # rubocop:disable Naming/MethodName
  @Contact ||= DotloopApi::EndPoints::Contact.new(client: self) # rubocop:disable Naming/VariableName
end

#delete(page) ⇒ Object



36
37
38
39
40
41
# File 'lib/dotloop_api/client.rb', line 36

def delete(page)
  response = self.class.delete(page, headers:, timeout: 60)
  limits_from_headers(response.headers)
  handle_dotloop_error(response.code) if response.code != 200
  self.class.snakify(response.parsed_response)
end

#download(page, params = {}) ⇒ Object



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

def download(page, params = {})
  response = self.class.get(page, query: params, headers: download_headers, timeout: 360)
  handle_dotloop_error(response.code) if response.code != 200
  response.body
end

#get(page, params = {}) ⇒ Object



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

def get(page, params = {})
  response = raw(page, params)
  self.class.snakify(response)
end

#handle_dotloop_error(code) ⇒ Object



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

def handle_dotloop_error(code)
  return unless (error_class = DotloopApi::CodeMap.call(code))
  raise error_class.new "Error communicating: Response code #{code}"
end

#patch(page, model) ⇒ Object



22
23
24
25
26
27
# File 'lib/dotloop_api/client.rb', line 22

def patch(page, model)
  response = self.class.patch(page, query: model.attributes, headers:, timeout: 60)
  limits_from_headers(response.headers)
  handle_dotloop_error(response.code) if response.code != 200
  self.class.snakify(response.parsed_response)
end

#post(page, model) ⇒ Object



29
30
31
32
33
34
# File 'lib/dotloop_api/client.rb', line 29

def post(page, model)
  response = self.class.post(page, query: model.attributes, headers:, timeout: 60)
  limits_from_headers(response.headers)
  handle_dotloop_error(response.code) if response.code != 200
  self.class.snakify(response.parsed_response)
end

#ProfileObject

rubocop:disable Naming/MethodName



65
66
67
# File 'lib/dotloop_api/client.rb', line 65

def Profile # rubocop:disable Naming/MethodName
  @Profile ||= DotloopApi::EndPoints::Profile.new(client: self) # rubocop:disable Naming/VariableName
end

#raw(page, params = {}) ⇒ Object



43
44
45
46
47
48
# File 'lib/dotloop_api/client.rb', line 43

def raw(page, params = {})
  response = self.class.get(page, query: params, headers:, timeout: 60)
  limits_from_headers(response.headers)
  handle_dotloop_error(response.code) if response.code != 200
  response.parsed_response
end