Class: Loginator::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/loginator/transaction.rb

Overview

A Loginator::Transaction is meant to encapsulate a single request/response cycle.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Transaction

Create a new Loginator::Transaction

Parameters:

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

    The options to create the Loginator::Transaction

Options Hash (opts):

  • :uuid (String)

    UUID (SecureRandom.uuid)

  • :timestamp (Time) — default: Time.now

    Beginning timestamp of transaction

  • :status (Integer)

    Status returned to the requester

  • :path (String)

    Path associated with the request

  • :request (String)

    Body of the request

  • :params (Hash)

    Parameters of the request

  • :response (String)

    Body of the response

  • :duration (Float)

    Duration of the request



51
52
53
54
55
56
57
58
# File 'lib/loginator/transaction.rb', line 51

def initialize(opts = {})
  # TODO: UUID Generation should have a service interface
  @uuid = opts.delete(:uuid) || SecureRandom.uuid
  @timestamp = opts.delete(:timestamp) || Time.now
  opts.each_pair do |k, v|
    send("#{k}=", v)
  end
end

Instance Attribute Details

#durationObject

Returns the value of attribute duration.



33
34
35
# File 'lib/loginator/transaction.rb', line 33

def duration
  @duration
end

#paramsObject

The following may look funny to people, but please know that it’s simply an attempt to convey intent. I want Transactions to be thought of as immutable objects. I’d like to take them in that direction eventually, but doing so in a way that is comprehensible is difficult. After rewriting the interface to Transaction several times, I settled here.



31
32
33
# File 'lib/loginator/transaction.rb', line 31

def params
  @params
end

#pathObject

The following may look funny to people, but please know that it’s simply an attempt to convey intent. I want Transactions to be thought of as immutable objects. I’d like to take them in that direction eventually, but doing so in a way that is comprehensible is difficult. After rewriting the interface to Transaction several times, I settled here.



31
32
33
# File 'lib/loginator/transaction.rb', line 31

def path
  @path
end

#requestObject

The following may look funny to people, but please know that it’s simply an attempt to convey intent. I want Transactions to be thought of as immutable objects. I’d like to take them in that direction eventually, but doing so in a way that is comprehensible is difficult. After rewriting the interface to Transaction several times, I settled here.



31
32
33
# File 'lib/loginator/transaction.rb', line 31

def request
  @request
end

#responseObject

The following may look funny to people, but please know that it’s simply an attempt to convey intent. I want Transactions to be thought of as immutable objects. I’d like to take them in that direction eventually, but doing so in a way that is comprehensible is difficult. After rewriting the interface to Transaction several times, I settled here.



31
32
33
# File 'lib/loginator/transaction.rb', line 31

def response
  @response
end

#statusObject

The following may look funny to people, but please know that it’s simply an attempt to convey intent. I want Transactions to be thought of as immutable objects. I’d like to take them in that direction eventually, but doing so in a way that is comprehensible is difficult. After rewriting the interface to Transaction several times, I settled here.



31
32
33
# File 'lib/loginator/transaction.rb', line 31

def status
  @status
end

#timestampObject

Returns the value of attribute timestamp.



33
34
35
# File 'lib/loginator/transaction.rb', line 33

def timestamp
  @timestamp
end

#uuidObject

Returns the value of attribute uuid.



33
34
35
# File 'lib/loginator/transaction.rb', line 33

def uuid
  @uuid
end

Class Method Details

.from_json(json) ⇒ Transaction

Parameters:

  • json (String)

    JSON body of the given transaction

Returns:



10
11
12
13
# File 'lib/loginator/transaction.rb', line 10

def from_json(json)
  obj = MultiJson.load(json, symbolize_keys: true)
  Transaction.new(filter_hash(obj))
end

Instance Method Details

#==(other) ⇒ Object

Two Transactions are considered equivalent if the following are all equal: UUID, Duration, Response, Path, Request, Parameters. This is largely used for testing serialization/deserialization.



112
113
114
115
# File 'lib/loginator/transaction.rb', line 112

def ==(other)
  timestamp.to_f == other.timestamp.to_f &&
    [:uuid, :duration, :response, :path, :request, :params].all? { |k| send(k) == other.send(k) }
end

#begin {|Transaction| ... } ⇒ Object

Marks the beginning of a Transaction. Optionally, will execute the given block in the context of a transaction, returning the last line of the block and raising any exceptions thrown in the block given.

NOTE: we make a best effort to guarantee that ‘transaction.finished` is called via an ensure block, but that is all. We do not set the status or response. You must do so in your block if you wish to record any failures.

Parameters:

  • &blk (Block)

    optional

Yields:

  • (Transaction)

    the transaction object after it has been updated

Returns:

  • Returns whatever the block returns



72
73
74
75
76
77
78
79
80
# File 'lib/loginator/transaction.rb', line 72

def begin
  @timestamp = Time.now
  # NOTE: yield self is a bit of a smell to me, but I am okay with this
  # as the block is evaluated in the context of the caller and not of
  # the Transaction object.
  yield self if block_given?
ensure
  finished
end

#finishedTime

Marks the end of the transaction.

NOTE: Once set, duration should be considered immutable. I.e. you cannot affect the duration of this Transaction by calling finished twice.

Returns:

  • (Time)

    time of the end of the transaction



88
89
90
91
92
# File 'lib/loginator/transaction.rb', line 88

def finished
  fin = Time.now
  @duration ||= calculate_duration(fin)
  fin
end

#to_hHash

Hashify the Transaction

Returns:

  • (Hash)


96
97
98
99
100
# File 'lib/loginator/transaction.rb', line 96

def to_h
  [:uuid, :timestamp, :status, :path, :request, :params, :response, :duration].each_with_object({}) do |key, hsh|
    hsh[key] = send(key)
  end
end

#to_jsonString

JSONify the Transaction

Returns:

  • (String)


104
105
106
# File 'lib/loginator/transaction.rb', line 104

def to_json
  MultiJson.dump(filter_hash!(to_h))
end