Class: Vessel::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/vessel/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler:, url: nil, data: nil, page: nil, attempt: 1) ⇒ Response

Returns a new instance of Response.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vessel/response.rb', line 16

def initialize(handler:, url: nil, data: nil, page: nil, attempt: 1)
  @url = Addressable::URI.parse(url) if url
  @data = data
  @handler = handler
  @attempt = attempt
  @driven_page = page
  @page = @driven_page&.page
  @rejected = false
  @cookies = @driven_page&.cookies&.map do |c|
    {
      name: c.name, value: c.value, domain: c.domain, path: c.path,
      httponly: c.httponly?, secure: c.secure?, expires: c.expires
    }
  end
end

Instance Attribute Details

#attemptObject (readonly)

Returns the value of attribute attempt.



12
13
14
# File 'lib/vessel/response.rb', line 12

def attempt
  @attempt
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



12
13
14
# File 'lib/vessel/response.rb', line 12

def cookies
  @cookies
end

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/vessel/response.rb', line 12

def data
  @data
end

#handlerObject (readonly) Also known as: callback

Returns the value of attribute handler.



12
13
14
# File 'lib/vessel/response.rb', line 12

def handler
  @handler
end

#pageObject (readonly)

Returns the value of attribute page.



12
13
14
# File 'lib/vessel/response.rb', line 12

def page
  @page
end

#rejectedObject

Returns the value of attribute rejected.



11
12
13
# File 'lib/vessel/response.rb', line 11

def rejected
  @rejected
end

#urlObject (readonly)

Returns the value of attribute url.



12
13
14
# File 'lib/vessel/response.rb', line 12

def url
  @url
end

Instance Method Details

#==(other) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/vessel/response.rb', line 91

def ==(other)
  url == other.url &&
    data == other.data &&
    handler == other.handler &&
    page == other.page &&
    attempt == other.attempt &&
    cookies == other.cookies
end

#absolute_url(relative, encode: true) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/vessel/response.rb', line 34

def absolute_url(relative, encode: true)
  if encode
    relative = url_decode(relative)
    relative = url_encode(relative)
  end
  join_url(url.to_s, relative)
end

#bodyObject



56
57
58
# File 'lib/vessel/response.rb', line 56

def body
  @body ||= Nokogiri::HTML(raw)
end

#closeObject



70
71
72
73
# File 'lib/vessel/response.rb', line 70

def close
  @driven_page&.close
  @driven_page = @page = nil
end

#headersObject



83
84
85
# File 'lib/vessel/response.rb', line 83

def headers
  @driven_page&.headers
end

#join_url(head, tail) ⇒ Object



42
43
44
# File 'lib/vessel/response.rb', line 42

def join_url(head, tail)
  Addressable::URI.join(head, tail).to_s
end

#rawObject



60
61
62
# File 'lib/vessel/response.rb', line 60

def raw
  @raw ||= @driven_page&.body
end

#sizeObject



87
88
89
# File 'lib/vessel/response.rb', line 87

def size
  @driven_page&.size
end

#statusObject



79
80
81
# File 'lib/vessel/response.rb', line 79

def status
  @driven_page&.status
end

#stub?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/vessel/response.rb', line 75

def stub?
  !url
end

#sync_body(seconds = 2) ⇒ Object



64
65
66
67
68
# File 'lib/vessel/response.rb', line 64

def sync_body(seconds = 2)
  sleep(seconds)
  @raw = @body = nil
  body
end

#url_decode(uri) ⇒ Object Also known as: uri_decode



51
52
53
# File 'lib/vessel/response.rb', line 51

def url_decode(uri)
  Addressable::URI.unencode(uri)
end

#url_encode(uri) ⇒ Object Also known as: uri_encode



46
47
48
# File 'lib/vessel/response.rb', line 46

def url_encode(uri)
  Addressable::URI.encode(uri)
end