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 => '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

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)})

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



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

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

#hrefObject

Returns the value of attribute href.



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

def href
  @href
end

Instance Method Details

#clientObject



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.headers['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.headers['Content-Type'] = 'application/json'
    req.body = obj.to_json
  end
end

#raw_connectionObject



75
76
77
# File 'lib/tms_client/client.rb', line 75

def raw_connection
  connection.connection
end