bushido 0.0.35 → 0.0.36
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/.rspec +4 -0
- data/Gemfile +6 -13
- data/README.md +3 -0
- data/Rakefile +31 -7
- data/app/controllers/bushido/data_controller.rb +2 -1
- data/app/controllers/bushido/envs_controller.rb +7 -5
- data/app/controllers/bushido/mail_controller.rb +37 -0
- data/lib/bushido.rb +27 -5
- data/lib/bushido/action_mailer.rb +35 -0
- data/lib/bushido/app.rb +7 -0
- data/lib/bushido/bar.rb +19 -0
- data/lib/bushido/base.rb +23 -0
- data/lib/bushido/command.rb +17 -15
- data/lib/bushido/config.rb +12 -0
- data/lib/bushido/data.rb +41 -23
- data/lib/bushido/data_helper.rb +1 -0
- data/lib/bushido/engine.rb +21 -0
- data/lib/bushido/envs_helper.rb +1 -0
- data/lib/bushido/event.rb +25 -2
- data/lib/bushido/event_observer.rb +20 -0
- data/lib/bushido/mail_helper.rb +1 -0
- data/lib/bushido/mail_route.rb +186 -0
- data/lib/bushido/middleware.rb +9 -16
- data/lib/bushido/platform.rb +27 -3
- data/lib/bushido/smtp.rb +24 -0
- data/lib/bushido/user.rb +39 -9
- data/lib/bushido/user_helper.rb +7 -0
- data/lib/bushido/utils.rb +44 -0
- data/lib/bushido/version.rb +1 -1
- data/lib/generators/bushido/auth_migration_generator.rb +46 -0
- data/lib/generators/bushido/hooks_generator.rb +43 -0
- data/lib/generators/bushido/mail_routes_generator.rb +45 -0
- data/lib/generators/bushido/routes_generator.rb +22 -0
- data/lib/{bushido/hooks.rb → hooks.rb} +0 -0
- data/lib/rails/routes.rb +36 -8
- data/lib/tasks/bushido.rake +6 -0
- data/{test_app/spec → spec/app_spec}/controllers/envs_controller_spec.rb +0 -0
- data/spec/app_spec/controllers/mail_controller_spec.rb +28 -0
- data/{test_app/spec → spec/app_spec}/integration/app_claim_spec.rb +4 -6
- data/spec/gem_spec/base_spec.rb +24 -0
- data/spec/gem_spec/command_spec.rb +181 -0
- data/spec/gem_spec/config_spec.rb +9 -0
- data/spec/gem_spec/data_spec.rb +13 -0
- data/spec/gem_spec/hooks_spec.rb +5 -0
- data/spec/gem_spec/mail_route_spec.rb +168 -0
- data/spec/gem_spec/platform_spec.rb +9 -0
- data/spec/gem_spec/user_spec.rb +45 -0
- data/spec/spec_helper.rb +21 -0
- data/spec/test_apps/rails-2.3/.gitignore +13 -0
- data/spec/test_apps/rails-2.3/Gemfile +13 -0
- data/spec/test_apps/rails-2.3/README +243 -0
- data/spec/test_apps/rails-2.3/Rakefile +10 -0
- data/spec/test_apps/rails-2.3/app/controllers/application_controller.rb +10 -0
- data/spec/test_apps/rails-2.3/app/helpers/application_helper.rb +3 -0
- data/spec/test_apps/rails-2.3/config/boot.rb +114 -0
- data/spec/test_apps/rails-2.3/config/environment.rb +41 -0
- data/spec/test_apps/rails-2.3/config/environments/development.rb +17 -0
- data/spec/test_apps/rails-2.3/config/environments/production.rb +28 -0
- data/spec/test_apps/rails-2.3/config/environments/test.rb +30 -0
- data/spec/test_apps/rails-2.3/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/test_apps/rails-2.3/config/initializers/cookie_verification_secret.rb +7 -0
- data/spec/test_apps/rails-2.3/config/initializers/inflections.rb +10 -0
- data/{test_app → spec/test_apps/rails-2.3}/config/initializers/mime_types.rb +0 -0
- data/spec/test_apps/rails-2.3/config/initializers/new_rails_defaults.rb +21 -0
- data/spec/test_apps/rails-2.3/config/initializers/session_store.rb +15 -0
- data/spec/test_apps/rails-2.3/config/locales/en.yml +5 -0
- data/spec/test_apps/rails-2.3/config/routes.rb +43 -0
- data/spec/test_apps/rails-2.3/db/seeds.rb +7 -0
- data/{test_app → spec/test_apps/rails-2.3}/doc/README_FOR_APP +0 -0
- data/spec/test_apps/rails-2.3/lib/tasks/rspec.rake +144 -0
- data/spec/test_apps/rails-2.3/public/404.html +30 -0
- data/spec/test_apps/rails-2.3/public/422.html +30 -0
- data/spec/test_apps/rails-2.3/public/500.html +30 -0
- data/{test_app → spec/test_apps/rails-2.3}/public/favicon.ico +0 -0
- data/{test_app → spec/test_apps/rails-2.3}/public/images/rails.png +0 -0
- data/{test_app → spec/test_apps/rails-2.3}/public/javascripts/application.js +0 -0
- data/spec/test_apps/rails-2.3/public/javascripts/controls.js +963 -0
- data/spec/test_apps/rails-2.3/public/javascripts/dragdrop.js +973 -0
- data/spec/test_apps/rails-2.3/public/javascripts/effects.js +1128 -0
- data/spec/test_apps/rails-2.3/public/javascripts/prototype.js +4320 -0
- data/{test_app → spec/test_apps/rails-2.3}/public/robots.txt +0 -0
- data/spec/test_apps/rails-2.3/script/about +4 -0
- data/spec/test_apps/rails-2.3/script/autospec +6 -0
- data/spec/test_apps/rails-2.3/script/console +3 -0
- data/spec/test_apps/rails-2.3/script/dbconsole +3 -0
- data/spec/test_apps/rails-2.3/script/destroy +3 -0
- data/spec/test_apps/rails-2.3/script/generate +3 -0
- data/spec/test_apps/rails-2.3/script/performance/benchmarker +3 -0
- data/spec/test_apps/rails-2.3/script/performance/profiler +3 -0
- data/spec/test_apps/rails-2.3/script/plugin +3 -0
- data/spec/test_apps/rails-2.3/script/runner +3 -0
- data/spec/test_apps/rails-2.3/script/server +3 -0
- data/spec/test_apps/rails-2.3/script/spec +10 -0
- data/spec/test_apps/rails-2.3/spec/all_spec.rb +5 -0
- data/spec/test_apps/rails-2.3/spec/rcov.opts +2 -0
- data/spec/test_apps/rails-2.3/spec/spec.opts +4 -0
- data/spec/test_apps/rails-2.3/spec/spec_helper.rb +83 -0
- data/spec/test_apps/rails-2.3/test/performance/browsing_test.rb +9 -0
- data/spec/test_apps/rails-2.3/test/test_helper.rb +38 -0
- data/{test_app → spec/test_apps/rails-3.0}/.gitignore +0 -0
- data/spec/test_apps/rails-3.0/.rspec +2 -0
- data/spec/test_apps/rails-3.0/Gemfile +10 -0
- data/{test_app → spec/test_apps/rails-3.0}/README +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/Rakefile +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/app/controllers/application_controller.rb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/app/controllers/static_controller.rb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/app/helpers/application_helper.rb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/app/helpers/static_helper.rb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/app/views/layouts/application.html.erb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/app/views/static/home.html.erb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/config.ru +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/config/application.rb +2 -0
- data/{test_app → spec/test_apps/rails-3.0}/config/boot.rb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/config/environment.rb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/config/environments/development.rb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/config/environments/production.rb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/config/environments/test.rb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/config/initializers/backtrace_silencers.rb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/config/initializers/inflections.rb +0 -0
- data/spec/test_apps/rails-3.0/config/initializers/mime_types.rb +5 -0
- data/{test_app → spec/test_apps/rails-3.0}/config/initializers/secret_token.rb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/config/initializers/session_store.rb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/config/locales/en.yml +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/config/routes.rb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/db/schema.rb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/db/seeds.rb +0 -0
- data/spec/test_apps/rails-3.0/doc/README_FOR_APP +2 -0
- data/{test_app → spec/test_apps/rails-3.0}/lib/tasks/.gitkeep +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/public/404.html +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/public/422.html +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/public/500.html +0 -0
- data/{test_app/public/stylesheets/.gitkeep → spec/test_apps/rails-3.0/public/favicon.ico} +0 -0
- data/spec/test_apps/rails-3.0/public/images/rails.png +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/public/index.html +0 -0
- data/spec/test_apps/rails-3.0/public/javascripts/application.js +2 -0
- data/{test_app → spec/test_apps/rails-3.0}/public/javascripts/controls.js +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/public/javascripts/dragdrop.js +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/public/javascripts/effects.js +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/public/javascripts/prototype.js +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/public/javascripts/rails.js +0 -0
- data/spec/test_apps/rails-3.0/public/robots.txt +5 -0
- data/{test_app/vendor/plugins → spec/test_apps/rails-3.0/public/stylesheets}/.gitkeep +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/script/rails +0 -0
- data/spec/test_apps/rails-3.0/spec/all_spec.rb +5 -0
- data/{test_app → spec/test_apps/rails-3.0}/spec/spec_helper.rb +13 -1
- data/{test_app → spec/test_apps/rails-3.0}/test/performance/browsing_test.rb +0 -0
- data/{test_app → spec/test_apps/rails-3.0}/test/test_helper.rb +0 -0
- data/spec/test_apps/rails-3.0/vendor/plugins/.gitkeep +0 -0
- data/tasks/cover_me.rake +11 -0
- metadata +249 -117
- data/lib/engine.rb +0 -16
- data/test/executable_test.rb +0 -10
- data/test/routes_test.rb +0 -12
- data/test/test_helper.rb +0 -8
- data/test_app/.rspec +0 -1
- data/test_app/Gemfile +0 -7
- data/test_app/config/database.yml +0 -22
- data/test_app/spec/helpers/static_helper_spec.rb +0 -14
- data/test_app/spec/support/integration_example_group.rb +0 -35
@@ -0,0 +1 @@
|
|
1
|
+
# Automatically required by the data_controller
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'bushido'
|
2
|
+
|
3
|
+
if defined?(Rails) && Rails::VERSION::MAJOR == 3
|
4
|
+
require 'rails'
|
5
|
+
require 'rails/routes'
|
6
|
+
|
7
|
+
module Bushido
|
8
|
+
class Engine < Rails::Engine
|
9
|
+
|
10
|
+
initializer "bushido.add_middleware" do |app|
|
11
|
+
# Only include our middleware if its on our platform
|
12
|
+
unless ENV['BUSHIDO_APP'].nil?
|
13
|
+
app.middleware.use Bushido::Middleware
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
else
|
19
|
+
# Rails 2 handling
|
20
|
+
Rails.configuration.middleware.use 'Bushido::Middleware'
|
21
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
# Automatically required by the envs_controller
|
data/lib/bushido/event.rb
CHANGED
@@ -15,6 +15,10 @@ module Bushido
|
|
15
15
|
attr_reader :category, :name, :data
|
16
16
|
|
17
17
|
class << self
|
18
|
+
def events_url
|
19
|
+
"#{Bushido::Platform.host}/apps/#{Bushido::Platform.name}/events.json"
|
20
|
+
end
|
21
|
+
|
18
22
|
# Lists all events
|
19
23
|
def all
|
20
24
|
@@events.collect{ |e| Event.new(e) }
|
@@ -29,12 +33,31 @@ module Bushido
|
|
29
33
|
def last
|
30
34
|
Event.new(@@events.last)
|
31
35
|
end
|
36
|
+
|
37
|
+
# NOOP right now
|
38
|
+
def refresh
|
39
|
+
@@events = Bushido::Command.get_command(events_url)
|
40
|
+
end
|
41
|
+
|
42
|
+
def publish(options={})
|
43
|
+
# Enforce standard format on client side so that any errors
|
44
|
+
# can be more quickly caught for the developer
|
45
|
+
return StandardError("Bushido::Event format incorrect, please make sure you're using the correct structure for sending events") unless !options[:name].nil? && !options[:category].nil? && !options[:data].nil?
|
46
|
+
|
47
|
+
payload = {}
|
48
|
+
payload[:category] = options[:category]
|
49
|
+
payload[:name] = options[:name]
|
50
|
+
payload[:data] = options[:data]
|
51
|
+
|
52
|
+
Bushido::Command.post_command(events_url, payload)
|
53
|
+
end
|
32
54
|
end
|
33
55
|
|
34
56
|
def initialize(options={})
|
35
57
|
@category = options["category"]
|
36
|
-
@name
|
37
|
-
@data
|
58
|
+
@name = options["name"]
|
59
|
+
@data = options["data"]
|
38
60
|
end
|
61
|
+
|
39
62
|
end
|
40
63
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Bushido
|
2
|
+
class EventObserver
|
3
|
+
attr_accessor :params
|
4
|
+
|
5
|
+
def self.inherited(klass)
|
6
|
+
if Bushido::Platform.on_bushido?
|
7
|
+
Bushido::Data.add_observer(klass.new)
|
8
|
+
else
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@params ||= {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def params
|
17
|
+
@params
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
# Automatically required by the mail_controller
|
@@ -0,0 +1,186 @@
|
|
1
|
+
module Bushido
|
2
|
+
class Mailroute
|
3
|
+
def self.map(&block)
|
4
|
+
raise StandardError.new("Mailroute only supported in Ruby >= 1.9.1") if RUBY_VERSION < "1.9.1"
|
5
|
+
|
6
|
+
@@routes ||= self.new
|
7
|
+
yield @@routes
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.routes
|
11
|
+
@@routes
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.clear_routes!
|
15
|
+
@@routes = self.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.pretty_print_routes
|
19
|
+
@@routes.routes.each_pair do |route_name, definition|
|
20
|
+
puts "#{route_name} => "
|
21
|
+
definition[:rules].each do |rule|
|
22
|
+
pretty_print_rule(rule, "\t\t")
|
23
|
+
end
|
24
|
+
definition[:constraints].each do |constraint|
|
25
|
+
pretty_print_contraint(constraint, "\t\t")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.pretty_print_rule(rule, prefix="")
|
31
|
+
output = "#{prefix}#{rule.first} => #{string_to_regex(rule[1]).inspect}, required? #{rule.last == true}"
|
32
|
+
puts output
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.pretty_print_contraint(constraint, prefix="")
|
36
|
+
output = "#{prefix}Constraint: #{constraint.inspect}"
|
37
|
+
puts output
|
38
|
+
end
|
39
|
+
|
40
|
+
# Taken from somewhere on stackoverflow, props to the author!
|
41
|
+
def self.string_to_regex(string)
|
42
|
+
return nil unless string.strip.match(/\A\/(.*)\/(.*)\Z/mx)
|
43
|
+
regexp , flags = $1 , $2
|
44
|
+
return nil if !regexp || flags =~ /[^xim]/m
|
45
|
+
|
46
|
+
x = /x/.match(flags) && Regexp::EXTENDED
|
47
|
+
i = /i/.match(flags) && Regexp::IGNORECASE
|
48
|
+
m = /m/.match(flags) && Regexp::MULTILINE
|
49
|
+
|
50
|
+
Regexp.new regexp , [x,i,m].inject(0){|a,f| f ? a+f : a }
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.field_matcher(*field_names)
|
54
|
+
self.class_eval do
|
55
|
+
field_names.each do |field_name|
|
56
|
+
define_method field_name do |line, *args|
|
57
|
+
constraints = args.first || {}
|
58
|
+
required = args[1]
|
59
|
+
|
60
|
+
add_route_rule(field_name.to_s, build_matcher(line, constraints), required)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
attr_accessor :params, :constraints, :routes, :current_target, :reply
|
67
|
+
field_matcher :from, :sender, :subject, :body
|
68
|
+
|
69
|
+
# Regex shortcuts
|
70
|
+
def ampm
|
71
|
+
/\d+[apmAPM]{2}/
|
72
|
+
end
|
73
|
+
|
74
|
+
def word
|
75
|
+
/\w+/
|
76
|
+
end
|
77
|
+
|
78
|
+
def number
|
79
|
+
/\d+/
|
80
|
+
end
|
81
|
+
|
82
|
+
def words_and_spaces
|
83
|
+
/[\w|\s]+/
|
84
|
+
end
|
85
|
+
|
86
|
+
def month
|
87
|
+
/((?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Sept|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?))/
|
88
|
+
end
|
89
|
+
|
90
|
+
def year
|
91
|
+
/((?:(?:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3})))(?![\\d])/
|
92
|
+
end
|
93
|
+
|
94
|
+
def initialize
|
95
|
+
@routes ||= ActiveSupport::OrderedHash.new
|
96
|
+
end
|
97
|
+
|
98
|
+
def route(target, &block)
|
99
|
+
@routes[@current_target = target] = {:rules => [], :constraints => []}
|
100
|
+
yield self
|
101
|
+
end
|
102
|
+
|
103
|
+
# Maybe add_requirement is better?
|
104
|
+
# Constraint procs must return true if the mail should be allowed
|
105
|
+
def add_constraint(params_field_name, requirement_type)
|
106
|
+
checkers = {
|
107
|
+
:not_allowed => Proc.new { |params| params[params_field_name.to_s].nil? },
|
108
|
+
:required => Proc.new { |params| not params[params_field_name.to_s].nil? },
|
109
|
+
:custom => Proc.new { |params| requirement_type.call(params[params_field_name.to_s]) }
|
110
|
+
}
|
111
|
+
|
112
|
+
checker = requirement_type.is_a?(Proc) ? checkers[:custom] : checkers[requirement_type]
|
113
|
+
|
114
|
+
raise StandardError if checker.nil?
|
115
|
+
|
116
|
+
@routes[@current_target][:constraints] << checker
|
117
|
+
@routes[@current_target][:constraints].flatten! # In case we've added a nil
|
118
|
+
end
|
119
|
+
|
120
|
+
def process(mail)
|
121
|
+
@params = preprocess_mail(mail)
|
122
|
+
|
123
|
+
# Iterate through the routes and test each rule for each route against current mail
|
124
|
+
@routes.each_pair do |route_name, definition|
|
125
|
+
_matches = true
|
126
|
+
|
127
|
+
# route: {'name-of-route' => {:rules => [[:field, matcher]], :constraints => []}}
|
128
|
+
definition[:rules].each do |matcher|
|
129
|
+
result = self.class.string_to_regex(matcher[1]).match(mail[matcher.first])
|
130
|
+
if result.nil?
|
131
|
+
_matches = false
|
132
|
+
break
|
133
|
+
end
|
134
|
+
|
135
|
+
# Only Ruby 1.9 support named capture groups
|
136
|
+
result.names.each { |key| @params[key] = result[key] }
|
137
|
+
end
|
138
|
+
|
139
|
+
# Run param-based constraints
|
140
|
+
if _matches and constraints_pass?(definition[:constraints], params)
|
141
|
+
@params['mail'] = mail
|
142
|
+
puts "Firing: #{@params.inspect}, #{route_name}"
|
143
|
+
return Bushido::Data.fire(params, "mail_#{route_name.gsub('.', '_')}")
|
144
|
+
break
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
private
|
151
|
+
|
152
|
+
def preprocess_mail(mail)
|
153
|
+
params = {}
|
154
|
+
# Remove Re:'s from the subject and set reply => true if present
|
155
|
+
if mail["subject"].match(/^(Re: ){1,}/i)
|
156
|
+
params['reply'] = true
|
157
|
+
until !mail["subject"].match(/^(Re: ){1,}/i)
|
158
|
+
mail["subject"] = mail["subject"][4..-1]
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
if from_details = mail['from'].try(:match, /^([\w|\s]*) <(.*)>/)
|
163
|
+
params['from_name'] = from_details[1]
|
164
|
+
params['from_email'] = from_details[2]
|
165
|
+
end
|
166
|
+
|
167
|
+
return params
|
168
|
+
end
|
169
|
+
|
170
|
+
def add_route_rule(field, matcher, required=true)
|
171
|
+
route = @routes[@current_target]
|
172
|
+
route[route.keys.first] << [field, matcher, required]
|
173
|
+
end
|
174
|
+
|
175
|
+
def build_matcher(line, constraints)
|
176
|
+
temp = "/#{line}/"
|
177
|
+
constraints.keys.each { |key| temp.gsub!("{:#{key}}", "(?<#{key}>#{constraints[key].inspect[1..-2]})") }
|
178
|
+
temp
|
179
|
+
end
|
180
|
+
|
181
|
+
def constraints_pass?(constraints, params)
|
182
|
+
constraints.nil? or constraints.select{ |proc| proc.call(params) != true }.empty?
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
data/lib/bushido/middleware.rb
CHANGED
@@ -2,36 +2,28 @@
|
|
2
2
|
|
3
3
|
module Bushido
|
4
4
|
class Middleware
|
5
|
-
|
6
|
-
BUSHIDO_JS_URL = "#{Bushido::Platform.host}/api/bushido.js"
|
7
|
-
|
8
5
|
include Rack::Utils
|
9
6
|
|
10
7
|
def initialize(app, opts = {})
|
11
8
|
@app = app
|
12
|
-
@bushido_app_name = ENV['BUSHIDO_APP']
|
13
|
-
@bushido_metrics_token = ENV['BUSHIDO_METRICS_TOKEN']
|
14
|
-
@bushido_claimed = (ENV['BUSHIDO_CLAIMED'].nil? or ENV['BUSHIDO_CLAIMED'].blank?) ? false : true
|
15
9
|
end
|
16
10
|
|
17
11
|
def call(env)
|
18
|
-
|
12
|
+
if Bushido::Platform.on_bushido? and Bushido::Bar.in_bar_display_path?(env)
|
13
|
+
status, headers, response = @app.call(env)
|
19
14
|
|
20
|
-
status, headers, response = @app.call(env)
|
21
|
-
|
22
|
-
unless @bushido_app_name.empty?
|
23
15
|
content = ""
|
24
16
|
response.each { |part| content += part }
|
25
17
|
|
26
18
|
# "claiming" bar + stats ?
|
27
19
|
content.gsub!(/<\/body>/i, <<-STR
|
28
20
|
<script type="text/javascript">
|
29
|
-
var _bushido_app = '#{
|
30
|
-
var _bushido_claimed = #{
|
31
|
-
var _bushido_metrics_token = '#{
|
21
|
+
var _bushido_app = '#{Bushido::Platform.name}';
|
22
|
+
var _bushido_claimed = #{Bushido::Platform.claimed?.to_s};
|
23
|
+
var _bushido_metrics_token = '#{Bushido::Platform.metrics_token}';
|
32
24
|
(function() {
|
33
25
|
var bushido = document.createElement('script'); bushido.type = 'text/javascript'; bushido.async = true;
|
34
|
-
bushido.src = '#{
|
26
|
+
bushido.src = '#{Bushido::Platform.bushido_js_source}?t=#{::Bushido::VERSION.gsub('.', '')}';
|
35
27
|
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(bushido, s);
|
36
28
|
})();
|
37
29
|
</script>
|
@@ -40,10 +32,11 @@ module Bushido
|
|
40
32
|
)
|
41
33
|
|
42
34
|
headers['content-length'] = bytesize(content).to_s
|
35
|
+
[status, headers, [content]]
|
36
|
+
else
|
37
|
+
@app.call(env)
|
43
38
|
end
|
44
|
-
[status, headers, [content]]
|
45
39
|
end
|
46
|
-
|
47
40
|
end
|
48
41
|
end
|
49
42
|
|
data/lib/bushido/platform.rb
CHANGED
@@ -5,7 +5,6 @@ module Bushido
|
|
5
5
|
ENV['BUSHIDO_NAME']
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
8
|
def key
|
10
9
|
ENV['BUSHIDO_APP_KEY']
|
11
10
|
end
|
@@ -14,9 +13,34 @@ module Bushido
|
|
14
13
|
"#{host}/apps/#{name}/bus"
|
15
14
|
end
|
16
15
|
|
16
|
+
def protocol
|
17
|
+
ENV['BUSHIDO_PROTOCOL'] || "https"
|
18
|
+
end
|
19
|
+
|
20
|
+
def port
|
21
|
+
ENV['BUSHIDO_PORT']
|
22
|
+
end
|
23
|
+
|
17
24
|
def host
|
18
|
-
|
19
|
-
|
25
|
+
bushido_port = port ? ":#{port}" : ""
|
26
|
+
bushido_host = ENV['BUSHIDO_HOST'] || 'bushi.do'
|
27
|
+
"#{protocol}://#{bushido_host}#{bushido_port}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def on_bushido?
|
31
|
+
ENV['HOSTING_PLATFORM']=="bushido"
|
32
|
+
end
|
33
|
+
|
34
|
+
def claimed?
|
35
|
+
(ENV['BUSHIDO_CLAIMED'].nil? or ENV['BUSHIDO_CLAIMED'].blank?) ? false : true
|
36
|
+
end
|
37
|
+
|
38
|
+
def metrics_token
|
39
|
+
ENV['BUSHIDO_METRICS_TOKEN']
|
40
|
+
end
|
41
|
+
|
42
|
+
def bushido_js_source
|
43
|
+
"#{Bushido::Platform.host}/api/bushido.js"
|
20
44
|
end
|
21
45
|
end
|
22
46
|
end
|
data/lib/bushido/smtp.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module Bushido
|
2
|
+
class SMTP
|
3
|
+
class << self
|
4
|
+
[:tls, :server, :port, :domain, :authentication, :user, :password].each do |method_name|
|
5
|
+
define_method "#{method_name}".to_sym do
|
6
|
+
ENV["SMTP_#{method_name.to_s.upcase}"]
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def setup_action_mailer_smtp!
|
11
|
+
ActionMailer::Base.smtp_settings = {
|
12
|
+
:enable_starttls_auto => Bushido::SMTP.tls,
|
13
|
+
:tls => Bushido::SMTP.tls,
|
14
|
+
:address => Bushido::SMTP.server,
|
15
|
+
:port => Bushido::SMTP.port,
|
16
|
+
:domain => Bushido::SMTP.domain,
|
17
|
+
:authentication => Bushido::SMTP.authentication,
|
18
|
+
:user_name => Bushido::SMTP.user,
|
19
|
+
:password => Bushido::SMTP.password,
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/bushido/user.rb
CHANGED
@@ -1,28 +1,58 @@
|
|
1
1
|
module Bushido
|
2
2
|
# Bushido User enables user validation against Bushido's server
|
3
|
-
class User
|
3
|
+
class User < Bushido::Base
|
4
4
|
class << self
|
5
|
-
|
6
|
-
"#{Bushido::Platform.host}/unity/v1"
|
7
|
-
end
|
8
|
-
|
9
|
-
|
5
|
+
|
10
6
|
# Checks whether user an email and password correspond to a valid bushido
|
11
7
|
# user. Returns nil if false, or the Bushido user's ID if true.
|
12
8
|
def valid?(email, pass)
|
13
9
|
params = {}
|
14
10
|
params[:email] = email
|
15
11
|
params[:pass] = pass
|
16
|
-
Bushido::Command.post_command(
|
12
|
+
Bushido::Command.post_command(valid_unity_url, params)
|
17
13
|
end
|
18
|
-
|
19
14
|
|
20
15
|
# Checks whether email corresponds to a valid Bushido user.
|
21
16
|
# Returns true or false
|
22
17
|
def exists?(email)
|
23
18
|
params = {}
|
24
19
|
params[:email] = email
|
25
|
-
Bushido::Command.post_command(
|
20
|
+
Bushido::Command.post_command(exists_unity_url, params)
|
21
|
+
end
|
22
|
+
|
23
|
+
# send a Bushido invite with a short description of the app (also a box of chocolates, if he's a Kryptonian)
|
24
|
+
# Bushido::User.invite("clark@kent-on-krypton.com")
|
25
|
+
def invite(email)
|
26
|
+
params = {}
|
27
|
+
params[:email] = email
|
28
|
+
Bushido::Command.post_command(invite_unity_url, params)
|
29
|
+
end
|
30
|
+
|
31
|
+
# List all pending invites
|
32
|
+
# Bushido::User.pending_invites
|
33
|
+
def pending_invites
|
34
|
+
params = {}
|
35
|
+
Bushido::Command.get_command(pending_invites_unity_url, params)
|
36
|
+
end
|
37
|
+
|
38
|
+
# To remove a user from an application
|
39
|
+
# Bushido::User.remove("5z325f4knbm2f")
|
40
|
+
def remove(ido_id)
|
41
|
+
params = {}
|
42
|
+
params[:ido_id] = ido_id
|
43
|
+
Bushido::Command.post_command(remove_unity_url, params)
|
44
|
+
end
|
45
|
+
|
46
|
+
# To send a notification to a user who belongs to your app
|
47
|
+
# Bushido::User.notify('5z325f4knbm2f', 'Example title', 'Example message', 'chat')
|
48
|
+
def notify(ido_id, title, body, category="general")
|
49
|
+
params = {}
|
50
|
+
params[:ido_id] = ido_id
|
51
|
+
params[:title] = title
|
52
|
+
params[:body] = body
|
53
|
+
params[:category] = category
|
54
|
+
|
55
|
+
Bushido::Command.post_command(notify_user_url, params)
|
26
56
|
end
|
27
57
|
end
|
28
58
|
end
|