lib/fanforce/utils.rb in fanforce-0.2.0 vs lib/fanforce/utils.rb in fanforce-0.2.2

- old
+ new

@@ -2,86 +2,99 @@ require 'query_string_parser' module Fanforce::Utils def self.included(base) base.extend(self) end - def blank?(obj) + def is_blank?(obj) obj.respond_to?(:empty?) ? obj.empty? : !obj end - def present?(obj) - !blank?(obj) + def is_present?(obj) + !is_blank?(obj) end def valid_request?(params) return false if !params.is_a?(Hash) - return false if blank?(params[:fanforce_id]) - return false if blank?(params[:app_id]) and blank?(params[:behavior_id]) and blank?(params[:module_id]) and blank?(params[:widget_id]) + return false if is_blank?(params[:fanforce_id]) + return false if is_blank?(params[:app_id]) and is_blank?(params[:behavior_id]) and is_blank?(params[:module_id]) and is_blank?(params[:widget_id]) return true end def valid_install_request?(params) return false if !params.is_a?(Hash) - return false if blank?(params[:fanforce_id]) - return false if blank?(params[:app_id]) and blank?(params[:behavior_id]) and blank?(params[:module_id]) and blank?(params[:widget_id]) - return false if blank?(params[:api_key]) + return false if is_blank?(params[:fanforce_id]) + return false if is_blank?(params[:app_id]) and is_blank?(params[:behavior_id]) and is_blank?(params[:module_id]) and is_blank?(params[:widget_id]) + return false if is_blank?(params[:api_key]) return true end def valid_uninstall_request?(params) return false if !params.is_a?(Hash) - return false if blank?(params[:fanforce_id]) - return false if blank?(params[:app_id]) and blank?(params[:behavior_id]) and blank?(params[:module_id]) and blank?(params[:widget_id]) - return false if blank?(params[:api_key]) + return false if is_blank?(params[:fanforce_id]) + return false if is_blank?(params[:app_id]) and is_blank?(params[:behavior_id]) and is_blank?(params[:module_id]) and is_blank?(params[:widget_id]) + return false if is_blank?(params[:api_key]) return true end def parse_params(params) - if present?(params[:app_id]) + if is_present?(params[:app_id]) plugin_type = :app plugin_id = params[:app_id] - elsif present?(params[:behavior_id]) + elsif is_present?(params[:behavior_id]) plugin_type = :behavior plugin_id = params[:behavior_id] - elsif present?(params[:module_id]) + elsif is_present?(params[:module_id]) plugin_type = :module plugin_id = params[:module_id] - elsif present?(params[:widget_id]) + elsif is_present?(params[:widget_id]) plugin_type = :widget plugin_id = params[:widget_id] end {:"#{plugin_type}_id" => params["#{plugin_type}_id"], plugin_type: plugin_type, plugin_id: plugin_id, fanforce_id: params[:fanforce_id], api_key: params[:api_key]} end def parse_url(raw_url) - return if blank?(raw_url) + return if is_blank?(raw_url) url = URI::parse(raw_url) query_params = QueryStringParser.qs_parse(url.query).inject({}) do |result, (k,v)| result[k] = v if k !~ /^ff_.+/ result end - query_string = to_query_string(Hash[query_params.sort]) if present?(query_params) + query_string = to_query_string(Hash[query_params.sort]) if is_present?(query_params) _external_id = url.host + url.path - clean_url = "#{url.scheme}://#{url.host}#{(if ![80,443].include?(url.port) then ":#{url.port}" end)}#{url.path}#{(if present?(query_string) then "?#{query_string}" end)}" + clean_url = "#{url.scheme}://#{url.host}#{(if ![80,443].include?(url.port) then ":#{url.port}" end)}#{url.path}#{(if is_present?(query_string) then "?#{query_string}" end)}" { _external_id: _external_id, clean_url: clean_url, raw_url: raw_url, query_params: query_params, query_string: query_string, scheme: url.scheme, host: url.host, port: url.port, path: url.path, fragment: url.fragment } end - def to_query_string(params, namespace = nil) - if params.is_a?(String) - params - elsif params.is_a?(Array) - params.collect { |value| to_query_chunk(value, "#{namespace}[]") }.join '&' - elsif params.is_a?(Hash) - params.collect { |key, value| to_query_chunk(value, namespace ? "#{namespace}[#{key}]" : key) }.sort * '&' + def to_query_string(obj, namespace=nil) + return '' if is_blank?(obj) + if obj.is_a?(Array) + obj.collect { |value| to_query_string(value, "#{namespace}[]") }.join '&' + elsif obj.is_a?(Hash) + obj.collect { |key, value| to_query_string(value, namespace ? "#{namespace}[#{key}]" : key) }.sort * '&' + elsif obj.is_a?(Object) + require 'cgi' unless defined?(CGI) && defined?(CGI::escape) + "#{CGI.escape(to_param(namespace))}=#{CGI.escape(to_param(obj).to_s)}" else - raise "params must be an Object, Hash, or Array" + raise "Argument must be an object, hash, or array; instead it was a #{obj.class}" end end - def to_query_chunk(value,key) - require 'cgi' unless defined?(CGI) && defined?(CGI::escape) - "#{CGI.escape(to_query_string(key))}=#{CGI.escape(to_query_string(value).to_s)}" + def to_param(obj, namespace=nil) + if obj.is_a?(Object) + obj.to_s + elsif obj.is_a?(Array) + collect { |e| e.to_param }.join '/' + elsif obj.is_a?(Hash) + to_query_string(obj, namespace) + elsif obj.is_a?(NilClass) + obj + elsif obj.is_a?(TrueClass) + obj + elsif obj.is_a?(FalseClass) + obj + end end def decode_json(str, symbolize_keys=true) MultiJson.load(str, :symbolize_keys => symbolize_keys) end