require 'uri' require 'rack/utils' require 'forwardable' require 'multi_json' module Fanforce::Utils extend Fanforce::Utils def self.included(base) base.extend(self) end def is_blank?(obj) obj.respond_to?(:empty?) ? obj.empty? : !obj end def is_present?(obj) !is_blank?(obj) end def valid_fanforce_js_request?(params=@params) return false if !params.is_a?(Hash) return false if is_blank?(params[:fanforce_id]) or is_blank?(params[:api_key]) return true end def valid_fanforce_request?(params=@params) return false if !params.is_a?(Hash) return false if is_blank?(params[:fanforce_id]) return false if is_blank?(params[:app_id]) and is_blank?(params[:behavior_plugin_id]) and is_blank?(params[:plugin_id]) and is_blank?(params[:widget_id]) return true end def valid_install_request?(params=@params) return false if !params.is_a?(Hash) return false if is_blank?(params[:fanforce_id]) return false if is_blank?(params[:app_id]) and is_blank?(params[:behavior_plugin_id]) and is_blank?(params[:plugin_id]) and is_blank?(params[:widget_id]) return false if is_blank?(params[:api_key]) return true end def valid_uninstall_request?(params=@params) return false if !params.is_a?(Hash) return false if is_blank?(params[:fanforce_id]) return false if is_blank?(params[:app_id]) and is_blank?(params[:behavior_plugin_id]) and is_blank?(params[:plugin_id]) and is_blank?(params[:widget_id]) return false if is_blank?(params[:api_key]) return true end def remove_nil_values(hash) hash.clone.delete_if {|k,v| v.nil? } end def collect_known_params(params) params = params.symbolize_keys if is_present?(params[:app_id]) addon_type = :app addon_id = params[:app_id] elsif is_present?(params[:behavior_plugin_id]) addon_type = :plugin addon_id = params[:behavior_plugin_id] elsif is_present?(params[:plugin_id]) addon_type = :plugin addon_id = params[:plugin_id] elsif is_present?(params[:widget_id]) addon_type = :widget addon_id = params[:widget_id] end remove_nil_values(:"#{addon_type}_id" => params[:"#{addon_type}_id"], addon_type: addon_type, addon_id: addon_id, fanforce_id: params[:fanforce_id], fanforce_slug: params[:fanforce_slug], api_key: params[:api_key], session_id: params[:session_id]) end def remove_internal_params(params) params.clone.delete_if { |k,v| [:app_id, :behavior_plugin_id, :plugin_id, :widget_id, :addon_type, :addon_id, :fanforce_id, :fanforce_slug, :api_key].include? k } end def remove_sensitive_params(params) params.clone.delete_if { |k,v| [:api_key].include? k } end def parse_url(raw_url) return if is_blank?(raw_url) url = URI::parse(raw_url) query_params = Rack::Utils.parse_query(url.query).inject({}) do |result, (k,v)| if k !~ /^ff_.+/ result[k] = v end result end 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 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(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 "Argument must be an object, hash, or array; instead it was a #{obj.class}" end end def to_param(obj, namespace=nil) if 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 elsif obj.is_a?(Object) obj.to_s end end def log(str) puts "Fanforce: #{str}" end def decode_json(str, symbolize_keys=true) log str MultiJson.load(str, :symbolize_keys => symbolize_keys) end # Creates a string representation of a javascript object for $.tmpl def compile_jquery_tmpls(options={}, &block) begin require 'haml' rescue LoadError raise "You must have the haml gem installed for Fanforce.compile_jquery_templates to work." end context = Object.new class << context include Haml::Helpers end context.init_haml_helpers format = if options[:format] == 'html' then :html else options[:callback].present? ? :jsonp : :json end return context.capture_haml(&block) if format == :html single_line_html = context.capture_haml(&block).split(/\r?\n/).inject('') {|sl, l| sl += l.strip + ' ' } matches = single_line_html.scan(/