Class: Spidy::Connector::Json

Inherits:
Object
  • Object
show all
Includes:
StaticAccessor
Defined in:
lib/spidy/connector/json.rb

Overview

OpenURI to JSON.parse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_agent:) ⇒ Json

Returns a new instance of Json.



11
12
13
# File 'lib/spidy/connector/json.rb', line 11

def initialize(user_agent:)
  @user_agent = user_agent
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/spidy/connector/json.rb', line 9

def logger
  @logger
end

Instance Method Details

#call(url, &block) ⇒ Object



15
16
17
18
# File 'lib/spidy/connector/json.rb', line 15

def call(url, &block)
  fail 'url is not specified' if url.blank?
  connect(url, &block)
end

#connect(url) ⇒ Object



20
21
22
23
24
# File 'lib/spidy/connector/json.rb', line 20

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