Class: Sites::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sites/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_type: :programming) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
# File 'lib/sites/base.rb', line 6

def initialize(job_type: :programming)
  @job_type = job_type
  @url = build_url
  @doc = Nokogiri::HTML(open_page(@url))
  @current_time = Time.new
  @timestamp = @current_time.strftime("%Y%m%d%H%M%S")
  @count = get_count
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



4
5
6
# File 'lib/sites/base.rb', line 4

def doc
  @doc
end

#job_typeObject (readonly)

Returns the value of attribute job_type.



4
5
6
# File 'lib/sites/base.rb', line 4

def job_type
  @job_type
end

#urlObject (readonly)

Returns the value of attribute url.



4
5
6
# File 'lib/sites/base.rb', line 4

def url
  @url
end

Instance Method Details

#open_page(url) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/sites/base.rb', line 15

def open_page(url)
  sleep(rand(0..2.0)) unless ENV['RAILS_ENV'] == 'test' # less mechanical behaviour

  if ENV['RAILS_ENV'] == 'test'
    open(url)
  else
    open(url, 'User-Agent' => user_agent)
  end
end