Class: GovDelivery::TMS::Client
- Inherits:
-
Object
- Object
- GovDelivery::TMS::Client
- Includes:
- CoreExt, Util::HalLinkParser
- Defined in:
- lib/govdelivery/tms/client.rb
Overview
The client class to connect and talk to the TMS REST API.
Constant Summary collapse
- DEFAULTS =
{ api_root: 'https://tms.govdelivery.com', logger: nil }.freeze
Instance Attribute Summary collapse
-
#api_root ⇒ Object
Returns the value of attribute api_root.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#href ⇒ Object
Returns the value of attribute href.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#sid ⇒ Object
Returns the value of attribute sid.
Instance Method Summary collapse
- #client ⇒ Object
- #connect!(auth_token, options = {}) ⇒ Object
- #delete(href) ⇒ Object
- #discover! ⇒ Object
- #get(href) ⇒ Object
-
#initialize(auth_token, options = DEFAULTS) ⇒ Client
constructor
Create a new client and issue a request for the available resources for a given account.
- #post(obj) ⇒ Object
- #put(obj) ⇒ Object
- #raw_connection ⇒ Object
Methods included from CoreExt
#classify, #demodulize, #instance_class, #pluralize, #singularize, #tmsify
Methods included from Util::HalLinkParser
Constructor Details
#initialize(auth_token, options = DEFAULTS) ⇒ Client
Create a new client and issue a request for the available resources for a given account.
25 26 27 28 29 30 |
# File 'lib/govdelivery/tms/client.rb', line 25 def initialize(auth_token, = DEFAULTS) @api_root = [:api_root] @logger = .fetch(:logger, setup_logging([:debug])) connect!(auth_token, .except(:api_root, :logger, :debug)) discover! end |
Instance Attribute Details
#api_root ⇒ Object
Returns the value of attribute api_root.
7 8 9 |
# File 'lib/govdelivery/tms/client.rb', line 7 def api_root @api_root end |
#connection ⇒ Object
Returns the value of attribute connection.
7 8 9 |
# File 'lib/govdelivery/tms/client.rb', line 7 def connection @connection end |
#href ⇒ Object
Returns the value of attribute href.
7 8 9 |
# File 'lib/govdelivery/tms/client.rb', line 7 def href @href end |
#logger ⇒ Object
Returns the value of attribute logger.
7 8 9 |
# File 'lib/govdelivery/tms/client.rb', line 7 def logger @logger end |
#sid ⇒ Object
Returns the value of attribute sid.
7 8 9 |
# File 'lib/govdelivery/tms/client.rb', line 7 def sid @sid end |
Instance Method Details
#client ⇒ Object
86 87 88 |
# File 'lib/govdelivery/tms/client.rb', line 86 def client self end |
#connect!(auth_token, options = {}) ⇒ Object
32 33 34 |
# File 'lib/govdelivery/tms/client.rb', line 32 def connect!(auth_token, = {}) self.connection = GovDelivery::TMS::Connection.new({ auth_token: auth_token, api_root: api_root, logger: logger }.merge!()) end |
#delete(href) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/govdelivery/tms/client.rb', line 72 def delete(href) response = raw_connection.delete(href) case response.status when 200...299 return response else fail GovDelivery::TMS::Request::Error.new(response.status) end end |
#discover! ⇒ Object
36 37 38 39 40 |
# File 'lib/govdelivery/tms/client.rb', line 36 def discover! services = get('/').body self.sid = services['sid'] parse_links(services['_links']) end |
#get(href) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/govdelivery/tms/client.rb', line 42 def get(href) response = raw_connection.get(href) case response.status when 500..599 fail GovDelivery::TMS::Request::Error.new(response.status) when 401..499 fail GovDelivery::TMS::Request::Error.new(response.status) when 202 fail GovDelivery::TMS::Request::InProgress.new(response.body['message']) else return response end end |
#post(obj) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/govdelivery/tms/client.rb', line 56 def post(obj) raw_connection.post do |req| req.url @api_root + obj.href req.headers['Content-Type'] = 'application/json' req.body = obj.to_json end end |
#put(obj) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/govdelivery/tms/client.rb', line 64 def put(obj) raw_connection.put do |req| req.url @api_root + obj.href req.headers['Content-Type'] = 'application/json' req.body = obj.to_json end end |
#raw_connection ⇒ Object
82 83 84 |
# File 'lib/govdelivery/tms/client.rb', line 82 def raw_connection connection.connection end |