zenbox 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,60 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rubygems/package_task'
4
+ require 'bundler'
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ require './lib/contextify/version'
8
+
9
+ namespace :changeling do
10
+ desc "Bumps the version by a minor or patch version, depending on what was passed in."
11
+ task :bump, :part do |t, args|
12
+ # Thanks, Jeweler!
13
+ if Contextify::VERSION =~ /^(\d+)\.(\d+)\.(\d+)(?:\.(.*?))?$/
14
+ major = $1.to_i
15
+ minor = $2.to_i
16
+ patch = $3.to_i
17
+ build = $4
18
+ else
19
+ abort
20
+ end
21
+
22
+ case args[:part]
23
+ when /minor/
24
+ minor += 1
25
+ patch = 0
26
+ when /patch/
27
+ patch += 1
28
+ else
29
+ abort
30
+ end
31
+
32
+ version = [major, minor, patch, build].compact.join('.')
33
+
34
+ File.open(File.join("lib", "contextify", "version.rb"), "w") do |f|
35
+ f.write <<EOF
36
+ module Contextify
37
+ VERSION = "#{version}".freeze
38
+ end
39
+ EOF
40
+ end
41
+ end
42
+
43
+ desc "Bump by a minor version (1.2.3 => 1.3.0)"
44
+ task :minor do |t|
45
+ Rake::Task['changeling:bump'].invoke(t.name)
46
+ end
47
+
48
+ desc "Bump by a patch version, (1.2.3 => 1.2.4)"
49
+ task :patch do |t|
50
+ Rake::Task['changeling:bump'].invoke(t.name)
51
+ end
52
+ end
53
+
54
+ begin
55
+ require 'yard'
56
+ YARD::Rake::YardocTask.new do |t|
57
+ t.files = ['lib/**/*.rb', 'TESTING.rdoc']
58
+ end
59
+ rescue LoadError
60
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "contextify/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = %q{zenbox}
7
+ s.version = Contextify::VERSION.dup
8
+ s.summary = %q{Send your user information to our hosted service and contextify your inbox.}
9
+
10
+ s.require_paths = ["lib"]
11
+ s.files = Dir["{generators/**/*,lib/**/*,rails/**/*,resources/*,script/*}"] +
12
+ %w(contextify.gemspec Gemfile INSTALL MIT-LICENSE Rakefile README.md install.rb)
13
+ s.test_files = Dir.glob("{test,spec,features}/**/*")
14
+
15
+ s.add_runtime_dependency("json")
16
+
17
+ s.authors = ["Contextify"]
18
+ s.email = %q{support@cloudfuji.com}
19
+ s.homepage = "http://cloudfuji.com"
20
+
21
+ s.platform = Gem::Platform::RUBY
22
+ end
@@ -0,0 +1,85 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/lib/insert_commands.rb")
2
+ require File.expand_path(File.dirname(__FILE__) + "/lib/rake_commands.rb")
3
+
4
+ class ContextifyGenerator < Rails::Generator::Base
5
+ def add_options!(opt)
6
+ opt.on('-k', '--api-key=token', String, "Your Contextify API key") { |v| options[:api_key] = v}
7
+ opt.on('-h', '--heroku', "Use the Heroku addon to provide your Contextify API key") { |v| options[:heroku] = v}
8
+ opt.on('-a', '--app=myapp', String, "Your Heroku app name (only required if deploying to >1 Heroku app)") { |v| options[:app] = v}
9
+ end
10
+
11
+ def manifest
12
+ if !api_key_configured? && !options[:api_key] && !options[:heroku]
13
+ puts "Must pass --api-key or --heroku or create config/initializers/contextify.rb"
14
+ exit
15
+ end
16
+
17
+ record do |m|
18
+ m.directory 'lib/tasks'
19
+ m.file 'contextify_tasks.rake', 'lib/tasks/contextify_tasks.rake'
20
+
21
+ if api_key_expression
22
+ if use_initializer?
23
+ m.template 'initializer.rb', 'config/initializers/contextify.rb',
24
+ :assigns => {:api_key => api_key_expression}
25
+ else
26
+ m.template 'initializer.rb', 'config/contextify.rb',
27
+ :assigns => {:api_key => api_key_expression}
28
+ m.append_to 'config/environment.rb', "require 'config/contextify'"
29
+ end
30
+ end
31
+ determine_api_key if heroku?
32
+ m.rake "contextify:test --trace", :generate_only => true
33
+ end
34
+ end
35
+
36
+ def api_key_expression
37
+ s = if options[:api_key]
38
+ "'#{options[:api_key]}'"
39
+ elsif options[:heroku]
40
+ "ENV['CONTEXTIFY_API_KEY']"
41
+ end
42
+ end
43
+
44
+ def determine_api_key
45
+ puts "Attempting to determine your API key from Heroku..."
46
+ ENV['CONTEXTIFY_API_KEY'] = heroku_api_key
47
+ if ENV['CONTEXTIFY_API_KEY'].blank?
48
+ puts "... Failed."
49
+ puts "WARNING: We were unable to detect the Contextify API key from your Heroku environment."
50
+ puts "Your Heroku application environment may not be configured correctly."
51
+ exit 1
52
+ else
53
+ puts "... Done."
54
+ puts "Heroku's Contextify API key is '#{ENV['CONTEXTIFY_API_KEY']}'"
55
+ end
56
+ end
57
+
58
+ def heroku_var(var,app_name = nil)
59
+ app = app_name ? "--app #{app_name}" : ''
60
+ `heroku config #{app} | grep -E "#{var.upcase}" | awk '{ print $3; }'`.strip
61
+ end
62
+
63
+ def heroku_api_key
64
+ heroku_var("contextify_api_key",options[:app]).split.find {|x| x unless x.blank?}
65
+ end
66
+
67
+ def heroku?
68
+ options[:heroku] ||
69
+ system("grep CONTEXTIFY_API_KEY config/initializers/contextify.rb") ||
70
+ system("grep CONTEXTIFY_API_KEY config/environment.rb")
71
+ end
72
+
73
+ def use_initializer?
74
+ Rails::VERSION::MAJOR > 1
75
+ end
76
+
77
+ def api_key_configured?
78
+ File.exists?('config/initializers/contextify.rb') ||
79
+ system("grep Contextify config/environment.rb")
80
+ end
81
+
82
+ def plugin_is_present?
83
+ File.exists?('vendor/plugins/contextify')
84
+ end
85
+ end
@@ -0,0 +1,34 @@
1
+ # Mostly pinched from http://github.com/ryanb/nifty-generators/tree/master
2
+
3
+ Rails::Generator::Commands::Base.class_eval do
4
+ def file_contains?(relative_destination, line)
5
+ File.read(destination_path(relative_destination)).include?(line)
6
+ end
7
+ end
8
+
9
+ Rails::Generator::Commands::Create.class_eval do
10
+ def append_to(file, line)
11
+ logger.insert "#{line} appended to #{file}"
12
+ unless options[:pretend] || file_contains?(file, line)
13
+ File.open(file, "a") do |file|
14
+ file.puts
15
+ file.puts line
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ Rails::Generator::Commands::Destroy.class_eval do
22
+ def append_to(file, line)
23
+ logger.remove "#{line} removed from #{file}"
24
+ unless options[:pretend]
25
+ gsub_file file, "\n#{line}", ''
26
+ end
27
+ end
28
+ end
29
+
30
+ Rails::Generator::Commands::List.class_eval do
31
+ def append_to(file, line)
32
+ logger.insert "#{line} appended to #{file}"
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ Rails::Generator::Commands::Create.class_eval do
2
+ def rake(cmd, opts = {})
3
+ logger.rake "rake #{cmd}"
4
+ unless system("rake #{cmd}")
5
+ logger.rake "#{cmd} failed. Rolling back"
6
+ command(:destroy).invoke!
7
+ end
8
+ end
9
+ end
10
+
11
+ Rails::Generator::Commands::Destroy.class_eval do
12
+ def rake(cmd, opts = {})
13
+ unless opts[:generate_only]
14
+ logger.rake "rake #{cmd}"
15
+ system "rake #{cmd}"
16
+ end
17
+ end
18
+ end
19
+
20
+ Rails::Generator::Commands::List.class_eval do
21
+ def rake(cmd, opts = {})
22
+ logger.rake "rake #{cmd}"
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ # Don't load anything when running the gems:* tasks.
2
+ # Otherwise, contextify will be considered a framework gem.
3
+ # https://thoughtbot.lighthouseapp.com/projects/14221/tickets/629
4
+ unless ARGV.any? {|a| a =~ /^gems/}
5
+
6
+ Dir[File.join(Rails.root, 'vendor', 'gems', 'contextify-*')].each do |vendored_notifier|
7
+ $: << File.join(vendored_notifier, 'lib')
8
+ end
9
+
10
+ begin
11
+ require 'contextify/tasks'
12
+ rescue LoadError => exception
13
+ namespace :contextify do
14
+ %w(deploy test log_stdout).each do |task_name|
15
+ desc "Missing dependency for contextify:#{task_name}"
16
+ task task_name do
17
+ $stderr.puts "Failed to run contextify:#{task_name} because of missing dependency."
18
+ $stderr.puts "You probably need to run `rake gems:install` to install the contextify gem"
19
+ abort exception.inspect
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,6 @@
1
+ <% if Rails::VERSION::MAJOR < 3 && Rails::VERSION::MINOR < 2 -%>
2
+ require 'contextify/rails'
3
+ <% end -%>
4
+ Contextify.configure do |config|
5
+ config.api_key = <%= api_key_expression %>
6
+ end
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ puts IO.read(File.join(File.dirname(__FILE__), 'INSTALL'))
data/lib/contextify.rb ADDED
@@ -0,0 +1,74 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'rubygems'
4
+ require 'contextify/version'
5
+ require 'contextify/configuration'
6
+ require 'contextify/sender'
7
+ require 'contextify/user_helper'
8
+
9
+ require 'contextify/railtie' if defined?(Rails::Railtie)
10
+
11
+ module Contextify
12
+ API_VERSION = "1.0"
13
+ LOG_PREFIX = "** [Contextify] "
14
+
15
+ class << self
16
+ # The sender object is responsible for delivering formatted data to the Contextify server.
17
+ # Must respond to #send_to_contextify. See Contextify::Sender.
18
+ attr_accessor :sender
19
+
20
+ # Names the Contextify model we should be watching. Set
21
+ # automatically when using contextify_user
22
+ attr_accessor :model
23
+
24
+ # A Contextify configuration object. Must act like a hash and return sensible
25
+ # values for all Contextify configuration options. See Contextify::Configuration.
26
+ attr_writer :configuration
27
+
28
+ # Prints out the response body from Contextify for debugging help
29
+ def report_response_body(response)
30
+ write_verbose_log("Response from Contextify: \n#{response}")
31
+ end
32
+
33
+ # Writes out the given message to the #logger
34
+ def write_verbose_log(message)
35
+ logger.info LOG_PREFIX + message if logger
36
+ end
37
+
38
+ # Look for the Rails logger currently defined
39
+ def logger
40
+ self.configuration.logger
41
+ end
42
+
43
+ # Call this method to modify defaults in your initializers.
44
+ #
45
+ # @example
46
+ # Contextify.configure do |config|
47
+ # config.api_key = '1234567890abcdef'
48
+ # config.secure = false
49
+ # end
50
+ def configure(silent = false)
51
+ yield(configuration)
52
+ self.sender = Sender.new(configuration)
53
+ end
54
+
55
+ # The configuration object.
56
+ # @see Contextify.configure
57
+ def configuration
58
+ @configuration ||= Configuration.new
59
+ end
60
+
61
+ # Sends data to contextify
62
+ #
63
+ # @param [String] email The email address you want to send data about.
64
+ # @param [Hash] opts Data that will be sent to Contextify.
65
+ def post(email, data)
66
+ post_data = {
67
+ :api_key => configuration.api_key,
68
+ :email => email,
69
+ :data => data.to_json
70
+ }
71
+ sender.send_to_contextify(post_data)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,115 @@
1
+ module Contextify
2
+ # Used to set up and modify settings for the notifier.
3
+ class Configuration
4
+
5
+ OPTIONS = [:api_key, :host, :http_open_timeout, :http_read_timeout,
6
+ :port, :proxy_host, :proxy_pass, :proxy_port, :proxy_user, :secure,
7
+ :use_system_ssl_cert_chain].freeze
8
+
9
+
10
+ # The API key for your Contextify account.
11
+ attr_accessor :api_key
12
+
13
+ # The host to connect to (defaults to contextify.me).
14
+ attr_accessor :host
15
+
16
+ # The port on which your Contextify server runs (defaults to 443 for secure
17
+ # connections, 80 for insecure connections).
18
+ attr_accessor :port
19
+
20
+ # +true+ for https connections, +false+ for http connections.
21
+ attr_accessor :secure
22
+
23
+ # +true+ to use whatever CAs OpenSSL has installed on your system. +false+ to use the ca-bundle.crt file included in Contextify itself (recommended and default)
24
+ attr_accessor :use_system_ssl_cert_chain
25
+
26
+ # The HTTP open timeout in seconds (defaults to 2).
27
+ attr_accessor :http_open_timeout
28
+
29
+ # The HTTP read timeout in seconds (defaults to 5).
30
+ attr_accessor :http_read_timeout
31
+
32
+ # The hostname of your proxy server (if using a proxy)
33
+ attr_accessor :proxy_host
34
+
35
+ # The port of your proxy server (if using a proxy)
36
+ attr_accessor :proxy_port
37
+
38
+ # The username to use when logging into your proxy server (if using a proxy)
39
+ attr_accessor :proxy_user
40
+
41
+ # The password to use when logging into your proxy server (if using a proxy)
42
+ attr_accessor :proxy_pass
43
+
44
+ # The logger used by Contextify
45
+ attr_accessor :logger
46
+
47
+ alias_method :secure?, :secure
48
+ alias_method :use_system_ssl_cert_chain?, :use_system_ssl_cert_chain
49
+
50
+ def initialize
51
+ @secure = false
52
+ @use_system_ssl_cert_chain= false
53
+ @host = 'contextify.me'
54
+ @http_open_timeout = 2
55
+ @http_read_timeout = 5
56
+ end
57
+
58
+
59
+ # Allows config options to be read like a hash
60
+ #
61
+ # @param [Symbol] option Key for a given attribute
62
+ def [](option)
63
+ send(option)
64
+ end
65
+
66
+ # Returns a hash of all configurable options
67
+ def to_hash
68
+ OPTIONS.inject({}) do |hash, option|
69
+ hash.merge(option.to_sym => send(option))
70
+ end
71
+ end
72
+
73
+ # Returns a hash of all configurable options merged with +hash+
74
+ #
75
+ # @param [Hash] hash A set of configuration options that will take precedence over the defaults
76
+ def merge(hash)
77
+ to_hash.merge(hash)
78
+ end
79
+
80
+ def port
81
+ @port || default_port
82
+ end
83
+
84
+ def protocol
85
+ if secure?
86
+ 'https'
87
+ else
88
+ 'http'
89
+ end
90
+ end
91
+
92
+ def ca_bundle_path
93
+ if use_system_ssl_cert_chain? && File.exist?(OpenSSL::X509::DEFAULT_CERT_FILE)
94
+ OpenSSL::X509::DEFAULT_CERT_FILE
95
+ else
96
+ local_cert_path # ca-bundle.crt built from source, see resources/README.md
97
+ end
98
+ end
99
+
100
+ def local_cert_path
101
+ File.expand_path(File.join("..", "..", "..", "resources", "ca-bundle.crt"), __FILE__)
102
+ end
103
+
104
+ private
105
+ def default_port
106
+ if secure?
107
+ 443
108
+ else
109
+ 80
110
+ end
111
+ end
112
+
113
+ end
114
+
115
+ end