lib/hungry/util.rb in hungry-0.0.1 vs lib/hungry/util.rb in hungry-0.1.0
- old
+ new
@@ -1,15 +1,25 @@
-require 'cgi'
-require 'rack/utils'
+require 'cgi' unless defined?(CGI)
module Hungry
module Util
extend self
+ def log(message, type = :info)
+ if Hungry.logger
+ message = "[Hungry] #{message}"
+ Hungry.logger.send(type, message)
+ end
+ end
+
+ def parse_json(json)
+ Hungry.json_parser.call(json)
+ end
+
def params_from_uri(uri)
uri = URI.parse(uri) unless uri.is_a?(URI)
- Rack::Utils.parse_query(uri.query) if uri.query.present?
+ parse_query(uri.query) if uri.query.present?
end
def uri_with_params(uri, params = {})
params = params.map { |k,v| "#{k}=#{CGI.escape(v.to_s)}" }
.join('&')
@@ -18,9 +28,39 @@
[uri, params].compact
.join('?')
end
def is_numeric?(value)
- value.to_s =~ /^[+-]?[\d]+(\.[\d]+){0,1}$/
+ !!(value.to_s =~ /^[+-]?[\d]+(\.[\d]+){0,1}$/)
+ end
+
+ private
+
+ # Copied from rack/utils:
+ def unescape(s, encoding = Encoding::UTF_8)
+ URI.decode_www_form_component(s, encoding)
+ end
+
+ def parse_query(qs, d = nil, &unescaper)
+ unescaper ||= method(:unescape)
+
+ params = {}
+
+ (qs || '').split(d ? /[#{d}] */ : /[&;] */n).each do |p|
+ next if p.empty?
+ k, v = p.split('=', 2).map(&unescaper)
+
+ if cur = params[k]
+ if cur.class == Array
+ params[k] << v
+ else
+ params[k] = [cur, v]
+ end
+ else
+ params[k] = v
+ end
+ end
+
+ params
end
end
end