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
-
#link ⇒ Object
readonly
Returns the value of attribute link.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #completed? ⇒ Boolean
-
#initialize(service, connection, url, method, body = nil, options = {}) ⇒ Request
constructor
A new instance of Request.
- #location(env = @env) ⇒ Object
- #on_error ⇒ Object
- #on_or_after_complete(&block) ⇒ Object
- #params ⇒ 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, body = nil, options = {}) ⇒ Request
Returns a new instance of Request.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/leadlight/request.rb', line 36 def initialize(service, connection, url, method, body=nil, ={}) @options = self.connection = connection self.url = url self.http_method = method self.body = body self.service = service @completed = new_cond @state = :initialized @env = nil @response = nil @link = .fetch(:link) { NullLink.new(url) } super() end |
Instance Attribute Details
#link ⇒ Object (readonly)
Returns the value of attribute link.
29 30 31 |
# File 'lib/leadlight/request.rb', line 29 def link @link end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
29 30 31 |
# File 'lib/leadlight/request.rb', line 29 def @options end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
29 30 31 |
# File 'lib/leadlight/request.rb', line 29 def response @response end |
Instance Method Details
#completed? ⇒ Boolean
51 52 53 |
# File 'lib/leadlight/request.rb', line 51 def completed? :completed == @state end |
#location(env = @env) ⇒ Object
130 131 132 133 134 |
# File 'lib/leadlight/request.rb', line 130 def location(env=@env) env ||= {} url = env.fetch(:response_headers){{}}.fetch('location'){ env.fetch(:url){ self.url } } Addressable::URI.parse(url) end |
#on_error ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/leadlight/request.rb', line 90 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
105 106 107 108 109 110 111 112 113 |
# File 'lib/leadlight/request.rb', line 105 def on_or_after_complete(&block) synchronize do if completed? block.call(response) else on_complete(&block) end end end |
#params ⇒ Object
126 127 128 |
# File 'lib/leadlight/request.rb', line 126 def params link_params.merge(request_params) end |
#raise_on_error ⇒ Object
99 100 101 102 103 |
# File 'lib/leadlight/request.rb', line 99 def raise_on_error on_error do |representation| raise representation end end |
#represent(env) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/leadlight/request.rb', line 115 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]) representation. extend(Representation). initialize_representation(env[:leadlight_service], location(env), env[:response], self). extend(Hyperlinkable). apply_all_tints end |
#submit ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/leadlight/request.rb', line 55 def submit entity = type_map.to_entity_body(body) entity_body = entity.body content_type = entity.content_type connection.run_request(http_method, url.to_s, entity_body, {}) do |request| 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
84 85 86 87 |
# File 'lib/leadlight/request.rb', line 84 def submit_and_wait(&block) submit wait(&block) end |
#wait {|@env.fetch(:leadlight_representation)| ... } ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/leadlight/request.rb', line 76 def wait synchronize do @completed.wait_until{completed?} end yield(@env.fetch(:leadlight_representation)) if block_given? self end |