Module: Hungry::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/hungry/util.rb

Instance Method Summary collapse

Instance Method Details

#is_numeric?(value) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/hungry/util.rb', line 32

def is_numeric?(value)
  !!(value.to_s =~ /^[+-]?[\d]+(\.[\d]+){0,1}$/)
end

#log(message, type = :info) ⇒ Object



7
8
9
10
11
12
# File 'lib/hungry/util.rb', line 7

def log(message, type = :info)
  if Hungry.logger
    message = "[Hungry] #{message}"
    Hungry.logger.send(type, message)
  end
end

#params_from_uri(uri) ⇒ Object



18
19
20
21
# File 'lib/hungry/util.rb', line 18

def params_from_uri(uri)
  uri = URI.parse(uri) unless uri.is_a?(URI)
  parse_query(uri.query) if uri.query.present?
end

#parse_json(json) ⇒ Object



14
15
16
# File 'lib/hungry/util.rb', line 14

def parse_json(json)
  Hungry.json_parser.call(json)
end

#uri_with_params(uri, params = {}) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/hungry/util.rb', line 23

def uri_with_params(uri, params = {})
  params = params.map { |k,v| "#{k}=#{CGI.escape(v.to_s)}" }
                 .join('&')
                 .presence
  
  [uri, params].compact
               .join('?')
end