Class: AocCli::Core::Resource

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#methodObject (readonly)

Returns the value of attribute method.



7
8
9
# File 'lib/aoc_cli/core/resource.rb', line 7

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/aoc_cli/core/resource.rb', line 7

def params
  @params
end

#scopeObject (readonly)

Returns the value of attribute scope.



7
8
9
# File 'lib/aoc_cli/core/resource.rb', line 7

def scope
  @scope
end

#urlObject (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

#fetchObject



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_markdownObject

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