Class: Loginator::Transaction
- Inherits:
-
Object
- Object
- Loginator::Transaction
- Defined in:
- lib/loginator/transaction.rb
Overview
A Loginator::Transaction is meant to encapsulate a single request/response cycle.
Instance Attribute Summary collapse
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#params ⇒ Object
The following may look funny to people, but please know that it’s simply an attempt to convey intent.
-
#path ⇒ Object
The following may look funny to people, but please know that it’s simply an attempt to convey intent.
-
#request ⇒ Object
The following may look funny to people, but please know that it’s simply an attempt to convey intent.
-
#response ⇒ Object
The following may look funny to people, but please know that it’s simply an attempt to convey intent.
-
#status ⇒ Object
The following may look funny to people, but please know that it’s simply an attempt to convey intent.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two Transactions are considered equivalent if the following are all equal: UUID, Duration, Response, Path, Request, Parameters.
-
#begin {|Transaction| ... } ⇒ Object
Marks the beginning of a Transaction.
-
#finished ⇒ Time
Marks the end of the transaction.
-
#initialize(opts = {}) ⇒ Transaction
constructor
Create a new Loginator::Transaction.
-
#to_h ⇒ Hash
Hashify the Transaction.
-
#to_json ⇒ String
JSONify the Transaction.
Constructor Details
#initialize(opts = {}) ⇒ Transaction
Create a new Loginator::Transaction
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
#duration ⇒ Object
Returns the value of attribute duration.
33 34 35 |
# File 'lib/loginator/transaction.rb', line 33 def duration @duration end |
#params ⇒ Object
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 |
#path ⇒ Object
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 |
#request ⇒ Object
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 |
#response ⇒ Object
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 |
#status ⇒ Object
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 |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
33 34 35 |
# File 'lib/loginator/transaction.rb', line 33 def @timestamp end |
#uuid ⇒ Object
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
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) .to_f == other..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.
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 |
#finished ⇒ Time
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.
88 89 90 91 92 |
# File 'lib/loginator/transaction.rb', line 88 def finished fin = Time.now @duration ||= calculate_duration(fin) fin end |
#to_h ⇒ Hash
Hashify the Transaction
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_json ⇒ String
JSONify the Transaction
104 105 106 |
# File 'lib/loginator/transaction.rb', line 104 def to_json MultiJson.dump(filter_hash!(to_h)) end |