Module: Cts::Mpx::Driver::Assemblers

Defined in:
lib/cts/mpx/driver/assemblers.rb

Overview

collection of methods used to assemble various parts of a request.

Class Method Summary collapse

Class Method Details

.host(user: nil, service: nil, account_id: 'urn:theplatform:auth:root') ⇒ String

assembles user service and account_id into a host string

Parameters:

  • user (Cts::Mpx::User) (defaults to: nil)

    user to make calls with

  • service (String) (defaults to: nil)

    title of a service

  • account_id (String) (defaults to: 'urn:theplatform:auth:root')

    long form account_id id (ownerId)

Returns:

  • (String)

    assembled scheme and host

Raises:

  • (ArgumentError)

    if user or service is not supplied

  • (RuntimeError)

    if the user token is not set



17
18
19
20
21
22
23
24
25
# File 'lib/cts/mpx/driver/assemblers.rb', line 17

def host(user: nil, service: nil, account_id: 'urn:theplatform:auth:root')
  Helpers.required_arguments %i[user service], binding
  user.token!

  service = Services[service]
  u = URI.parse service.url()

  [u.scheme, u.host].join('://')
end

.path(service: nil, endpoint: nil, extra_path: nil, ids: nil, account_id: 'urn:theplatform:auth:root') ⇒ String

Assembles service, endpoint, extra_path, ids, and account_id into a host path

Parameters:

  • service (String) (defaults to: nil)

    title of a service

  • endpoint (String) (defaults to: nil)

    endpoint to make the call against

  • extra_path (String) (defaults to: nil)

    additional part to add to the path

  • ids (String) (defaults to: nil)

    comma delimited list of short id’s to add to the path.

  • account_id (String) (defaults to: 'urn:theplatform:auth:root')

    long form account_id id (ownerId)

Returns:

  • (String)

    assembled path for a data call

Raises:

  • (ArgumentError)

    if service or endpoint is not supplied



35
36
37
38
39
40
41
42
43
44
# File 'lib/cts/mpx/driver/assemblers.rb', line 35

def path(service: nil, endpoint: nil, extra_path: nil, ids: nil, account_id: 'urn:theplatform:auth:root')
  Helpers.required_arguments %i[service endpoint], binding
  service = Services[].find { |s| s.name == service && s.endpoints.include?(endpoint) }

  path = "#{URI.parse(service.url()).path}/#{service.path}/#{endpoint}"
  path += "/#{extra_path}" if extra_path
  path += "/feed" if service.type == 'data'
  path += "/#{ids}" if ids
  path
end

.query(user: nil, account_id: nil, service: nil, endpoint: nil, query: {}, range: nil, count: nil, entries: nil, sort: nil) ⇒ Hash

Assembles service, endpoint, query, range, count, entries, sort and account_id into a query

Parameters:

  • user (Cts::Mpx::User) (defaults to: nil)

    user to make calls with

  • account_id (String) (defaults to: nil)

    long form account_id id (ownerId)

  • service (String) (defaults to: nil)

    title of a service

  • endpoint (String) (defaults to: nil)

    endpoint to make the call against

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

    any additional parameters to add

  • range (String) (defaults to: nil)

    string (service) format of a range.

  • count (TrueFalse) (defaults to: nil)

    ask for a count of objects from the services.

  • entries (TrueFalse) (defaults to: nil)

    return an array of entries.

  • sort (String) (defaults to: nil)

    set the sort field

Returns:

  • (Hash)

    assembled query for a data call

Raises:

  • (ArgumentError)

    if user, service or endpoint is not supplied

  • (RuntimeError)

    if the user token is not set



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cts/mpx/driver/assemblers.rb', line 59

def query(user: nil, account_id: nil, service: nil, endpoint: nil, query: {}, range: nil, count: nil, entries: nil, sort: nil)
  Helpers.required_arguments %i[user service endpoint], binding
  user.token!

  service = Services[].find { |s| s.name == service && s.endpoints.include?(endpoint) }

  h = {
    schema: service.type == 'data' ? service.schema : service.endpoints[endpoint]['schema'],
    form:   service.form,
    token:  user.token
  }

  h[:account] =  if 
  h[:count] =   count if count
  h[:entries] = entries if entries
  h[:range] =   range if range
  h[:sort] =    sort if sort
  h.merge! Oj.load(Oj.dump(query), symbol_keys: true) if query.any?
  h
end