Class: Ugigi::Parser

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

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



5
6
7
8
# File 'lib/ugigi/parser.rb', line 5

def initialize
  @agent = Mechanize.new
  @agent.user_agent = "Ugigi Ruby #{Ugigi::VERSION}"
end

Instance Method Details

#fetch(args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ugigi/parser.rb', line 10

def fetch(args)
  case args[:only]
  when :sosowa
    args = args.update(:sswp => 0, :compe => 0)
  when :compe
    args = args.update(:sswl => 0, :sswp => 0)
  when :sswp
    args = args.update(:sswl => 0, :compe => 0)
  end
  args.delete(:only) if args.has_key? :only
  params = Ugigi.serialize_parameter(args)
  page = @agent.get(URI.join(BASE_URL, params))
  data = JSON.parse(page.body)
  indexes = []
  data.each do |e|
    index = Index.new(e)
    case args[:style]
    when :sosowa
      index.element = index.to_sosowa_index
      indexes << index
    else
      indexes << index
    end
  end
  return indexes
end

#total_count(args) ⇒ Object



37
38
39
40
41
# File 'lib/ugigi/parser.rb', line 37

def total_count(args)
  params = Ugigi.serialize_parameter(args)
  page = @agent.get(URI.join(BASE_URL_NOAPI, params))
  return page.search(%{div[@class="info"]}).children[0].text.strip.split("/")[1].to_i
end