fanforce-utils 0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDc2ZTQyOTdlODczYTJkZjkyY2FkZWUyZWE4NjY5MmFmNjMxODIxMg==
5
+ data.tar.gz: !binary |-
6
+ YWZmNmRjMmY3NTNiODVmNDZhNGNkOTdjNjI4ZTdkMjc3MWI4NjkxNw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NjRlYzJkMTQzMWE3MzRmNzNhODM1ODVhNjI1YTk3ZTlkYTk5MTllYzAyZTIw
10
+ NGFjNDJiMmI0YmNlYWEyYmE0MThhYzVhOTE4Nzc5OGIwMzBmMmQ0MTBjYTYw
11
+ NjEzMDdkYzcwZjBiYmNiMzc5ZjJhYzU0YzFmYWEyMmYzZWU0Y2Y=
12
+ data.tar.gz: !binary |-
13
+ Mjc2YTk4ODdmYjFhMzA0MTUzYWM0MDViMTVjMmZkZjJhNWM5ODhlMTg5YTZj
14
+ MWFmMzU3MTM5OWJmM2RlZjFhYzE0MzE5NzRkNjUwYzM1Y2MwNWY5ZDM1N2My
15
+ MjhiNGNiNmNmMzg0OWZlMzM4MWI1ZTg5MjBjNzhkNTZmNzc4MDU=
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+ .DS_Store
20
+ .idea/
21
+ .idea/*
22
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ fanforce-utils
2
+ ==
3
+
4
+ Internal utils library used by the Fanforce codebase.
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'fileutils'
4
+ include FileUtils
5
+
6
+ # Default Rake task is compile
7
+ #task :default => :compile
8
+
9
+ ########################################################################
10
+
11
+ Rake::TestTask.new do |t|
12
+ t.libs.push "lib"
13
+ t.libs.push "test"
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = false
16
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fanforce/utils/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'fanforce-utils'
8
+ gem.version = Fanforce::Utils::VERSION
9
+ gem.date = '2012-10-19'
10
+ gem.summary = %q{Internal utils library used by the Fanforce codebase.}
11
+ gem.authors = ['Caleb Clark']
12
+ gem.email = ['cclark@fanforce.com']
13
+ gem.homepage = 'http://github.com/fanforce/fanforce-utils'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ['lib']
18
+
19
+ gem.add_runtime_dependency 'rack'
20
+ gem.add_runtime_dependency 'multi_json', '>= 1.3.6'
21
+ end
@@ -0,0 +1,14 @@
1
+ class Fanforce
2
+ def self.env(&block)
3
+ if !block.nil?
4
+ @env = block
5
+ elsif !@env.nil?
6
+ @env.call()
7
+ else
8
+ {}
9
+ end
10
+ end
11
+ require_relative 'utils/version'
12
+ require_relative 'utils/utils'
13
+ end
14
+
@@ -0,0 +1,127 @@
1
+ require 'uri'
2
+ require 'rack/utils'
3
+ require 'multi_json'
4
+
5
+ module Fanforce::Utils
6
+ extend Fanforce::Utils
7
+ def self.included(base) base.extend(self) end
8
+
9
+ def is_blank?(obj)
10
+ obj.respond_to?(:empty?) ? obj.empty? : !obj
11
+ end
12
+
13
+ def is_present?(obj)
14
+ !is_blank?(obj)
15
+ end
16
+
17
+ def symbolize_keys(object) #:nodoc:
18
+ Fanforce::InternalUtils.modify_keys(object) do |key|
19
+ key.is_a?(String) ? key.to_sym : key
20
+ end
21
+ end
22
+
23
+ def parse_url(raw_url)
24
+ return if is_blank?(raw_url)
25
+ url = URI::parse(raw_url)
26
+ query_params = Rack::Utils.parse_query(url.query).inject({}) do |result, (k,v)|
27
+ if k !~ /^ff_.+/
28
+ result[k] = v
29
+ end
30
+ result
31
+ end
32
+ query_string = to_query_string(Hash[query_params.sort]) if is_present?(query_params)
33
+ _external_id = url.host + url.path
34
+ 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)}"
35
+
36
+ { _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 }
37
+ end
38
+
39
+ def to_query_string(obj, namespace=nil)
40
+ return '' if is_blank?(obj)
41
+ if obj.is_a?(Array)
42
+ obj.collect { |value| to_query_string(value, "#{namespace}[]") }.join '&'
43
+ elsif obj.is_a?(Hash)
44
+ obj.collect { |key, value| to_query_string(value, namespace ? "#{namespace}[#{key}]" : key) }.sort * '&'
45
+ elsif obj.is_a?(Object)
46
+ require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
47
+ "#{CGI.escape(Fanforce::InternalUtils.to_param(namespace))}=#{CGI.escape(Fanforce::InternalUtils.to_param(obj).to_s)}"
48
+ else
49
+ raise "Argument must be an object, hash, or array; instead it was a #{obj.class}"
50
+ end
51
+ end
52
+
53
+ def curl_command(method, url, req_params)
54
+ case method.to_s.downcase.to_sym
55
+ when :get
56
+ "curl \"#{url}?#{to_query_string(req_params)}\""
57
+ when :post
58
+ "curl -X POST -d \"#{to_query_string(req_params)}\" #{url}"
59
+ when :put
60
+ "curl -X PUT -d \"#{to_query_string(req_params)}\" #{url.to_json}"
61
+ when :delete
62
+ "curl --request DELETE \"#{url}?#{to_query_string(req_params)}\""
63
+ else
64
+ raise 'Unknown request method'
65
+ end
66
+ end
67
+
68
+ def decode_json(str, symbolize_keys=true)
69
+ MultiJson.load(str, :symbolize_keys => symbolize_keys)
70
+ end
71
+
72
+ # Creates a string representation of a javascript object for $.tmpl
73
+ def compile_jquery_tmpls(options={}, &block)
74
+ begin require 'haml' rescue LoadError raise 'You must have the haml gem installed to use Fanforce.compile_jquery_templates.' end
75
+ context = Object.new
76
+ class << context
77
+ include Haml::Helpers
78
+ end
79
+ context.init_haml_helpers
80
+
81
+ format = options[:format] == 'html' ? :html : :json
82
+
83
+ return context.capture_haml(&block) if format == :html
84
+ single_line_html = context.capture_haml(&block).split(/\r?\n/).inject('') {|sl, l| sl += l.strip + ' ' }
85
+ matches = single_line_html.scan(/<script id=[\"'](.*?)[\"'](?:.*?)>(.*?)(?:<\/script>)/mi)
86
+
87
+ matches.inject({}) {|t,m| t[m[0]] = m[1]; t }.to_json
88
+ end
89
+
90
+ end
91
+
92
+ module Fanforce::InternalUtils
93
+
94
+ def self.modify_keys(object, &modifier) #:nodoc:
95
+ case object
96
+ when Array
97
+ object.map do |value|
98
+ modify_keys(value, &modifier)
99
+ end
100
+ when Hash
101
+ object.inject({}) do |result, (key, value)|
102
+ new_key = modifier.call(key)
103
+ new_value = modify_keys(value, &modifier)
104
+ result.merge! new_key => new_value
105
+ end
106
+ else
107
+ object
108
+ end
109
+ end
110
+
111
+ def self.to_param(obj, namespace=nil)
112
+ if obj.is_a?(Array)
113
+ collect { |e| to_param(e) }.join '/'
114
+ elsif obj.is_a?(Hash)
115
+ to_query_string(obj, namespace)
116
+ elsif obj.is_a?(NilClass)
117
+ obj
118
+ elsif obj.is_a?(TrueClass)
119
+ obj
120
+ elsif obj.is_a?(FalseClass)
121
+ obj
122
+ elsif obj.is_a?(Object)
123
+ obj.to_s
124
+ end
125
+ end
126
+
127
+ end
@@ -0,0 +1,5 @@
1
+ class Fanforce
2
+ module Utils
3
+ VERSION = '0.2'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fanforce-utils
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ platform: ruby
6
+ authors:
7
+ - Caleb Clark
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2012-10-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.6
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.6
41
+ description:
42
+ email:
43
+ - cclark@fanforce.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - fanforce-utils.gemspec
53
+ - lib/fanforce/utils.rb
54
+ - lib/fanforce/utils/utils.rb
55
+ - lib/fanforce/utils/version.rb
56
+ homepage: http://github.com/fanforce/fanforce-utils
57
+ licenses: []
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.0.3
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Internal utils library used by the Fanforce codebase.
79
+ test_files: []