Class: GovDelivery::TMS::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from CoreExt

#classify, #demodulize, #instance_class, #pluralize, #singularize, #tmsify

Methods included from Util::HalLinkParser

#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.

Examples:

client = TMS::Client.new("auth_token", {
                            :api_root => "https://tms.govdelivery.com",
                            :logger => Logger.new(STDOUT)})
client = TMS::Client.new("auth_token", {
                            api_root: "https://tms.govdelivery.com",
                            logger:   false})

Parameters:

  • auth_token (String)

    The auth_token of your account

  • options (Hash) (defaults to: DEFAULTS)

Options Hash (options):

  • :api_root (String)

    The root URL of the TMS api. Defaults to localhost:3000

  • :logger (Logger)

    An instance of a Logger class (http transport information will be logged here) - defaults to nil



25
26
27
28
29
30
# File 'lib/govdelivery/tms/client.rb', line 25

def initialize(auth_token, options = DEFAULTS)
  @api_root = options[:api_root]
  @logger = options.fetch(:logger, setup_logging(options[:debug]))
  connect!(auth_token, options.except(:api_root, :logger, :debug))
  discover!
end

Instance Attribute Details

#api_rootObject

Returns the value of attribute api_root.



7
8
9
# File 'lib/govdelivery/tms/client.rb', line 7

def api_root
  @api_root
end

#connectionObject

Returns the value of attribute connection.



7
8
9
# File 'lib/govdelivery/tms/client.rb', line 7

def connection
  @connection
end

#hrefObject

Returns the value of attribute href.



7
8
9
# File 'lib/govdelivery/tms/client.rb', line 7

def href
  @href
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/govdelivery/tms/client.rb', line 7

def logger
  @logger
end

#sidObject

Returns the value of attribute sid.



7
8
9
# File 'lib/govdelivery/tms/client.rb', line 7

def sid
  @sid
end

Instance Method Details

#clientObject



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, options = {})
  self.connection = GovDelivery::TMS::Connection.new({ auth_token: auth_token, api_root: api_root, logger: logger }.merge!(options))
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_connectionObject



82
83
84
# File 'lib/govdelivery/tms/client.rb', line 82

def raw_connection
  connection.connection
end