Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Merge ccc700b into 23e2885
Browse files Browse the repository at this point in the history
  • Loading branch information
bartes committed Jan 12, 2019
2 parents 23e2885 + ccc700b commit 30cdb83
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: ruby
rvm:
- 2.5.0
- 2.5.3
- 2.4.3
- 2.3.6
- ruby-head
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ GEM
remote: https://rubygems.org/
specs:
byebug (10.0.2)
castle-rb (3.5.1)
castle-rb (3.5.2)
coveralls_reborn (0.12.0)
json (~> 2.1)
simplecov (~> 0.16.1)
Expand Down
4 changes: 1 addition & 3 deletions lib/castle/middleware/body_modify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ class BodyModify
extend Forwardable
def_delegators :@middleware, :log, :configuration

CJS_PATH = 'https://d2t77mnxyo7adj.cloudfront.net/v1/c.js'.freeze

def initialize
@middleware = Middleware.instance
end
Expand Down Expand Up @@ -48,7 +46,7 @@ def close_old_response(response)
def js_content_to_add(req)
[
"\n",
script_tag('', type: 'text/javascript', src: "#{CJS_PATH}?#{configuration.app_id}"),
script_tag('', type: 'text/javascript', src: "#{configuration.cjs_path}?#{configuration.app_id}"),
script_tag(js_commands(req).join, js_options(req)),
"\n"
].join
Expand Down
10 changes: 8 additions & 2 deletions lib/castle/middleware/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Configuration
def_delegators :@options,
:logger, :transport, :api_secret, :app_id,
:tracker_url, :autoforward_client_id, :cookie_domain,
:services,
:services, :api_options, :cjs_path,
:events, :identify, :user_traits, :security_headers
def_delegators :@middleware, :log, :track

Expand All @@ -28,14 +28,20 @@ def reload
track(context, options)
end
# Forward setting to Castle SDK
Castle.api_secret = api_secret
Castle.configure do |config|
api_options.each do |key, value|
config.public_send("#{key}=", value)
end
config.api_secret = api_secret
end
load_config_file if options.file_path
end

def load_config_file
file_config = YAML.load_file(options.file_path)
options.events = (options.events || {}).merge(file_config['events'] || {})
options.identify = (options.identify || {}).merge(file_config['identify'] || {})
options.api_options = (options.api_options || {}).merge(file_config['api_options'] || {})
options.user_traits = (options.user_traits || {}).merge(file_config['user_traits'] || {})
rescue Errno::ENOENT => e
log(:error, '[Castle] No config file found')
Expand Down
6 changes: 6 additions & 0 deletions lib/castle/middleware/configuration/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ class Middleware
# Configuration options accessible for configure in mounted app
class Configuration
class Options
CJS_PATH = 'https://d2t77mnxyo7adj.cloudfront.net/v1/c.js'.freeze

%i[
api_secret
api_options
cjs_path
app_id
tracker_url
autoforward_client_id
Expand All @@ -26,6 +30,8 @@ class Options
def initialize
@events = {}
@security_headers = false
@cjs_path = CJS_PATH
@api_options = {}
@identify = {}
@user_traits = {}
@services = ::Castle::Middleware::Configuration::Services.new
Expand Down
1 change: 1 addition & 0 deletions lib/castle/middleware/configuration/validate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def call(options)
validate_app_id(options)
error(:events) unless options.events.is_a?(Hash)
error(:identify) unless options.identify.is_a?(Hash)
error(:api_options) unless options.api_options.is_a?(Hash)
error(:user_traits) unless options.user_traits.is_a?(Hash)
error(:'identify/id') if options.identify['id'].nil?
error(:'user_traits/registered_at') if options.user_traits['registered_at'].nil?
Expand Down
2 changes: 1 addition & 1 deletion spec/castle/middleware/sensor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def xhr?

matcher :inject_the_script do
match_unless_raises do |subscriber|
expect(subscriber[2]).to include ::Castle::Middleware::BodyModify::CJS_PATH
expect(subscriber[2]).to include ::Castle::Middleware::Configuration::Options::CJS_PATH
end
end

Expand Down
2 changes: 2 additions & 0 deletions spec/castle/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
subject(:config) { described_class.instance.configuration }

it { expect(config.api_secret).to be_eql('secret') }

it { expect(Castle.config.port).to be_eql(3000) }
end

describe '::configure' do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
config.before do
::Castle::Middleware.configure do |c|
c.api_secret = 'secret'
c.api_options = { port: 3000 }
c.app_id = '1234'
c.file_path = './spec/castle/middleware/castle_config.yml'
end
Expand Down

0 comments on commit 30cdb83

Please sign in to comment.