Class: Popularity::Crawler

Inherits:
Object
  • Object
show all
Defined in:
lib/popularity/crawler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Crawler

Returns a new instance of Crawler.



10
11
12
# File 'lib/popularity/crawler.rb', line 10

def initialize(url)
  @url = url
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/popularity/crawler.rb', line 8

def url
  @url
end

Instance Method Details

#async_done?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/popularity/crawler.rb', line 35

def async_done?
  @async_done
end

#fetchObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/popularity/crawler.rb', line 65

def fetch
  return false unless valid?
    
  begin
    response = Unirest.get(request_url)
    @response = response.raw_body
  rescue OpenURI::HTTPError, Timeout::Error, SocketError
    nil
  end
end

#fetch_async(&block) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/popularity/crawler.rb', line 55

def fetch_async(&block)
  return false unless valid?

  Unirest.get(request_url) do |response|
    @async_done = true
    @response = response.raw_body
    block.call(response.code, response.raw_body) if block_given? 
  end
end

#has_response?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
# File 'lib/popularity/crawler.rb', line 22

def has_response?
  response #fetch it

  return false if response.nil? 
  return false if response.empty?

  true
end

#hostObject



39
40
41
# File 'lib/popularity/crawler.rb', line 39

def host
  URI.parse(@url).host.gsub('www.', '')
end

#nameObject



47
48
49
# File 'lib/popularity/crawler.rb', line 47

def name
  self.class.to_s.split('::').last.gsub(/(.)([A-Z])/,'\1_\2').downcase
end

#request_urlObject

Raises:

  • (NotImplemented)


14
15
16
# File 'lib/popularity/crawler.rb', line 14

def request_url
  raise NotImplemented
end

#responseObject



18
19
20
# File 'lib/popularity/crawler.rb', line 18

def response
  @response ||= fetch
end

#response_jsonObject



43
44
45
# File 'lib/popularity/crawler.rb', line 43

def response_json
  @json ||= JSON.parse(response)
end

#to_json(options = {}) ⇒ Object



51
52
53
# File 'lib/popularity/crawler.rb', line 51

def to_json(options = {})
  as_json(options)
end

#valid?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/popularity/crawler.rb', line 31

def valid? 
  true # to be overridden in subclasses
end