Class: Spidy::Connector::Json
- Inherits:
-
Object
- Object
- Spidy::Connector::Json
- Includes:
- StaticAccessor
- Defined in:
- lib/spidy/connector/json.rb
Overview
OpenURI to JSON.parse
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #call(url) ⇒ Object
- #connect(url) ⇒ Object
-
#initialize(user_agent:) ⇒ Json
constructor
A new instance of Json.
Constructor Details
#initialize(user_agent:) ⇒ Json
Returns a new instance of Json.
9 10 11 |
# File 'lib/spidy/connector/json.rb', line 9 def initialize(user_agent:) @user_agent = user_agent end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
7 8 9 |
# File 'lib/spidy/connector/json.rb', line 7 def logger @logger end |
Instance Method Details
#call(url) ⇒ Object
13 14 15 16 |
# File 'lib/spidy/connector/json.rb', line 13 def call(url, &) fail 'url is not specified' if url.blank? connect(url, &) end |
#connect(url) ⇒ Object
18 19 20 21 22 |
# File 'lib/spidy/connector/json.rb', line 18 def connect(url) OpenURI.open_uri(url, 'User-Agent' => @user_agent) { |body| yield JSON.parse(body.read, symbolize_names: true) } rescue OpenURI::HTTPError => e raise Spidy::Connector::Retry.new(error: e, response_code: e.io.status[0]) end |