Class: Honeysearch::Search

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term, page = 0) ⇒ Search

Returns a new instance of Search.



15
16
17
18
# File 'lib/honeysearch.rb', line 15

def initialize(term, page=0)
  @term = term
  @page = page
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



13
14
15
# File 'lib/honeysearch.rb', line 13

def page
  @page
end

Instance Method Details

#bodyObject



39
40
41
# File 'lib/honeysearch.rb', line 39

def body
  @body ||= res.body.force_encoding('UTF-8')
end

#more?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/honeysearch.rb', line 47

def more?
  body.include?('次の30曲')
end

#resObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/honeysearch.rb', line 20

def res
  @res ||= begin
    uri = URI("#{SEARCH_URL}?p=#{@page}")
    Net::HTTP.post_form(
      uri,
      sel_rows1: 30,
      topsearch_box: @term,
      sel_rows2: 30,
      s_title: '',
      s_singer: '',
      s_intro: '',
      s_tieup: '',
      search_genre: 0,
      sel_search_genre: 0,
      method: 0
    )
  end
end

#resultObject



43
44
45
# File 'lib/honeysearch.rb', line 43

def result
  body
end

#total_numberObject



51
52
53
# File 'lib/honeysearch.rb', line 51

def total_number
  body.match(/検索結果\uff1a(?<number>[0-9]+)曲/)[:number].to_i
end