Class: SearchYJ::UriManager

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

Overview

Manage the URI instance

Author:

  • indeep-xyz

Constant Summary collapse

URI_BASE =
'http://search.yahoo.co.jp/search'
QUERY_DEFAULT =
{
# Search term
p: nil,
# Character encoding
ei: 'UTF-8',
# Suppress that advise to rewrite the search-term
qrw: 0,
# Flag for offset (?)
pstart: 1,
# Offset
# - if less than or equal to 1,
# - the search result is from first
b: 1
}
IndexError =
Class.new(StandardError)
PageSizeError =
Class.new(StandardError)
SearchTermError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(query = {}) ⇒ UriManager

Returns a new instance of UriManager.



30
31
32
# File 'lib/searchyj/uri_manager.rb', line 30

def initialize(query = {})
  @query = QUERY_DEFAULT.merge(query)
end

Instance Method Details

#baseObject



42
43
44
# File 'lib/searchyj/uri_manager.rb', line 42

def base
  URI_BASE
end

#indexObject



38
39
40
# File 'lib/searchyj/uri_manager.rb', line 38

def index
  (@query[:b] < 1) ? 1 : @query[:b]
end

#index=(index) ⇒ Object



52
53
54
55
56
57
# File 'lib/searchyj/uri_manager.rb', line 52

def index=(index)
  fail IndexError unless index.is_a?(Integer)
  fail IndexError if index < 1

  @query[:b] = index
end

#move_index(distance) ⇒ Object



65
66
67
68
# File 'lib/searchyj/uri_manager.rb', line 65

def move_index(distance)
  @query[:b] += distance
  @query[:b]  = 1 if @query[:b] < 1
end

#search_termObject



34
35
36
# File 'lib/searchyj/uri_manager.rb', line 34

def search_term
  @query[:p]
end

#search_term=(search_term) ⇒ Object



46
47
48
49
50
# File 'lib/searchyj/uri_manager.rb', line 46

def search_term=(search_term)
  fail SearchTermError unless search_term.is_a?(String)

  @query[:p] = search_term
end

#to_sObject



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

def to_s
  uri = URI(URI_BASE)
  uri.query = create_query_string
  uri.to_s
end