Class: SearchYJ::PageSizeAdjuster

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

Overview

Manage cookie for page

Constant Summary collapse

SIZE_PATTERN =
[10, 15, 20, 30, 40, 100]
SIZE_DEFAULT =
'sB="n=<<n>>&nw=-1&fp_ipod=0&fp_pl=0"; ' \
'path=/; ' \
'expire=<<expire>>'
EXPIRE_DELAY =
60 * 60 * 24

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePageSizeAdjuster

Initialize myself.



20
21
22
23
# File 'lib/searchyj/page_size_adjuster.rb', line 20

def initialize
  @size   = SIZE_DEFAULT
  @expire = Time.now.to_i + EXPIRE_DELAY
end

Instance Attribute Details

#sizeObject

Returns the value of attribute size.



9
10
11
# File 'lib/searchyj/page_size_adjuster.rb', line 9

def size
  @size
end

Instance Method Details

Attach the cookie string to the argument if @size has differed from the default value.

Parameters:

  • hash (Hash)

Returns:

  • (Hash)


46
47
48
49
50
51
52
# File 'lib/searchyj/page_size_adjuster.rb', line 46

def attach_cookie(hash)
  if @size > SIZE_DEFAULT
    hash['Cookie'] = create_cookie
  end

  hash
end

#optimize_page_size(size) ⇒ Number

Optimize the number of the page size for searching.

Parameters:

  • size (Number)

    The number of the page size

Returns:

  • (Number)

    The optimized number



33
34
35
36
37
38
39
# File 'lib/searchyj/page_size_adjuster.rb', line 33

def optimize_page_size(size)
  SIZE_PATTERN.reverse_each do |n|
    return n if size >= n
  end

  SIZE_DEFAULT
end