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.



13
14
15
16
17
18
19
# File 'lib/dotloop_api/client.rb', line 13

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.



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

def application
  @application
end

#limitObject

Returns the value of attribute limit.



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

def limit
  @limit
end

#limit_remainingObject

Returns the value of attribute limit_remaining.



10
11
12
# File 'lib/dotloop_api/client.rb', line 10

def limit_remaining
  @limit_remaining
end

#limit_resetObject

Returns the value of attribute limit_reset.



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

def limit_reset
  @limit_reset
end

Class Method Details

.snakify(hash) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/dotloop_api/client.rb', line 77

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



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

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

#ContactObject

rubocop:disable Naming/MethodName



73
74
75
# File 'lib/dotloop_api/client.rb', line 73

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

#delete(page) ⇒ Object



40
41
42
43
44
45
# File 'lib/dotloop_api/client.rb', line 40

def delete(page)
  response = self.class.delete(page, headers: 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



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

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



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

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

#handle_dotloop_error(code) ⇒ Object



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

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



26
27
28
29
30
31
# File 'lib/dotloop_api/client.rb', line 26

def patch(page, model)
  response = self.class.patch(page, query: model.attributes, headers: 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



33
34
35
36
37
38
# File 'lib/dotloop_api/client.rb', line 33

def post(page, model)
  response = self.class.post(page, query: model.attributes, headers: 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



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

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

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



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

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