Class: TMS::Client

Inherits:
Object
  • Object
show all
Includes:
CoreExt, Util::HalLinkParser
Defined in:
lib/tms_client/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/tms_client/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/tms_client/client.rb', line 7

def api_root
  @api_root
end

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

#hrefObject

Returns the value of attribute href.



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

def href
  @href
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#clientObject



85
86
87
# File 'lib/tms_client/client.rb', line 85

def client
  self
end

#connect!(auth_token, options = {}) ⇒ Object



32
33
34
# File 'lib/tms_client/client.rb', line 32

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



71
72
73
74
75
76
77
78
79
# File 'lib/tms_client/client.rb', line 71

def delete(href)
  response = raw_connection.delete(href)
  case response.status
    when 200...299
      return response
    else
      raise TMS::Request::Error.new(response.status)
  end
end

#discover!Object



36
37
38
39
# File 'lib/tms_client/client.rb', line 36

def discover!
  services = get('/').body
  parse_links(services['_links'])
end

#get(href) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tms_client/client.rb', line 41

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



55
56
57
58
59
60
61
# File 'lib/tms_client/client.rb', line 55

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



63
64
65
66
67
68
69
# File 'lib/tms_client/client.rb', line 63

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



81
82
83
# File 'lib/tms_client/client.rb', line 81

def raw_connection
  connection.connection
end