Class: JxClient::OperationBase

Inherits:
Object
  • Object
show all
Defined in:
lib/jx_client/operation_base.rb

Direct Known Subclasses

ConfirmDocument, GetDocument, PutDocument

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation_name:, client:, default_options: {}, operation_default_options: {}, message_id_generate: false, timestamp_generate: false) ⇒ OperationBase

Returns a new instance of OperationBase.

Parameters:

  • operation_name (Symbol)

    operation name

  • client (Savon::Client)

    client

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

    default options

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

    operation specific default options

  • message_id_generate (Proc | Boolean) (defaults to: false)

    message id generator

  • timestamp_generate (Proc | Boolean) (defaults to: false)

    timestamp generator



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jx_client/operation_base.rb', line 13

def initialize(
  operation_name:,
  client:,
  default_options: {},
  operation_default_options: {},
  message_id_generate: false,
  timestamp_generate: false
)
  @operation_name = operation_name
  @client = client
  @default_options = default_options
  @operation_default_options = operation_default_options
  @message_id_generate = message_id_generate == true ? -> { SecureRandom.uuid } : message_id_generate
  @timestamp_generate = timestamp_generate == true ? -> { Time.now.utc.iso8601 } : timestamp_generate
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/jx_client/operation_base.rb', line 5

def response
  @response
end

#sent_optionsObject (readonly)

Returns the value of attribute sent_options.



5
6
7
# File 'lib/jx_client/operation_base.rb', line 5

def sent_options
  @sent_options
end

Instance Method Details

#callObject

call operation



42
43
44
45
# File 'lib/jx_client/operation_base.rb', line 42

def call
  @response = wrap_response(@client.call(@operation_name, sent_locals))
  self
end

#locals(options) ⇒ Hash

Returns locals for savon call.

Parameters:

  • options (Hash)

    options

Returns:

  • (Hash)

    locals for savon call

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/jx_client/operation_base.rb', line 54

def locals(options)
  raise NotImplementedError
end

#merge_options(options) ⇒ Object

merge options, default options and operation specific default options



48
49
50
# File 'lib/jx_client/operation_base.rb', line 48

def merge_options(options)
  with_generated(@default_options.merge(@operation_default_options, options))
end

#new_message_idString

Returns new message id.

Returns:

  • (String)

    new message id



67
68
69
70
# File 'lib/jx_client/operation_base.rb', line 67

def new_message_id
  raise "message_id must be included because message ID generation is disabled." unless @message_id_generate
  @message_id_generate.call
end

#new_timestampObject



72
73
74
75
# File 'lib/jx_client/operation_base.rb', line 72

def new_timestamp
  raise "timestamp must be included because timestamp generation is disabled." unless @timestamp_generate
  @timestamp_generate.call
end

#options(options) ⇒ self

Parameters:

  • options (Hash)

    options

Returns:

  • (self)


36
37
38
39
# File 'lib/jx_client/operation_base.rb', line 36

def options(options)
  @sent_options = merge_options(options)
  self
end

#sent_localsHash

Returns sent locals for savon call.

Returns:

  • (Hash)

    sent locals for savon call



30
31
32
# File 'lib/jx_client/operation_base.rb', line 30

def sent_locals
  locals(@sent_options)
end

#with_generated(options) ⇒ Hash

Returns options with :message_id.

Parameters:

  • options (Hash)

    options

Returns:

  • (Hash)

    options with :message_id



60
61
62
63
64
# File 'lib/jx_client/operation_base.rb', line 60

def with_generated(options)
  options[:message_id] ||= new_message_id
  options[:timestamp] ||= new_timestamp
  options
end

#wrap_response(response) ⇒ Object

wraps response



78
79
80
# File 'lib/jx_client/operation_base.rb', line 78

def wrap_response(response)
  response
end