Class: Ruboty::Bokete::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.



8
9
10
11
# File 'lib/ruboty/bokete/client.rb', line 8

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

Instance Method Details

#get(mode) ⇒ Object



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

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)
      p images
      results.concat(images)

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

  results
end

#get_next_page(page) ⇒ Object



46
47
48
# File 'lib/ruboty/bokete/client.rb', line 46

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

#scraping_from_page(page) ⇒ Object



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

def scraping_from_page(page)
  page.search('.boke').map {|node|
    node.children.each_with_object({}) do |child, hash|
      case child.name
      when 'div'
        img = child.children.children.children.detect {|o| o.name == 'img' }
        hash[:url] ||= img.attribute('src').value rescue nil
      when 'h3'
        hash[:boke] = child.text
      end
    end
  }.reject(&:empty?)
end