Class: OptimusPrime::Base
- Inherits:
-
Object
- Object
- OptimusPrime::Base
- Defined in:
- lib/optimus_prime.rb
Instance Attribute Summary collapse
-
#op_port ⇒ Object
readonly
Returns the value of attribute op_port.
-
#wait_for ⇒ Object
readonly
Returns the value of attribute wait_for.
Instance Method Summary collapse
- #clear! ⇒ Object
- #count(path_name) ⇒ Object
-
#initialize(opts = {}) ⇒ Base
constructor
A new instance of Base.
- #last_request_for(path_name) ⇒ Object
- #prime(path_name, response = "", options = {}) ⇒ Object
- #wait_until_request(path_name, &block) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Base
Returns a new instance of Base.
107 108 109 110 |
# File 'lib/optimus_prime.rb', line 107 def initialize(opts={}) @wait_for = opts[:wait_for] || 3 @op_port = $port end |
Instance Attribute Details
#op_port ⇒ Object (readonly)
Returns the value of attribute op_port.
105 106 107 |
# File 'lib/optimus_prime.rb', line 105 def op_port @op_port end |
#wait_for ⇒ Object (readonly)
Returns the value of attribute wait_for.
105 106 107 |
# File 'lib/optimus_prime.rb', line 105 def wait_for @wait_for end |
Instance Method Details
#clear! ⇒ Object
123 124 125 |
# File 'lib/optimus_prime.rb', line 123 def clear! ::Faraday.get("http://localhost:#{op_port}/clear") end |
#count(path_name) ⇒ Object
127 128 129 130 |
# File 'lib/optimus_prime.rb', line 127 def count(path_name) requests = ::Faraday.get("http://localhost:#{op_port}/requests/#{path_name}").body JSON.parse(requests)["count"] end |
#last_request_for(path_name) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/optimus_prime.rb', line 132 def last_request_for(path_name) seconds = 0 while :waiting return {} if seconds > wait_for.to_f sleep(0.1) seconds += 0.1 requests = ::Faraday.get("http://localhost:#{op_port}/requests/#{path_name}").body last_request = JSON.parse(requests)["last_request"] return last_request if !last_request.nil? && !last_request.empty? end end |
#prime(path_name, response = "", options = {}) ⇒ Object
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/optimus_prime.rb', line 112 def prime(path_name, response="", ={}) connection = ::Faraday.new("http://localhost:#{op_port}") connection.post do |request| request.url('/prime') request.headers['Accept'] = 'application/json' request.headers['Content-Type'] = 'application/json' # request.headers['Access-Control-Allow-Meth'] = 'post' request.body = ({ path_name: path_name, response: response }.merge!()).to_json end end |
#wait_until_request(path_name, &block) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/optimus_prime.rb', line 145 def wait_until_request(path_name, &block) seconds = 0 while :waiting sleep(0.1) raise "Timeout - waited for: #{wait_for}. \n--> No requests have been made to: #{path_name} endpoint." if seconds > wait_for.to_f seconds += 0.1 request = ::Faraday.get("http://localhost:#{op_port}/requests/#{path_name}").body last_request = JSON.parse(request)["last_request"] begin return true if block.call(last_request) rescue Exception => e raise "#{e}" if seconds > wait_for.to_f end end end |