Class: Maestrano::Connec::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Preset
Defined in:
lib/maestrano/connec/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Preset

included

Constructor Details

#initialize(group_id) ⇒ Client

Returns a new instance of Client.



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

def initialize(group_id)
  @group_id = group_id
  @debug = !!ENV['DEBUG_CONNEC']

  self.class.base_uri("#{Maestrano[self.class.preset].param('connec.host')}#{Maestrano[self.class.preset].param('connec.base_path')}")
end

Instance Attribute Details

#group_idObject (readonly)

Returns the value of attribute group_id.



12
13
14
# File 'lib/maestrano/connec/client.rb', line 12

def group_id
  @group_id
end

Instance Method Details

#batch(body, options = {}) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/maestrano/connec/client.rb', line 74

def batch(body, options = {})
  url = "#{Maestrano[self.class.preset].param('connec.host')}/batch"
  response = self.class.post(url, default_options.merge(body: body.to_json).merge(options))

  save_request_response(:post, '/batch', body.to_json, response) if @debug

  response
end

#default_optionsObject

Return the default options which includes maestrano authentication



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

def default_options
  {
    basic_auth: {
      username: Maestrano[self.class.preset].param('api.id'),
      password: Maestrano[self.class.preset].param('api.key')
    },
    timeout: Maestrano[self.class.preset].param('connec.timeout')
  }
end

#get(relative_path, options = {}) ⇒ Object

E.g: client.get(‘/organizations’) E.g: client.get(‘/organizations/123’)



42
43
44
45
46
47
48
49
# File 'lib/maestrano/connec/client.rb', line 42

def get(relative_path, options = {})
  url = self.scoped_path(relative_path)
  response = self.class.get(url, default_options.merge(options))

  save_request_response(:get, url, nil, response) if @debug

  response
end

#post(relative_path, body, options = {}) ⇒ Object

E.g: client.post(‘/organizations’, { organizations: { name: ‘DoeCorp Inc.’ } })



52
53
54
55
56
57
58
59
# File 'lib/maestrano/connec/client.rb', line 52

def post(relative_path, body, options = {})
  url = self.scoped_path(relative_path)
  response = self.class.post(url, default_options.merge(body: body.to_json).merge(options))

  save_request_response(:post, url, body.to_json, response) if @debug

  response
end

#put(relative_path, body, options = {}) ⇒ Object

E.g for collection:

> client.put(‘/organizations/123’, { organizations: { name: ‘DoeCorp Inc.’ } })

E.g for singular resource:

> client.put(‘/company’, { company: { name: ‘DoeCorp Inc.’ } })



65
66
67
68
69
70
71
72
# File 'lib/maestrano/connec/client.rb', line 65

def put(relative_path, body, options = {})
  url = self.scoped_path(relative_path)
  response = self.class.put(url, default_options.merge(body: body.to_json).merge(options))

  save_request_response(:put, url, body.to_json, response) if @debug

  response
end

#save_request_response(action, url, request_body, response) ⇒ Object

Save request/response files

  • GET request: /tmp/connec/get/cld-123/organizations/XYZ/request response: /tmp/connec/get/cld-123/organizations/XYZ/response

  • POST request: /tmp/connec/post/cld-123/organizations/XYZ/request response: /tmp/connec/post/cld-123/organizations/XYZ/response

  • PUT request: /tmp/connec/put/cld-123/organizations/XYZ/request response: /tmp/connec/put/cld-123/organizations/XYZ/response



93
94
95
96
97
98
99
100
101
# File 'lib/maestrano/connec/client.rb', line 93

def save_request_response(action, url, request_body, response)
  # Path to store files
  file_path = "#{Dir.tmpdir}/connec/#{action}#{url}/#{SecureRandom.hex}"
  FileUtils.mkdir_p(file_path)

  # Save request and response
  File.open("#{file_path}/request", 'w') { |file| file.write(request_body) }
  File.open("#{file_path}/response", 'w') { |file| file.write(response.body) }
end

#scoped_path(relative_path) ⇒ Object

Return the right path scoped using the customer group id



35
36
37
38
# File 'lib/maestrano/connec/client.rb', line 35

def scoped_path(relative_path)
  clean_path = relative_path.gsub(/^\/+/, "").gsub(/\/+$/, "")
  "/#{@group_id}/#{clean_path}"
end