class ChambaSearchMx::UrlFormatter
it formats urls base on hash options @see ChambaSearchMx::LOCATIONS @see ChambaSearchMx::SALARIES @see ChambaSearchMx::DAYS
Public Class Methods
new(opts = {})
click to toggle source
# File lib/chamba_search_mx/url_formatter.rb, line 8 def initialize(opts = {}) @locations = opts[:locations] || ChambaSearchMx::LOCATIONS @salaries = opts[:salaries] || ChambaSearchMx::SALARIES @days = opts[:days] || ChambaSearchMx::DAYS end
Public Instance Methods
format(query, *options)
click to toggle source
@param <query> string “web developer” @params *<options> :location, :salary, :days
# File lib/chamba_search_mx/url_formatter.rb, line 17 def format(query, *options) @query = query @options = options.inject({}) { |acc, elem| acc.merge! elem } "/#{location}#{salary}#{days}/#{query_slug}" end
Private Instance Methods
argument_error(arg_name, available_options)
click to toggle source
Argument error parser
# File lib/chamba_search_mx/url_formatter.rb, line 83 def argument_error(arg_name, available_options) msg = "#{arg_name}: {OPTION} |" msg += "avalilable options #{available_options.keys.join(', ')}" raise ArgumentError, msg end
days()
click to toggle source
returns the days string based on options @raise Argument error when option is not valid
# File lib/chamba_search_mx/url_formatter.rb, line 73 def days return unless @options.key? :days return if @options[:days] == 'default' error = proc { argument_error 'days', @days } error.call unless option_exists?(@days, :days) "/#{@days[@options[:days].to_s.to_sym]}" end
downcase()
click to toggle source
string downcase
# File lib/chamba_search_mx/url_formatter.rb, line 33 def downcase @query.downcase end
location()
click to toggle source
returns the location string based on options or default @raise Argument error when option is not valid
# File lib/chamba_search_mx/url_formatter.rb, line 52 def location return @locations[:default] unless @options[:location] error = proc { argument_error 'location', @locations } error.call unless option_exists?(@locations, :location) @locations[@options[:location].to_sym] end
option_exists?(hash, key)
click to toggle source
gets value from the options hash @returm <boolean>
# File lib/chamba_search_mx/url_formatter.rb, line 92 def option_exists?(hash, key) hash.key? @options[key].to_s.to_sym end
query_slug()
click to toggle source
converts query string into a url slug
# File lib/chamba_search_mx/url_formatter.rb, line 27 def query_slug remove_white_spaces(remove_spaces(downcase)) end
remove_spaces(str)
click to toggle source
replace space for -
# File lib/chamba_search_mx/url_formatter.rb, line 39 def remove_spaces(str) str.strip.tr(' ', '-') end
remove_white_spaces(str)
click to toggle source
removes extra white spaces
# File lib/chamba_search_mx/url_formatter.rb, line 45 def remove_white_spaces(str) str.gsub(/[^\w-]/, '') end
salary()
click to toggle source
returns the salary string based on options @raise Argument error when option is not valid
# File lib/chamba_search_mx/url_formatter.rb, line 62 def salary return unless @options.key? :salary return if @options[:salary] == 'default' error = proc { argument_error 'salary', @salaries } error.call unless option_exists?(@salaries, :salary) "/#{@salaries[@options[:salary].to_sym]}" end