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
# File 'lib/web_fetch/request.rb', line 8

def initialize
  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

#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

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

Raises:

  • (ArgumentError)


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

Returns:

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

#methodObject



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_hObject



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