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.
23
24
25
26
27
28
|
# File 'lib/tms_client/client.rb', line 23
def initialize(auth_token, options = DEFAULTS)
@api_root = options.delete(:api_root)
@logger = options.delete(:logger) || setup_logging(options.delete(:debug))
connect!(auth_token, options)
discover!
end
|
Instance Attribute Details
#api_root ⇒ Object
Returns the value of attribute api_root.
7
8
9
|
# File 'lib/tms_client/client.rb', line 7
def api_root
@api_root
end
|
#connection ⇒ Object
Returns the value of attribute connection.
7
8
9
|
# File 'lib/tms_client/client.rb', line 7
def connection
@connection
end
|
#href ⇒ Object
Returns the value of attribute href.
7
8
9
|
# File 'lib/tms_client/client.rb', line 7
def href
@href
end
|
#logger ⇒ Object
Returns the value of attribute logger.
7
8
9
|
# File 'lib/tms_client/client.rb', line 7
def logger
@logger
end
|
Instance Method Details
#client ⇒ Object
83
84
85
|
# File 'lib/tms_client/client.rb', line 83
def client
self
end
|
#connect!(auth_token, options = {}) ⇒ Object
30
31
32
|
# File 'lib/tms_client/client.rb', line 30
def connect!(auth_token, options={})
self.connection = TMS::Connection.new({:auth_token => auth_token, :api_root => api_root, :logger => logger}.merge!(options))
end
|
#delete(href) ⇒ Object
69
70
71
72
73
74
75
76
77
|
# File 'lib/tms_client/client.rb', line 69
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
34
35
36
37
|
# File 'lib/tms_client/client.rb', line 34
def discover!
services = get('/').body
parse_links(services['_links'])
end
|
#get(href) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/tms_client/client.rb', line 39
def get(href)
response = raw_connection.get(href)
case response.status
when 500..599
raise TMS::Request::Error.new(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
53
54
55
56
57
58
59
|
# File 'lib/tms_client/client.rb', line 53
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
61
62
63
64
65
66
67
|
# File 'lib/tms_client/client.rb', line 61
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
79
80
81
|
# File 'lib/tms_client/client.rb', line 79
def raw_connection
connection.connection
end
|