Class: Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ruboty/bokete/client.rb

Constant Summary collapse

BOKETE_URL =
'http://bokete.jp'

Instance Method Summary collapse

Constructor Details

#initialize(max_results: 30) ⇒ Client

Returns a new instance of Client.



6
7
8
9
# File 'lib/ruboty/bokete/client.rb', line 6

def initialize(max_results: 30)
  @max_results = max_results
  @agent = Mechanize.new
end

Instance Method Details

#get(mode) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruboty/bokete/client.rb', line 11

def get(mode)
  results = []

  @agent.get("#{BOKETE_URL}/boke/#{mode}") do |page|
    target_page = page

    while results.size < @max_results
      images = scraping_from_page(target_page)
      results.concat(images)

      target_page = get_next_page(target_page)
      break if target_page.nil?
    end
  end

  results
end

#get_next_page(page) ⇒ Object



44
45
46
# File 'lib/ruboty/bokete/client.rb', line 44

def get_next_page(page)
  page.links_with(href: /\?page/)[-1].click rescue nil
end

#scraping_from_page(page) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruboty/bokete/client.rb', line 29

def scraping_from_page(page)
  images = []

  page.search('a>img').each do |node|
    image = node.attribute('src').value rescue nil
    alt = node.attribute('alt').value rescue nil

    if image && alt && image.match(%r|\A//|)
      images << "http:#{image}\n#{alt}"
    end
  end

  images
end