Class: WebFetch::Request
- Inherits:
-
Object
- Object
- WebFetch::Request
- Defined in:
- lib/web_fetch/request.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#custom ⇒ Object
Returns the value of attribute custom.
-
#headers ⇒ Object
Returns the value of attribute headers.
- #method ⇒ Object
-
#query ⇒ Object
Returns the value of attribute query.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
-
#initialize {|_self| ... } ⇒ Request
constructor
A new instance of Request.
- #to_h ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Request
Returns a new instance of Request.
8 9 10 11 |
# File 'lib/web_fetch/request.rb', line 8 def initialize @method = 'GET' yield self end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
6 7 8 |
# File 'lib/web_fetch/request.rb', line 6 def body @body end |
#custom ⇒ Object
Returns the value of attribute custom.
6 7 8 |
# File 'lib/web_fetch/request.rb', line 6 def custom @custom end |
#headers ⇒ Object
Returns the value of attribute headers.
6 7 8 |
# File 'lib/web_fetch/request.rb', line 6 def headers @headers end |
#method ⇒ Object
13 14 15 |
# File 'lib/web_fetch/request.rb', line 13 def method @method.downcase.to_sym end |
#query ⇒ Object
Returns the value of attribute query.
6 7 8 |
# File 'lib/web_fetch/request.rb', line 6 def query @query end |
#url ⇒ Object
Returns the value of attribute url.
6 7 8 |
# File 'lib/web_fetch/request.rb', line 6 def url @url end |
Class Method Details
.build_request(hash) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/web_fetch/request.rb', line 48 def build_request(hash) Request.new do |request| %i[url query headers body method custom].each do |key| request.send("#{key}=", hash.delete(key)) end end end |
.from_hash(hash, options = {}) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/web_fetch/request.rb', line 38 def self.from_hash(hash, = {}) hash_copy = hash.dup request = build_request(hash_copy) return request unless .fetch(:validate, true) raise ArgumentError, "Unrecognized keys: #{hash}" unless hash_copy.empty? request end |
Instance Method Details
#==(other) ⇒ Object
34 35 36 |
# File 'lib/web_fetch/request.rb', line 34 def ==(other) eql?(other) end |
#eql?(other) ⇒ Boolean
28 29 30 31 32 |
# File 'lib/web_fetch/request.rb', line 28 def eql?(other) # Makes testing WebFetch a bit easier (based on real world case I hit # using WebFetch in a Rails app) other.to_h == to_h end |
#to_h ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/web_fetch/request.rb', line 17 def to_h { url: url, query: query, headers: headers, body: body, method: method, custom: custom } end |