Module: Cts::Mpx::Services::Web

Defined in:
lib/cts/mpx/services/web.rb

Overview

Collection of procedural methods to interact with the Web services All of these methods mimic the Business clients as close as possible If your operation does not work in the Business client, it will not work here

Class Method Summary collapse

Class Method Details

.[](key = nil) ⇒ Service[], Service

Addressable method, indexed by web service title

Parameters:

  • key (String) (defaults to: nil)

    service title to look up the service object

Returns:

  • (Service[])

    if no key, return the entire array of services

  • (Service)

    a service

Raises:

  • (ArgumentError)

    if the key is not a service name

  • (ArgumentError)

    if the key is not a string



16
17
18
19
20
21
22
23
24
# File 'lib/cts/mpx/services/web.rb', line 16

def [](key = nil)
  return services unless key

  Driver::Exceptions.raise_unless_argument_error?(key, String)
  service = services.find { |e| e.name == key }
  Driver::Exceptions.raise_unless_argument_error?(service, Driver::Service)

  service
end

.assemble_payload(service: nil, endpoint: nil, method: nil, arguments: nil) ⇒ Hash

assembles service, endpoint, method, and arguments into a payload

Parameters:

  • service (String) (defaults to: nil)

    title of a service

  • endpoint (String) (defaults to: nil)

    endpoint to make the call against

  • method (String) (defaults to: nil)

    method to make the call against

  • arguments (Hash) (defaults to: nil)

    arguments to send to the method

Returns:

  • (Hash)

    a hash ready for the web services



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cts/mpx/services/web.rb', line 38

def assemble_payload(service: nil, endpoint: nil, method: nil, arguments: nil)
  Driver::Helpers.required_arguments ['service', 'endpoint', 'method', 'arguments'], binding
  service = Services[service]
  method_list = service.endpoints[endpoint]['methods']
  Driver::Exceptions.raise_unless_argument_error?(arguments, Hash)
  Driver::Exceptions.raise_unless_argument_error?(method, 'method') { !method_list.key?(method) }

  arguments.each_key { |k| Driver::Exceptions.raise_unless_argument_error?(arguments, 'argument') { !method_list[method].include? k.to_s } }

  h = {}
  h[method] = arguments
  h
end

.post(user: nil, account: nil, service: nil, endpoint: nil, method: nil, query: {}, headers: {}, arguments: {}, extra_path: nil) ⇒ Response

Procedural method to interact with a web service via POST

Parameters:

  • user (User) (defaults to: nil)

    user to make calls with

  • headers (Hash) (defaults to: {})

    additional headers to attach to the http call

  • query (Hash) (defaults to: {})

    additional parameters to add to the http call

  • arguments (Hash) (defaults to: {})

    data to be sent to the data service

  • account (String) (defaults to: nil)

    account context, can be id or name

  • method (String) (defaults to: nil)

    method to make the call against

  • endpoint (String) (defaults to: nil)

    endpoint to make the call against

  • extra_path (String) (defaults to: nil)

    additional part to add to the path

  • service (String) (defaults to: nil)

    title of a service

Returns:

  • (Response)

    Response of the call

Raises:

  • (ArgumentError)

    if headers is not a [Hash]

  • (ArgumentError)

    if the list of [User], service or endpoint is not included

  • (ArgumentError)

    if the [User] does not have a token

  • (ArgumentError)

    if the user is not a user object

  • (ArgumentError)

    if the account_id is not valid

  • (ArgumentError)

    if the data is not a valid hash

  • (ArgumentError)

    if the account_id is not valid



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cts/mpx/services/web.rb', line 68

def post(user: nil, account: nil, service: nil, endpoint: nil, method: nil, query: {}, headers: {}, arguments: {}, extra_path: nil)
  ### check arguments
  Driver::Helpers.required_arguments ['user', 'service', 'endpoint', 'method', 'arguments'], binding
  Driver::Helpers.raise_if_not_a_hash [query, headers, arguments]
  user.token!

  ### Registry
  Registry.fetch_and_store_domain(user, ) unless self[service].url?

  ### Assemblers/prep
  host = Driver::Assemblers.host user: user, service: service
  path = Driver::Assemblers.path service: service, endpoint: endpoint, extra_path: extra_path
  payload = assemble_payload service: service, endpoint: endpoint, method: method, arguments: arguments
  query = Driver::Assemblers.query user: user, account_id: , service: service, endpoint: endpoint, query: query

  ### Request
  request = Driver::Request.create(method: :post, url: [host, path].join, query: query, payload: Oj.dump(payload), headers: headers)
  request.call
end

.servicesServices[]

Web service list

Returns:

  • (Services[])

    Array of web services



28
29
30
# File 'lib/cts/mpx/services/web.rb', line 28

def services
  Services[].select { |s| s.type == 'web' }
end