Class: TMS::Client
Overview
The client class to connect and talk to the TMS REST API.
Constant Summary
collapse
- DEFAULTS =
{:api_root => 'http://localhost:3000', :logger => nil}.freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from CoreExt
#classify, #demodulize, #instance_class, #pluralize, #singularize, #tmsify
#parse_links, #subresources
Constructor Details
#initialize(auth_token, options = DEFAULTS) ⇒ Client
Create a new client and issue a request for the available resources for a given account.
22
23
24
25
26
|
# File 'lib/tms_client/client.rb', line 22
def initialize(auth_token, options = DEFAULTS)
@api_root = options[:api_root]
connect!(auth_token, options[:logger])
discover!
end
|
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
6
7
8
|
# File 'lib/tms_client/client.rb', line 6
def connection
@connection
end
|
#href ⇒ Object
Returns the value of attribute href.
6
7
8
|
# File 'lib/tms_client/client.rb', line 6
def href
@href
end
|
Instance Method Details
#client ⇒ Object
79
80
81
|
# File 'lib/tms_client/client.rb', line 79
def client
self
end
|
#connect!(auth_token, logger) ⇒ Object
28
29
30
|
# File 'lib/tms_client/client.rb', line 28
def connect!(auth_token, logger)
self.connection = TMS::Connection.new(:auth_token => auth_token, :api_root => @api_root, :logger => logger)
end
|
#delete(href) ⇒ Object
65
66
67
68
69
70
71
72
73
|
# File 'lib/tms_client/client.rb', line 65
def delete(href)
response = raw_connection.delete(href)
case response.status
when 200
return response
else
raise TMS::Request::Error.new(response.status)
end
end
|
#discover! ⇒ Object
32
33
34
35
|
# File 'lib/tms_client/client.rb', line 32
def discover!
services = get('/').body
parse_links(services['_links'])
end
|
#get(href) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/tms_client/client.rb', line 37
def get(href)
response = raw_connection.get(href)
case response.status
when 401..499
raise TMS::Request::Error.new(response.status)
when 202
raise TMS::Request::InProgress.new(response.body['message'])
else
return response
end
end
|
#post(obj) ⇒ Object
49
50
51
52
53
54
55
|
# File 'lib/tms_client/client.rb', line 49
def post(obj)
raw_connection.post do |req|
req.url @api_root + obj.href
req.['Content-Type'] = 'application/json'
req.body = obj.to_json
end
end
|
#put(obj) ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/tms_client/client.rb', line 57
def put(obj)
raw_connection.put do |req|
req.url @api_root + obj.href
req.['Content-Type'] = 'application/json'
req.body = obj.to_json
end
end
|
#raw_connection ⇒ Object
75
76
77
|
# File 'lib/tms_client/client.rb', line 75
def raw_connection
connection.connection
end
|