Module: Fanforce::Utils
- Extended by:
- Utils
- Included in:
- Utils
- Defined in:
- lib/fanforce/utils/utils.rb,
lib/fanforce/utils/version.rb
Constant Summary collapse
- VERSION =
'0.1.4'
Class Method Summary collapse
Instance Method Summary collapse
-
#compile_jquery_tmpls(options = {}, &block) ⇒ Object
Creates a string representation of a javascript object for $.tmpl.
- #curl_command(method, url, req_params) ⇒ Object
- #decode_json(str, symbolize_keys = true) ⇒ Object
- #is_blank?(obj) ⇒ Boolean
- #is_present?(obj) ⇒ Boolean
- #parse_url(raw_url) ⇒ Object
-
#symbolize_keys(object) ⇒ Object
:nodoc:.
- #to_query_string(obj, namespace = nil) ⇒ Object
Class Method Details
.included(base) ⇒ Object
6 |
# File 'lib/fanforce/utils/utils.rb', line 6 def self.included(base) base.extend(self) end |
Instance Method Details
#compile_jquery_tmpls(options = {}, &block) ⇒ Object
Creates a string representation of a javascript object for $.tmpl
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/fanforce/utils/utils.rb', line 72 def compile_jquery_tmpls(={}, &block) begin require 'haml' rescue LoadError raise 'You must have the haml gem installed to use Fanforce.compile_jquery_templates.' end context = Object.new class << context include Haml::Helpers end context.init_haml_helpers format = [:format] == 'html' ? :html : :json 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(/<script id=[\"'](.*?)[\"'](?:.*?)>(.*?)(?:<\/script>)/mi) matches.inject({}) {|t,m| t[m[0]] = m[1]; t }.to_json end |
#curl_command(method, url, req_params) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/fanforce/utils/utils.rb', line 52 def curl_command(method, url, req_params) case method.to_s.downcase.to_sym when :get "curl \"#{url}?#{to_query_string(req_params)}\"" when :post "curl -X POST -d \"#{to_query_string(req_params)}\" #{url}" when :put "curl -X PUT -d \"#{to_query_string(req_params)}\" #{url.to_json}" when :delete "curl --request DELETE \"#{url}?#{to_query_string(req_params)}\"" else raise 'Unknown request method' end end |
#decode_json(str, symbolize_keys = true) ⇒ Object
67 68 69 |
# File 'lib/fanforce/utils/utils.rb', line 67 def decode_json(str, symbolize_keys=true) MultiJson.load(str, :symbolize_keys => symbolize_keys) end |
#is_blank?(obj) ⇒ Boolean
8 9 10 |
# File 'lib/fanforce/utils/utils.rb', line 8 def is_blank?(obj) obj.respond_to?(:empty?) ? obj.empty? : !obj end |
#is_present?(obj) ⇒ Boolean
12 13 14 |
# File 'lib/fanforce/utils/utils.rb', line 12 def is_present?(obj) !is_blank?(obj) end |
#parse_url(raw_url) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/fanforce/utils/utils.rb', line 22 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 |
#symbolize_keys(object) ⇒ Object
:nodoc:
16 17 18 19 20 |
# File 'lib/fanforce/utils/utils.rb', line 16 def symbolize_keys(object) #:nodoc: Fanforce::InternalUtils.modify_keys(object) do |key| key.is_a?(String) ? key.to_sym : key end end |
#to_query_string(obj, namespace = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/fanforce/utils/utils.rb', line 38 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(Fanforce::InternalUtils.to_param(namespace))}=#{CGI.escape(Fanforce::InternalUtils.to_param(obj).to_s)}" else raise "Argument must be an object, hash, or array; instead it was a #{obj.class}" end end |