Class: CobotClient::ApiClient

Inherits:
Object
  • Object
show all
Includes:
UrlHelper
Defined in:
lib/cobot_client/api_client.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from UrlHelper

#cobot_url, site, site=

Constructor Details

#initialize(access_token) ⇒ ApiClient

Returns a new instance of ApiClient.



12
13
14
# File 'lib/cobot_client/api_client.rb', line 12

def initialize(access_token)
  @access_token = access_token
end

Class Attribute Details

.user_agentObject

Returns the value of attribute user_agent.



9
10
11
# File 'lib/cobot_client/api_client.rb', line 9

def user_agent
  @user_agent
end

Instance Method Details

#content_type_headerObject



61
62
63
# File 'lib/cobot_client/api_client.rb', line 61

def content_type_header
  {'Content-Type' => 'application/json'}
end

#create_booking(subdomain, resource_id, attributes) ⇒ Object



20
21
22
# File 'lib/cobot_client/api_client.rb', line 20

def create_booking(subdomain, resource_id, attributes)
  post subdomain, "/resources/#{resource_id}/bookings", attributes
end

#delete(subdomain, path) ⇒ Object



40
41
42
# File 'lib/cobot_client/api_client.rb', line 40

def delete(subdomain, path)
  RestClient.delete(cobot_url(subdomain, "/api#{path}"), headers)
end

#delete_booking(subdomain, id) ⇒ Object



28
29
30
# File 'lib/cobot_client/api_client.rb', line 28

def delete_booking(subdomain, id)
  delete subdomain, "/bookings/#{id}"
end

#get(subdomain, path, params = {}) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/cobot_client/api_client.rb', line 52

def get(subdomain, path, params = {})
  JSON.parse(
    RestClient.get(
      cobot_url(subdomain, "/api#{path}", params: params),
      headers).body,
    symbolize_names: true
  )
end

#get_resources(subdomain) ⇒ Object



16
17
18
# File 'lib/cobot_client/api_client.rb', line 16

def get_resources(subdomain)
  get subdomain, '/resources'
end

#headersObject



65
66
67
68
69
70
# File 'lib/cobot_client/api_client.rb', line 65

def headers
  {
    'Authorization' => "Bearer #{@access_token}",
    'User-Agent' => self.class.user_agent || "Cobot Client #{CobotClient::VERSION}"
  }
end

#post(subdomain, path, body) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/cobot_client/api_client.rb', line 44

def post(subdomain, path, body)
  JSON.parse RestClient.post(
      cobot_url(subdomain, "/api#{path}"),
      body.to_json,
      headers.merge(content_type_header)).body,
    symbolize_names: true
end

#put(subdomain, path, body) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/cobot_client/api_client.rb', line 32

def put(subdomain, path, body)
  JSON.parse RestClient.put(
      cobot_url(subdomain, "/api#{path}"),
      body.to_json,
      headers.merge(content_type_header)).body,
    symbolize_names: true
end

#update_booking(subdomain, id, attributes) ⇒ Object



24
25
26
# File 'lib/cobot_client/api_client.rb', line 24

def update_booking(subdomain, id, attributes)
  put subdomain, "/bookings/#{id}", attributes
end