Module: Cts::Mpx::Services::Data

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

Overview

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

Class Method Summary collapse

Class Method Details

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

Addressable method, indexed by data 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
25
# File 'lib/cts/mpx/services/data.rb', line 16

def [](key = nil)
  services = Services[].select { |s| s.type == 'data' }
  return services unless key
  raise ArgumentError, 'key must be a string' unless key.is_a? String

  service = services.find { |e| e.name == key }
  raise ArgumentError, "#{key} must be a service name." unless service

  service
end

.delete(user: nil, account_id: nil, service: nil, endpoint: nil, sort: nil, extra_path: nil, range: nil, ids: nil, query: {}, headers: {}, count: nil, entries: nil) ⇒ Response

Procedural method to DELETE data from the data services

Parameters:

  • count (Boolean) (defaults to: nil)

    ask for a count of objects from the services

  • entries (Boolean) (defaults to: nil)

    return an array of entries

  • user (User) (defaults to: nil)

    user to make calls with

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

    additional parameters to add to the http call

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

    additional headers to attach to the http call

  • account (String)

    context account id or name

  • endpoint (String) (defaults to: nil)

    endpoint to make the call against

  • extra_path (String) (defaults to: nil)

    additional part to add to the path

  • fields (String)

    comma delimited list of fields to collect

  • ids (String) (defaults to: nil)

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

  • range (String) (defaults to: nil)

    string (service) format of a range

  • service (String) (defaults to: nil)

    title of a service

  • sort (String) (defaults to: nil)

    set the sort field

Returns:

  • (Response)

    Response of the call



31
32
33
# File 'lib/cts/mpx/services/data.rb', line 31

def delete(user: nil, account_id: nil, service: nil, endpoint: nil, sort: nil, extra_path: nil, range: nil, ids: nil, query: {}, headers: {}, count: nil, entries: nil)
  get(user: user, account_id: , service: service, endpoint: endpoint, sort: sort, extra_path: extra_path, range: range, ids: ids, query: query, headers: headers, count: count, entries: entries, method: :delete)
end

.get(user: nil, account_id: nil, service: nil, fields: nil, endpoint: nil, sort: nil, extra_path: nil, range: nil, ids: nil, query: {}, headers: {}, count: nil, entries: nil, method: :get) ⇒ Response

Procedural method to GET data from the data services

Parameters:

  • count (Boolean) (defaults to: nil)

    ask for a count of objects from the services

  • entries (Boolean) (defaults to: nil)

    return an array of entries

  • user (User) (defaults to: nil)

    user to make calls with

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

    additional parameters to add to the http call

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

    additional headers to attach to the http call

  • account (String)

    context account id or name

  • endpoint (String) (defaults to: nil)

    endpoint to make the call against

  • extra_path (String) (defaults to: nil)

    additional part to add to the path

  • fields (String) (defaults to: nil)

    comma delimited list of fields to collect

  • ids (String) (defaults to: nil)

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

  • range (String) (defaults to: nil)

    string (service) format of a range

  • service (String) (defaults to: nil)

    title of a service

  • sort (String) (defaults to: nil)

    set the sort field

Returns:

  • (Response)

    Response of the call

Raises:

  • (ArgumentError)

    if :query or :headers values are not a Hash

  • (ArgumentError)

    if the :page value is not a Page

  • (ArgumentError)

    if the list of :required_arguments are not set

  • (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



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cts/mpx/services/data.rb', line 51

def get(user: nil, account_id: nil, service: nil, fields: nil, endpoint: nil, sort: nil, extra_path: nil, range: nil, ids: nil, query: {}, headers: {}, count: nil, entries: nil, method: :get)
  prep_call(user: user, account_id: , service: service, query: query, headers: headers, required_arguments: ['user', 'service', 'endpoint'], binding: binding)

  host = Driver::Assemblers.host user: user, service: service, account_id: 
  path = Driver::Assemblers.path service: service, endpoint: endpoint, extra_path: extra_path, ids: ids
  query = Driver::Assemblers.query user: user, account_id: , service: service, endpoint: endpoint, query: query, range: range

  query[:fields] = fields if Services[service].type == 'data' && fields

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

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

Procedural method to POST data to the data services

Parameters:

  • page (Driver::Page) (defaults to: nil)

    formated page to send to the data services

  • user (User) (defaults to: nil)

    user to make calls with

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

    additional parameters to add to the http call

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

    additional headers to attach to the http call

  • account (String)

    account context, can be id or name

  • 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 :query or :headers values are not a Hash

  • (ArgumentError)

    if the :page value is not a Page

  • (ArgumentError)

    if the list of :required_arguments are not set

  • (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



75
76
77
78
79
80
81
82
83
# File 'lib/cts/mpx/services/data.rb', line 75

def post(user: nil, account_id: nil, service: nil, endpoint: nil, extra_path: nil, query: {}, page: nil, headers: {}, method: :post)
  prep_call(user: user, account_id: , service: service, query: query, headers: headers, required_arguments: ['user', 'service', 'endpoint', 'page'], page: page, binding: binding)
  host = Driver::Assemblers.host user: user, service: service, account_id: 
  path = Driver::Assemblers.path service: service, endpoint: endpoint, extra_path: extra_path
  query = Driver::Assemblers.query user: user, account_id: , service: service, endpoint: endpoint, query: query

  request = Driver::Request.create(method: method, url: [host, path].join, query: query, headers: headers, payload: page.to_s)
  request.call
end

.prep_call(args = {}) ⇒ Response

Helper method to assure that everything is is ok to call the methods above

Parameters:

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

    params to assure are correct

Options Hash (args):

  • :account (Symbol)

    account to apply to account_context lookups

  • :binding (Symbol)

    local binding (goes with required arguments)

  • :headers (Symbol)

    header object to check

  • :page (Symbol)

    page to send to the data service

  • :query (Symbol)

    query object to test

  • :required_arguments (Symbol)

    list of arguments required

  • :service (Symbol)

    title of the service

  • :user (Symbol)

    user to make calls with

Returns:

  • (Response)

    Response of the call

Raises:

  • (ArgumentError)

    if :query or :headers values are not a Hash

  • (ArgumentError)

    if the :page value is not a Page

  • (ArgumentError)

    if the list of :required_arguments are not set

  • (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



111
112
113
114
115
116
117
118
# File 'lib/cts/mpx/services/data.rb', line 111

def prep_call(args = {})
  Driver::Helpers.required_arguments args[:required_arguments], args[:binding]
  Driver::Helpers.raise_if_not_a([args[:user]], User) if args[:user]
  Driver::Helpers.raise_if_not_a_hash [args[:query], args[:headers]]
  Driver::Helpers.raise_if_not_a([args[:page]], Driver::Page) if args[:page]
  args[:user].token!
  Registry.fetch_and_store_domain(args[:user], args[:account_id])
end

.put(user: nil, account_id: nil, service: nil, endpoint: nil, extra_path: nil, query: {}, page: nil, headers: {}) ⇒ Response

Procedural method to PUT data to the data services

Parameters:

  • page (Driver::Page) (defaults to: nil)

    formated page to send to the data services

  • user (User) (defaults to: nil)

    user to make calls with

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

    additional parameters to add to the http call

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

    additional headers to attach to the http call

  • account (String)

    account context, can be id or name

  • 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



89
90
91
# File 'lib/cts/mpx/services/data.rb', line 89

def put(user: nil, account_id: nil, service: nil, endpoint: nil, extra_path: nil, query: {}, page: nil, headers: {})
  post(user: user, account_id: , service: service, endpoint: endpoint, extra_path: extra_path, query: query, page: page, headers: headers, method: :put)
end