Class: Cts::Mpx::Entry

Inherits:
Object
  • Object
show all
Includes:
Creatable
Defined in:
lib/cts/mpx/entry.rb

Overview

ORM style class to contain any entry from the data services.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEntry

Initialize an entry. Currently only instantiates fields.



51
52
53
# File 'lib/cts/mpx/entry.rb', line 51

def initialize
  @fields = Fields.new
end

Instance Attribute Details

#idEntry

Return the id of the entry.

Returns:

  • (Entry)

    the resulting entry



30
31
32
# File 'lib/cts/mpx/entry.rb', line 30

def id
  @id
end

Class Method Details

.load_by_id(user: nil, id: nil, fields: nil, account_id: nil) ⇒ Entry

Load a Entry based on a long form ID

Parameters:

  • user (User) (defaults to: nil)

    user to make calls with

  • id (String) (defaults to: nil)

    long form id to look up

  • fields (String) (defaults to: nil)

    comma delimited list of fields to collect

Returns:

  • (Entry)

    the resulting entry



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

def self.load_by_id(user: nil, id: nil, fields: nil, account_id: nil)
  Driver::Helpers.required_arguments %i[user id], binding
  Driver::Exceptions.raise_unless_argument_error? user, User
  Driver::Exceptions.raise_unless_reference? id

  e = new
  e.id = id
  e.load user: user, fields: fields, account_id: 
  e
end

Instance Method Details

#load(user: nil, fields: nil, account_id: 'urn:theplatform:auth:root') ⇒ Driver::Response

Load data from the remote services based on the id.

Parameters:

  • user (User) (defaults to: nil)

    user to make calls with

  • fields (String) (defaults to: nil)

    comma delimited list of fields to collect

Returns:



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

def load(user: nil, fields: nil, account_id: 'urn:theplatform:auth:root')
  Driver::Exceptions.raise_unless_required_keyword? binding, :user
  Driver::Exceptions.raise_unless_argument_error? user, User
  Driver::Exceptions.raise_unless_argument_error? fields, String if fields
  Driver::Exceptions.raise_unless_reference? id

  Registry.fetch_and_store_domain user, 
  response = Services::Data.get account_id: , user: user, service: service, endpoint: endpoint, fields: fields, ids: id.split("/").last

  entries = response.data['entries']

  raise "could not load #{id}" unless entries.any?

  self.fields.parse data: entries.first, xmlns: response.data['xmlns']
  self
end

#save(user: nil) ⇒ Driver::Response

Save the entry to the remote services.

Parameters:

  • user (User) (defaults to: nil)

    user to make calls with

Returns:

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cts/mpx/entry.rb', line 88

def save(user: nil)
  Driver::Helpers.required_arguments %i[user], binding
  Driver::Exceptions.raise_unless_argument_error? user, User
  raise ArgumentError, "fields['ownerId'] is a required field" unless fields['ownerId']
  raise ArgumentError, "endpoint is a required attribute" unless endpoint
  raise ArgumentError, "service is a required attribute" unless service

  p = Driver::Page.create entries: [fields.to_h], xmlns: fields.xmlns
  Registry.fetch_and_store_domain user, fields["ownerId"]

  response_params = { account_id: fields['ownerId'], user: user, service: service, endpoint: endpoint, page: p }
  result = Services::Data.send(id ? :put : :post, response_params)
  result.data if result.service_exception?

  self
end

#to_hHash

Return a [Hash] of the entry.

Returns:

  • (Hash)

    includes keys xmlns: [Hash] and entries: [Fields]



57
58
59
60
61
62
# File 'lib/cts/mpx/entry.rb', line 57

def to_h
  {
    xmlns: fields.xmlns,
    entry: fields.to_h
  }
end