Class: AocCli::Core::Resource
- Inherits:
-
Object
- Object
- AocCli::Core::Resource
- Defined in:
- lib/aoc_cli/core/resource.rb
Overview
Scope filters the response with the given xpath which ensures that only desired content is extracted from the (usually) HTML response. The whole response is considered the resource payload if no scope is specified.
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #fetch ⇒ Object
-
#fetch_markdown ⇒ Object
Bypess ignores unknown tags and tries to convert their nested content.
-
#initialize(url:, scope: nil, method: :get, params: {}) ⇒ Resource
constructor
A new instance of Resource.
Constructor Details
#initialize(url:, scope: nil, method: :get, params: {}) ⇒ Resource
Returns a new instance of Resource.
9 10 11 12 13 14 |
# File 'lib/aoc_cli/core/resource.rb', line 9 def initialize(url:, scope: nil, method: :get, params: {}) @url = url @scope = scope @method = method @params = params end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
7 8 9 |
# File 'lib/aoc_cli/core/resource.rb', line 7 def method @method end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
7 8 9 |
# File 'lib/aoc_cli/core/resource.rb', line 7 def params @params end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
7 8 9 |
# File 'lib/aoc_cli/core/resource.rb', line 7 def scope @scope end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
7 8 9 |
# File 'lib/aoc_cli/core/resource.rb', line 7 def url @url end |
Instance Method Details
#fetch ⇒ Object
16 17 18 19 20 |
# File 'lib/aoc_cli/core/resource.rb', line 16 def fetch return response if scope.nil? Nokogiri.HTML5(response).xpath(scope).to_html end |
#fetch_markdown ⇒ Object
Bypess ignores unknown tags and tries to convert their nested content. The parser adds an extra newline to the end which is removed with chomp.
24 25 26 |
# File 'lib/aoc_cli/core/resource.rb', line 24 def fetch_markdown ReverseMarkdown.convert(fetch, unknown_tags: :bypass).chomp end |