Class: Popularity::Crawler
- Inherits:
-
Object
- Object
- Popularity::Crawler
show all
- Defined in:
- lib/popularity/crawler.rb
Direct Known Subclasses
Facebook, Github, GooglePlus, Medium, Pinterest, RedditComment, RedditPost, RedditShare, Rubygems, Soundcloud, Twitter
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
#url ⇒ Object
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
35
36
37
|
# File 'lib/popularity/crawler.rb', line 35
def async_done?
@async_done
end
|
#fetch ⇒ Object
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
22
23
24
25
26
27
28
29
|
# File 'lib/popularity/crawler.rb', line 22
def has_response?
response
return false if response.nil?
return false if response.empty?
true
end
|
#host ⇒ Object
39
40
41
|
# File 'lib/popularity/crawler.rb', line 39
def host
URI.parse(@url).host.gsub('www.', '')
end
|
#name ⇒ Object
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_url ⇒ Object
14
15
16
|
# File 'lib/popularity/crawler.rb', line 14
def request_url
raise NotImplemented
end
|
#response ⇒ Object
18
19
20
|
# File 'lib/popularity/crawler.rb', line 18
def response
@response ||= fetch
end
|
#response_json ⇒ Object
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
31
32
33
|
# File 'lib/popularity/crawler.rb', line 31
def valid?
true
end
|