Class: SearchYJ::Main

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

Instance Method Summary collapse

Instance Method Details

#detect(term, regexp, key = :title, **args) ⇒ Hash

Detect a first record that meet the conditions of a regexp and a key.

Parameters:

  • term (String)

    Search term

  • regexp (Regexp)

    Want to match with value of a record

  • key (Symbol) (defaults to: :title)

    The key name for comparing values

Returns:

  • (Hash)

    A result record if matched the arguments Else nil

[View source]

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/searchyj/main.rb', line 14

def detect(term, regexp, key = :title, **args)
  key = key.to_sym unless key.is_a?(Symbol)

  searcher = Searcher.new(args)
  searcher.uri.search_term = term
  searcher.pager.size      = 100

  searcher.run do |record|
    if regexp.match(record[key])
      return record
    end
  end

  nil
end

#list(term, size = 10, **args) ⇒ Array

Get records of the search result.

Parameters:

  • term (String)

    Search term

  • size (Integer) (defaults to: 10)

    The size of the returner

  • args (Hash)

Returns:

  • (Array)

    Includes the result records

[View source]

37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/searchyj/main.rb', line 37

def list(term, size = 10, **args)
  searcher = Searcher.new(args)
  searcher.uri.search_term = term
  searcher.pager.size      = size
  list = []

  searcher.run do |record|
    list << record
    break if list.size >= size
  end

  list
end

#rank(term, rank, **args) ⇒ Hash

Get a record in the search result at a particular rank order in the search ranking.

Parameters:

  • term (String)

    Search term

  • rank (Integer)

    The rank order in the search ranking

Returns:

  • (Hash)

    A result record if matched the arguments Else nil

[View source]

59
60
61
62
63
# File 'lib/searchyj/main.rb', line 59

def rank(term, rank, **args)
  args[:from] = rank
  result = list(term, 1, args)
  (result.size > 0) ? result[0] : nil
end