Class: WebFetch::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/web_fetch/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Request

Returns a new instance of Request.

Yields:

  • (_self)

Yield Parameters:



8
9
10
11
# File 'lib/web_fetch/request.rb', line 8

def initialize
  @method = 'GET'
  yield self
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



6
7
8
# File 'lib/web_fetch/request.rb', line 6

def body
  @body
end

#customObject

Returns the value of attribute custom.



6
7
8
# File 'lib/web_fetch/request.rb', line 6

def custom
  @custom
end

#headersObject

Returns the value of attribute headers.



6
7
8
# File 'lib/web_fetch/request.rb', line 6

def headers
  @headers
end

#methodObject



13
14
15
# File 'lib/web_fetch/request.rb', line 13

def method
  @method.downcase.to_sym
end

#queryObject

Returns the value of attribute query.



6
7
8
# File 'lib/web_fetch/request.rb', line 6

def query
  @query
end

#urlObject

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

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
# File 'lib/web_fetch/request.rb', line 38

def self.from_hash(hash, options = {})
  hash_copy = hash.dup
  request = build_request(hash_copy)
  return request unless options.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

Returns:

  • (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_hObject



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