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.
56 57 58 59 |
# File 'lib/optimus_prime.rb', line 56 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.
54 55 56 |
# File 'lib/optimus_prime.rb', line 54 def op_port @op_port end |
#wait_for ⇒ Object (readonly)
Returns the value of attribute wait_for.
54 55 56 |
# File 'lib/optimus_prime.rb', line 54 def wait_for @wait_for end |
Instance Method Details
#clear! ⇒ Object
72 73 74 |
# File 'lib/optimus_prime.rb', line 72 def clear! ::Faraday.get("http://localhost:#{op_port}/clear") end |
#count(path_name) ⇒ Object
76 77 78 79 |
# File 'lib/optimus_prime.rb', line 76 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
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/optimus_prime.rb', line 81 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
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/optimus_prime.rb', line 61 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/optimus_prime.rb', line 94 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 |