Class: Maestrano::Connec::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group_id) ⇒ Client

Returns a new instance of Client.



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

def initialize(group_id)
  @group_id = group_id
  self.class.base_uri("#{Maestrano.param('connec.host')}#{Maestrano.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

#default_optionsObject

Return the default options which includes maestrano authentication



21
22
23
24
25
26
27
28
# File 'lib/maestrano/connec/client.rb', line 21

def default_options
  {
    basic_auth: { 
      username: Maestrano.param('api.id'), 
      password: Maestrano.param('api.key')
    } 
  }
end

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

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



39
40
41
# File 'lib/maestrano/connec/client.rb', line 39

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

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

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



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

def post(relative_path, body, options = {})
  self.class.post(self.scoped_path(relative_path),
    default_options.merge(body: body.to_json).merge(options)
  )
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.’ } })



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

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

#scoped_path(relative_path) ⇒ Object

Return the right path scoped using the customer group id



32
33
34
35
# File 'lib/maestrano/connec/client.rb', line 32

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