Class: GuardianSearcher::Options

Inherits:
Hash
  • Object
show all
Defined in:
lib/guardian_searcher/options.rb

Overview

the class maps the options passed to the ones needed by the Guardian API for searching

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Options

Returns a new instance of Options.



17
18
19
20
21
22
# File 'lib/guardian_searcher/options.rb', line 17

def initialize(options)
  super
  raise OptionsNotHashError unless options.is_a?(Hash)

  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &blk) ⇒ Object



7
8
9
10
11
# File 'lib/guardian_searcher/options.rb', line 7

def method_missing(method_name, *args, &blk)
  return options.[](method_name, &blk) if @options.key?(method_name)

  super(method_name, *args, &blk)
end

Instance Method Details

#build_optionsObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/guardian_searcher/options.rb', line 24

def build_options
  return {} if options.empty?

  opt = ""
  options.each do |key, value|
    valid_option?(key)
    opt += "&#{map_option(key)}=#{value}"
  end
  return opt
end

#map_option(key) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/guardian_searcher/options.rb', line 39

def map_option(key)
  {
    from_date: "from-date",
    to_date: "to-date",
    page_size: "page-size",
    page: "page"
  }[key]
end

#respond_to_missing?(method_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(method_name, *args)
  @options.key?(method_name) || super(method_name, *args)
end

#valid_option?(option) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



35
36
37
# File 'lib/guardian_searcher/options.rb', line 35

def valid_option?(option)
  raise OptionsNotSupportedError unless %i[from_date to_date page_size page].include?(option)
end