fanforce-base 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a5e8ce0ddfe5ae255cbaace4d27d1e4bb33a5b00
4
+ data.tar.gz: 76091e58b1942cab6591528f9a670fb6f02ae4e4
5
+ SHA512:
6
+ metadata.gz: df91f669fa4bdecfe4d99cdbb7f7c35851a2ab311b5cee0632b39282ca5b088dc89f13dc1d85591fb1c22fd1147044977ae6cc70e5b8fc37e4879e8bf229fe58
7
+ data.tar.gz: b5e4266cc7753a77780361916bb394c245ae229736044856afd53049a7bc2aefbcebe364258826a75fce23759867a6a8624ad1b21e3c466c91c41606198a45c6
@@ -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
@@ -0,0 +1 @@
1
+ lib/**/*.rb
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ group :development, :test do
5
+ gem 'minitest'
6
+ gem 'rack-test'
7
+ gem 'simplecov', :require => false
8
+ gem 'yard'
9
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Caleb Clark
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ fanforce-base
2
+ ========
3
+
4
+ The base gem used by all other Fanforce gems
@@ -0,0 +1,22 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'fileutils'
4
+ include FileUtils
5
+
6
+ task :run_test do
7
+ Rake::Task['test'].execute
8
+ puts '---------------------------------------------------------------------'
9
+ puts 'TESTS PASSED... READY TO BUILD...'
10
+ puts '---------------------------------------------------------------------'
11
+ end
12
+ task :build => :run_test
13
+ task :default => :build
14
+
15
+ ########################################################################
16
+
17
+ Rake::TestTask.new do |t|
18
+ t.libs.push 'lib'
19
+ t.libs.push 'test'
20
+ t.pattern = 'test/**/*_test.rb'
21
+ t.verbose = false
22
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fanforce/base/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'fanforce-base'
8
+ gem.version = Fanforce::Base::VERSION
9
+ gem.date = Time.now.utc.strftime('%Y-%m-%d')
10
+ gem.summary = %q{The base gem used by other fanforce gems}
11
+
12
+ gem.authors = ['Caleb Clark']
13
+ gem.email = ['cclark@fanforce.com']
14
+ gem.homepage = 'http://github.com/fanforce/gem-fanforce-base'
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+
20
+ gem.add_runtime_dependency 'rack'
21
+ gem.add_runtime_dependency 'addressable', '~> 2.3.6'
22
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'base/version'
2
+
3
+ class Fanforce
4
+ class Base
5
+ require_relative 'base/domains'
6
+ require_relative 'base/internal_utils'
7
+ require_relative 'base/utils'
8
+ end
9
+ include Base::Utils
10
+ include Base::Domains
11
+ end
@@ -0,0 +1,44 @@
1
+ module Fanforce::Base::DomainEnvironments
2
+
3
+ # Base domains for development environment.
4
+ # @return [hash]
5
+ def self.development
6
+ root_domain = (ENV['FANFORCE_GEM_TOP_LEVEL_DOMAIN'] || 'gg')
7
+ {
8
+ :base => ENV['FANFORCE_GEM_BASE_DOMAIN'] || "fanforce.#{root_domain}",
9
+ :default_short_domain => ENV['FANFORCE_GEM_DEFAULT_SHORT_DOMAIN'] || "fanforc.#{root_domain}",
10
+ :apps_base => ENV['FANFORCE_GEM_APPS_BASE_DOMAIN'] || "ffapp.#{root_domain}",
11
+ }
12
+ end
13
+
14
+ # Base domains for test environment.
15
+ # @return [hash]
16
+ def self.test
17
+ {
18
+ :base => 'fanforce.gg',
19
+ :default_short_domain => 'fanforc.gg',
20
+ :apps_base => 'ffapp.gg',
21
+ }
22
+ end
23
+
24
+ # Base domains for staging environment.
25
+ # @return [hash]
26
+ def self.staging
27
+ {
28
+ :base => 'fanforce-staging.com',
29
+ :default_short_domain => 'fanforc.us',
30
+ :apps_base => 'ffapp.us',
31
+ }
32
+ end
33
+
34
+ # Base domains for production environment.
35
+ # @return [hash]
36
+ def self.production
37
+ {
38
+ :base => 'fanforce.com',
39
+ :default_short_domain => 'fanforce.io',
40
+ :apps_base => 'ffapp.io',
41
+ }
42
+ end
43
+
44
+ end
@@ -0,0 +1,100 @@
1
+ require_relative 'domain_environments'
2
+
3
+ module Fanforce::Base::Domains
4
+ extend self
5
+ def self.included(base) base.extend(self) end
6
+
7
+ # Current rack environment (i.e., production)
8
+ # @return [string] defaults to development
9
+ def environment
10
+ ENV['RACK_ENV'] || 'development'
11
+ end
12
+
13
+ # Abbreviation of current rack environment (i.e., dev)
14
+ # @return [symbol]
15
+ def environ
16
+ case environment
17
+ when 'production' then :prd
18
+ when 'staging' then :stg
19
+ when 'test' then :tst
20
+ when 'development' then :dev
21
+ end
22
+ end
23
+
24
+ # Base domain (i.e., fanforce.com) for current environment
25
+ # @return [domain]
26
+ def base_domain
27
+ Fanforce::Base::DomainEnvironments.method(environment).call[:base]
28
+ end
29
+
30
+ # Default short domain (i.e., fanforce.io) for current environment
31
+ # @return [domain]
32
+ def default_short_domain
33
+ Fanforce::Base::DomainEnvironments.method(environment).call[:default_short_domain]
34
+ end
35
+
36
+ # Base domain used for apps (i.e., ffapp.com) for current environment
37
+ # @return [domain]
38
+ def apps_base_domain
39
+ Fanforce::Base::DomainEnvironments.method(environment).call[:apps_base]
40
+ end
41
+
42
+ # Domain for accessing supercore-api (i.e., api.fanforce.com) for current environment
43
+ # @return [domain]
44
+ def api_domain
45
+ 'api.' + base_domain
46
+ end
47
+
48
+ # Domain for accessing supercore-api with https (i.e., prd-supercore-api.herokuapp.com) for current environment
49
+ # @return [domain]
50
+ def api_domain_ssl
51
+ environ == 'development' ? api_domain : "#{environ.to_s}-supercore-api.herokuapp.com"
52
+ end
53
+
54
+ # Domain for accessing supercore-eye (i.e., i.fanforce.com) for current environment
55
+ # @return [domain]
56
+ def eye_domain
57
+ 'i.' + base_domain
58
+ end
59
+
60
+ # Domain for accessing supercore-controller (i.e., controller.fanforce.com) for current environment
61
+ # @return [domain]
62
+ def controller_domain
63
+ 'controller.' + base_domain
64
+ end
65
+
66
+ # Domain for accessing uranium (i.e., uranium.fanforce.com) for current environment
67
+ # @return [domain]
68
+ def uranium_domain
69
+ 'uranium.' + base_domain
70
+ end
71
+
72
+ # Domain for Fanforce's website (i.e., fanforce.com) for current environment
73
+ # @return [domain]
74
+ def website_domain
75
+ 'www.' + base_domain
76
+ end
77
+
78
+ # Domain for accessing developer's website (i.e., developers.fanforce.com) for current environment
79
+ # @return [domain]
80
+ def developers_domain
81
+ 'developers.' + base_domain
82
+ end
83
+
84
+ ###########
85
+ # The following should be moved to edition config
86
+ ###########
87
+
88
+ # Domain for accessing the CommandCenter Webapp (i.e., app.fanforce.com) for current environment
89
+ # @return [domain]
90
+ def app_domain
91
+ 'app.' + base_domain
92
+ end
93
+
94
+ # Domain for accessing the CommandCenter Signup (i.e., signup.fanforce.com) for current environment
95
+ # @return [domain]
96
+ def signup_domain
97
+ 'signup.' + base_domain
98
+ end
99
+
100
+ end
@@ -0,0 +1,36 @@
1
+ module Fanforce::Base::InternalUtils
2
+
3
+ def self.modify_keys(object, &modifier) #:nodoc:
4
+ case object
5
+ when Array
6
+ object.map do |value|
7
+ modify_keys(value, &modifier)
8
+ end
9
+ when Hash
10
+ object.inject({}) do |result, (key, value)|
11
+ new_key = modifier.call(key)
12
+ new_value = modify_keys(value, &modifier)
13
+ result.merge! new_key => new_value
14
+ end
15
+ else
16
+ object
17
+ end
18
+ end
19
+
20
+ def self.to_param(obj, namespace=nil)
21
+ if obj.is_a?(Array)
22
+ collect { |e| to_param(e) }.join '/'
23
+ elsif obj.is_a?(Hash)
24
+ to_query_string(obj, namespace)
25
+ elsif obj.is_a?(NilClass)
26
+ obj
27
+ elsif obj.is_a?(TrueClass)
28
+ obj
29
+ elsif obj.is_a?(FalseClass)
30
+ obj
31
+ elsif obj.is_a?(Object)
32
+ obj.to_s
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,111 @@
1
+ require 'uri'
2
+ require 'cgi'
3
+ require 'rack/utils'
4
+ require 'addressable/uri'
5
+
6
+ require_relative 'internal_utils'
7
+
8
+ module Fanforce::Base::Utils
9
+ extend self
10
+ def self.included(base) base.extend(self) end
11
+
12
+ # An object is blank if it's nil, false, or empty. For example, '', nil, [], and {} are all blank.
13
+ # @return [boolean]
14
+ def is_blank?(obj)
15
+ obj.respond_to?(:empty?) ? !!obj.empty? : !obj
16
+ end
17
+
18
+ # An object is present if it's not blank.
19
+ # @return [boolean]
20
+ def is_present?(obj)
21
+ !is_blank?(obj)
22
+ end
23
+
24
+ # Return a new hash with all keys converted to symbols, as long as they respond to to_sym.
25
+ def symbolize_keys(object) #:nodoc:
26
+ Fanforce::Base::InternalUtils.modify_keys(object) do |key|
27
+ key.is_a?(String) ? key.to_sym : key
28
+ end
29
+ end
30
+
31
+ # Convert a URL string to a hash of URI parts:
32
+ # - scheme
33
+ # - host
34
+ # - port
35
+ # - path
36
+ # - query_params
37
+ # - query_string
38
+ # - fragment
39
+ # - clean_url
40
+ # @param url [string]
41
+ # @return [hash]
42
+ def extract_uri_hash(url)
43
+ uri = extract_uri(url) || return
44
+ if is_present?(uri.query)
45
+ query_params = Rack::Utils.parse_nested_query(uri.query).inject({}) do |result, (k,v)|
46
+ result[k] = v if k !~ /^ff_.+/
47
+ result
48
+ end
49
+ query_string = to_query_string(Hash[query_params.sort])
50
+ end
51
+ fragment = uri.fragment.gsub(/^ff_[^=]+=[^&]+$/, '').gsub(/&ff_[^=]+=[^&]+/, '') if is_present?(uri.fragment)
52
+
53
+ uri_hash = {}
54
+ uri_hash[:scheme] = uri.scheme || 'http'
55
+ uri_hash[:host] = clean_uri_var(uri.host)
56
+ uri_hash[:port] = clean_uri_var(uri.port)
57
+ uri_hash[:path] = clean_uri_var(uri.path || '/')
58
+ uri_hash[:query_params] = query_params || {}
59
+ uri_hash[:query_string] = query_string || ''
60
+ uri_hash[:fragment] = fragment || ''
61
+ uri_hash[:clean_url] = "#{uri_hash[:scheme]}://#{uri_hash[:host]}#{":#{uri_hash[:port]}" if [80,443].include?(uri_hash[:port])}#{uri_hash[:path]}#{"?#{query_string}" if is_present?(uri_hash[:query_string])}"
62
+ return uri_hash
63
+ end
64
+
65
+ def clean_uri_var(v)
66
+ (v.is_a?(Hash) or v.is_a?(Array) or v.is_a?(Integer)) ? v : v.to_s
67
+ end
68
+
69
+ def extract_uri(url)
70
+ url = url.gsub(/^file:\/\/:/, 'file://LOCALFILE') if url =~ /^file:\/\//
71
+ Addressable::URI.parse(url)
72
+ rescue
73
+ return if !url.is_a?(String)
74
+ if url =~ /^(http(.+))http\2+$/
75
+ extract_uri($1)
76
+ elsif url =~ /^(.+)#.*\s+.+$/
77
+ extract_uri($1)
78
+ end
79
+ end
80
+
81
+ def to_query_string(obj, namespace=nil)
82
+ return '' if is_blank?(obj)
83
+ if obj.is_a?(Array)
84
+ obj.collect { |value| to_query_string(value, "#{namespace}[]") }.join '&'
85
+ elsif obj.is_a?(Hash)
86
+ obj.collect { |key, value| to_query_string(value, namespace ? "#{namespace}[#{key}]" : key) }.sort * '&'
87
+ elsif obj.is_a?(Object)
88
+ "#{CGI.escape(Fanforce::Base::InternalUtils.to_param(namespace))}=#{CGI.escape(Fanforce::Base::InternalUtils.to_param(obj).to_s)}"
89
+ else
90
+ raise "Argument must be an object, hash, or array; instead it was a #{obj.class}"
91
+ end
92
+ end
93
+
94
+ def curl_command(method, url, req_params)
95
+ case method.to_s.downcase.to_sym
96
+ when :get
97
+ "curl \"#{url}?#{to_query_string(req_params)}\""
98
+ when :post
99
+ "curl -X POST -d \"#{to_query_string(req_params)}\" #{url}"
100
+ when :put
101
+ "curl -X PUT -d \"#{to_query_string(req_params)}\" #{url.to_json}"
102
+ when :delete
103
+ "curl --request DELETE \"#{url}?#{to_query_string(req_params)}\""
104
+ when :options
105
+ "curl -X OPTIONS \"#{url}?#{to_query_string(req_params)}\""
106
+ else
107
+ raise 'Unknown request method'
108
+ end
109
+ end
110
+
111
+ end
@@ -0,0 +1,5 @@
1
+ class Fanforce
2
+ class Base
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ describe Fanforce::Base::Domains do
4
+
5
+ [:test,:development,:staging,:production].each do |rack_env| it "should return domains for #{rack_env}" do
6
+ ENV['RACK_ENV'] = rack_env.to_s
7
+ assert Fanforce.environ.is_a?(Symbol)
8
+
9
+ assert Fanforce.base_domain == Fanforce::Base::DomainEnvironments.method(rack_env.to_s).call[:base]
10
+ assert Fanforce.default_short_domain == Fanforce::Base::DomainEnvironments.method(rack_env.to_s).call[:default_short_domain]
11
+ assert Fanforce.apps_base_domain == Fanforce::Base::DomainEnvironments.method(rack_env.to_s).call[:apps_base]
12
+
13
+ assert Fanforce.api_domain
14
+ assert Fanforce.api_domain_ssl
15
+ assert Fanforce.eye_domain
16
+ assert Fanforce.controller_domain
17
+ assert Fanforce.uranium_domain
18
+ assert Fanforce.website_domain
19
+ assert Fanforce.developers_domain
20
+ assert Fanforce.app_domain
21
+ assert Fanforce.signup_domain
22
+ end end
23
+
24
+ end
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ describe Fanforce::Base::Utils do
4
+
5
+ it 'should return true if false, nil, or empty' do
6
+ assert Fanforce.is_blank?(nil) == true
7
+ assert Fanforce.is_blank?('') == true
8
+ assert Fanforce.is_blank?([]) == true
9
+ assert Fanforce.is_blank?({}) == true
10
+ assert Fanforce.is_blank?(false) == true
11
+
12
+ assert Fanforce.is_blank?(true) == false
13
+ assert Fanforce.is_blank?(0) == false
14
+ assert Fanforce.is_blank?(1) == false
15
+ assert Fanforce.is_blank?('SOMETHING') == false
16
+ end
17
+
18
+ it 'should return true if not blank' do
19
+ assert Fanforce.is_present?(nil) == false
20
+ assert Fanforce.is_present?('') == false
21
+ assert Fanforce.is_present?([]) == false
22
+ assert Fanforce.is_present?({}) == false
23
+ assert Fanforce.is_present?(false) == false
24
+
25
+ assert Fanforce.is_present?(true) == true
26
+ assert Fanforce.is_present?(0) == true
27
+ assert Fanforce.is_present?(1) == true
28
+ assert Fanforce.is_present?('SOMETHING') == true
29
+ end
30
+
31
+ it 'should symbolize all keys' do
32
+ Fanforce.symbolize_keys({'first' => 'test', 'second' => 2}).each do |k,v|
33
+ assert k.is_a?(Symbol)
34
+ end
35
+ Fanforce.symbolize_keys([{'first' => 'test', 'second' => 2}]).each do |hash|
36
+ hash.each do |k,v|
37
+ assert k.is_a?(Symbol)
38
+ end
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,11 @@
1
+ require 'simplecov'
2
+ SimpleCov.start { add_filter '/test/' }
3
+
4
+ require 'rubygems'
5
+ require 'minitest/autorun'
6
+ require 'minitest/spec'
7
+ require 'rack/test'
8
+
9
+ ENV['RACK_ENV'] = 'test'
10
+
11
+ require 'fanforce/base'
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fanforce-base
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Caleb Clark
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-24 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: addressable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.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: 2.3.6
41
+ description:
42
+ email:
43
+ - cclark@fanforce.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - .yardopts
50
+ - Gemfile
51
+ - LICENSE.md
52
+ - README.md
53
+ - Rakefile
54
+ - fanforce-base.gemspec
55
+ - lib/fanforce/base.rb
56
+ - lib/fanforce/base/domain_environments.rb
57
+ - lib/fanforce/base/domains.rb
58
+ - lib/fanforce/base/internal_utils.rb
59
+ - lib/fanforce/base/utils.rb
60
+ - lib/fanforce/base/version.rb
61
+ - test/base/domains_test.rb
62
+ - test/base/utils_test.rb
63
+ - test/test_helper.rb
64
+ homepage: http://github.com/fanforce/gem-fanforce-base
65
+ licenses: []
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.0.14
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: The base gem used by other fanforce gems
87
+ test_files:
88
+ - test/base/domains_test.rb
89
+ - test/base/utils_test.rb
90
+ - test/test_helper.rb
91
+ has_rdoc: