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.
-
#query ⇒ Object
Returns the value of attribute query.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
-
.build_request(hash) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity.
- .from_hash(hash) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
-
#initialize {|_self| ... } ⇒ Request
constructor
A new instance of Request.
- #method ⇒ Object
- #method=(val) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Request
Returns a new instance of Request.
8 9 10 |
# File 'lib/web_fetch/request.rb', line 8 def initialize 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 |
#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
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/web_fetch/request.rb', line 51 def build_request(hash) Request.new do |request| request.url = hash.delete(:url) if hash.key?(:url) request.query = hash.delete(:query) if hash.key?(:query) request.headers = hash.delete(:headers) if hash.key?(:headers) request.body = hash.delete(:body) if hash.key?(:body) request.method = hash.delete(:method) if hash.key?(:method) request.custom = hash.delete(:custom) if hash.key?(:custom) end end |
.from_hash(hash) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/web_fetch/request.rb', line 41 def self.from_hash(hash) hash_copy = hash.dup request = build_request(hash_copy) raise ArgumentError, "Unrecognized keys: #{hash}" unless hash_copy.empty? request end |
Instance Method Details
#==(other) ⇒ Object
37 38 39 |
# File 'lib/web_fetch/request.rb', line 37 def ==(other) eql?(other) end |
#eql?(other) ⇒ Boolean
31 32 33 34 35 |
# File 'lib/web_fetch/request.rb', line 31 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 |
#method ⇒ Object
16 17 18 |
# File 'lib/web_fetch/request.rb', line 16 def method @method ||= :get end |
#method=(val) ⇒ Object
12 13 14 |
# File 'lib/web_fetch/request.rb', line 12 def method=(val) @method = val.downcase.to_sym end |
#to_h ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/web_fetch/request.rb', line 20 def to_h { url: url, query: query, headers: headers, body: body, method: method, custom: custom } end |