Class: Leadlight::Request
- Inherits:
-
Object
- Object
- Leadlight::Request
- Extended by:
- Forwardable
- Includes:
- HookR::Hooks, HeaderHelpers, MonitorMixin
- Defined in:
- lib/leadlight/request.rb
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #completed? ⇒ Boolean
-
#initialize(service, connection, url, method, params = {}, body = nil) ⇒ Request
constructor
A new instance of Request.
- #on_error ⇒ Object
- #on_or_after_complete(&block) ⇒ Object
- #raise_on_error ⇒ Object
- #represent(env) ⇒ Object
- #submit ⇒ Object
- #submit_and_wait(&block) ⇒ Object (also: #then)
- #wait {|@env.fetch(:leadlight_representation)| ... } ⇒ Object
Methods included from HeaderHelpers
Constructor Details
#initialize(service, connection, url, method, params = {}, body = nil) ⇒ Request
Returns a new instance of Request.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/leadlight/request.rb', line 35 def initialize(service, connection, url, method, params={}, body=nil) self.connection = connection self.url = url self.http_method = method self.body = body self.params = params self.service = service @completed = new_cond @state = :initialized @env = nil @response = nil super() end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
28 29 30 |
# File 'lib/leadlight/request.rb', line 28 def response @response end |
Instance Method Details
#completed? ⇒ Boolean
49 50 51 |
# File 'lib/leadlight/request.rb', line 49 def completed? :completed == @state end |
#on_error ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/leadlight/request.rb', line 87 def on_error on_or_after_complete do |response| unless response.success? yield(response.env.fetch(:leadlight_representation)) end end self end |
#on_or_after_complete(&block) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/leadlight/request.rb', line 102 def on_or_after_complete(&block) synchronize do if completed? block.call(response) else on_complete(&block) end end end |
#raise_on_error ⇒ Object
96 97 98 99 100 |
# File 'lib/leadlight/request.rb', line 96 def raise_on_error on_error do |representation| raise representation end end |
#represent(env) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/leadlight/request.rb', line 112 def represent(env) content_type = env[:response_headers]['Content-Type'] content_type = clean_content_type(content_type) representation = type_map.to_native(content_type, env[:body]) location = Addressable::URI.parse(env[:response_headers].fetch('location'){ env[:url] }) representation. extend(Representation). initialize_representation(env[:leadlight_service], location, env[:response]). extend(Hyperlinkable). apply_all_tints end |
#submit ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/leadlight/request.rb', line 53 def submit entity = type_map.to_entity_body(body) entity_body = entity.body content_type = entity.content_type connection.run_request(http_method, url, entity_body, {}) do |request| request.params.update(params) unless params.empty? request.headers['Content-Type'] = content_type if content_type request.[:leadlight_request] = self execute_hook(:on_prepare_request, request) end.on_complete do |env| synchronize do @response = env.fetch(:response) execute_hook :on_complete, @response @env = env @state = :completed @completed.broadcast end end end |
#submit_and_wait(&block) ⇒ Object Also known as: then
81 82 83 84 |
# File 'lib/leadlight/request.rb', line 81 def submit_and_wait(&block) submit wait(&block) end |
#wait {|@env.fetch(:leadlight_representation)| ... } ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/leadlight/request.rb', line 73 def wait synchronize do @completed.wait_until{completed?} end yield(@env.fetch(:leadlight_representation)) if block_given? self end |