Module: Spaceborne
- Defined in:
- lib/spaceborne.rb,
lib/spaceborne/version.rb
Overview
module for apitesting with spaceborne
Constant Summary collapse
- VERSION =
'0.1.19'.freeze
Instance Method Summary collapse
- #add_request ⇒ Object
- #add_response ⇒ Object
- #add_time ⇒ Object
- #json?(headers) ⇒ Boolean
- #request_info(str = "\n") ⇒ Object
- #response_body ⇒ Object
- #wrap_request ⇒ Object
Instance Method Details
#add_request ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/spaceborne.rb', line 18 def add_request if @request_body "REQUEST: #{response.request.method.upcase} #{response.request.url}\n"\ " HEADERS:\n#{JSON.pretty_generate(response.request.headers)}\n"\ " PAYLOAD:\n#{@request_body}\n" else '' end end |
#add_response ⇒ Object
28 29 30 31 32 |
# File 'lib/spaceborne.rb', line 28 def add_response "RESPONSE: #{response.code}\n"\ " HEADERS:\n#{JSON.pretty_generate(response.headers)}\n"\ << response_body end |
#add_time ⇒ Object
14 15 16 |
# File 'lib/spaceborne.rb', line 14 def add_time "TIME: #{Time.now.strftime('%d/%m/%Y %H:%M')}\n" end |
#json?(headers) ⇒ Boolean
9 10 11 12 |
# File 'lib/spaceborne.rb', line 9 def json?(headers) headers.key?(:content_type) && headers[:content_type].include?('application/json') end |
#request_info(str = "\n") ⇒ Object
44 45 46 47 |
# File 'lib/spaceborne.rb', line 44 def request_info(str = "\n") str << add_time << add_request << add_response str end |
#response_body ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/spaceborne.rb', line 34 def response_body return '' if response.request.method.casecmp('head').zero? str = if json?(response.headers) " JSON_BODY\n#{JSON.pretty_generate(json_body)}\n" else " BODY\n#{response.body}\n" end str end |
#wrap_request ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/spaceborne.rb', line 49 def wrap_request yield rescue Exception => e raise e unless response e. << request_info raise e end |