Class: RubyHackernews::EntryService

Inherits:
Object
  • Object
show all
Includes:
MechanizeContext
Defined in:
lib/ruby-hackernews/services/entry_service.rb

Instance Method Summary collapse

Methods included from MechanizeContext

#[], #agent, agent=, #authenticated?, #require_authentication

Instance Method Details

#ask(title, text) ⇒ Object



45
46
47
48
49
50
# File 'lib/ruby-hackernews/services/entry_service.rb', line 45

def ask(title, text)
  require_authentication
  form = agent.get(ConfigurationService.submit_url).forms.first
  submit_question(form, title, text)
  return true
end

#find_by_id(id) ⇒ Object



20
21
22
23
24
# File 'lib/ruby-hackernews/services/entry_service.rb', line 20

def find_by_id(id)
  page = agent.get(ConfigurationService.base_url + "item?id=#{id}")
  lines = page.search("table")[2].search("tr")
  return EntryParser.new(lines[0], lines[1]).parse
end

#get_entries(pages = 1, url = ConfigurationService.base_url) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ruby-hackernews/services/entry_service.rb', line 6

def get_entries(pages = 1, url = ConfigurationService.base_url)
  parser = EntryPageParser.new(agent.get(url))
   = []
  pages.times do
    lines = parser.get_lines
    (lines.length / 2).times do
       << EntryParser.new(lines.shift, lines.shift).parse
    end
    next_url = parser.get_next_url || break
    parser = EntryPageParser.new(agent.get(next_url))
  end
  return 
end

#get_jobs(pages = 1) ⇒ Object



34
35
36
# File 'lib/ruby-hackernews/services/entry_service.rb', line 34

def get_jobs(pages = 1)
  return get_entries(pages, ConfigurationService.jobs_url)
end

#get_new_entries(pages = 1) ⇒ Object



26
27
28
# File 'lib/ruby-hackernews/services/entry_service.rb', line 26

def get_new_entries(pages = 1)
  return get_entries(pages, ConfigurationService.new_url)
end

#get_questions(pages = 1) ⇒ Object



30
31
32
# File 'lib/ruby-hackernews/services/entry_service.rb', line 30

def get_questions(pages = 1)
  return get_entries(pages, ConfigurationService.ask_url)
end

#submit(title, url) ⇒ Object



38
39
40
41
42
43
# File 'lib/ruby-hackernews/services/entry_service.rb', line 38

def submit(title, url)
  require_authentication
  form = agent.get(ConfigurationService.submit_url).forms.first
  submit_link(form, title, url)
  return true
end