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.



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

def initialize(access_token:, application: 'dotloop')
  @access_token = access_token
  @application = application
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

Class Method Details

.snakify(hash) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/dotloop_api/client.rb', line 67

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



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

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

#ContactObject

rubocop:disable Naming/MethodName



63
64
65
# File 'lib/dotloop_api/client.rb', line 63

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

#delete(page) ⇒ Object



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

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

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



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

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



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

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

#handle_dotloop_error(code) ⇒ Object



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

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



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

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

#post(page, model) ⇒ Object



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

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

#ProfileObject

rubocop:disable Naming/MethodName



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

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

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



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

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