excursion 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91ab81cf91d566addc17aa5963965138a1c6592a
4
- data.tar.gz: 32aae702c9c69c58bbb4c420e606a50990e51d5f
3
+ metadata.gz: 70d3b67fdc9a21e72d9d1c5571f3a2c3010f45a2
4
+ data.tar.gz: ebdec9417ed30b61d4eaa1eb966ea406a0ea6821
5
5
  SHA512:
6
- metadata.gz: cb034cdfc374f4266221b176d3cf786981b7af4287446340530933ae097f9e8ba3f13df52b0bd7460a7bf3a1e5af72d241984a2df26c1674fdced47f6b98c376
7
- data.tar.gz: eb5cfb2d550eee50c5b26db1ef89a5f188846dd3bebe29d46c89c430c219f3b3d58c16f6ecc10a02c78ac2769e5b8031a6f447b0b33eecca125d14917d4e8996
6
+ metadata.gz: 057db352ba02d3f2c41507b88237cb6ab6afb7c7c1b52cc526e6c4d99a117fd3408a1490b8936281c69b0f2d4953bac810d4af339e60d0212dd81792b6d00451
7
+ data.tar.gz: 3d3b389f2d04646bc130f9c970a34764864979cf7e546dcf8cafade41dcea0c64e2041e26ab9d3a69c4b77754f7cf0b18715c1ecbc28348eb7e3d4ec51688570
@@ -4,7 +4,8 @@ module Excursion
4
4
  # TODO
5
5
  # exclude_pattern: to exclude certain routes from being shared
6
6
  # include_pattern: to only include certain routes
7
- default_url_options: {}
7
+ default_url_options: {}, # default_url_options used when building routes for this app
8
+ retry_limit: 3 # retry limit for datasources that user remote servers
8
9
  }
9
10
 
10
11
  #attr_reader *DEFAULT_CONFIGURATION_OPTIONS.keys
@@ -29,10 +29,13 @@ module Excursion
29
29
  protected
30
30
 
31
31
  def initialize(path=nil)
32
- path = Excursion.configuration.datasource_file
32
+ path ||= Excursion.configuration.datasource_file
33
+ raise DatasourceConfigurationError, "You must configure the :file datasource with a datasource_file path" if path.nil?
33
34
  @path = ::File.expand_path(path)
35
+ rescue DatasourceConfigurationError => e
36
+ raise e
34
37
  rescue
35
- raise "Could not initialize the File datasource. Make sure you have properly configured your datasource"
38
+ raise DatasourceConfigurationError, "Could not initialize the :file datasource. Make sure you have properly configured the datasource_file path"
36
39
  end
37
40
 
38
41
  def exists?
@@ -46,7 +49,10 @@ module Excursion
46
49
  end
47
50
 
48
51
  def write_file(results)
52
+ FileUtils.mkpath(::File.dirname(@path))
49
53
  ::File.open(@path, 'w') { |f| f.write(results.to_yaml)}
54
+ rescue
55
+ raise DatasourceConfigurationError, "Could not write to the excursion route pool file: #{@path}"
50
56
  end
51
57
  end
52
58
  end
@@ -0,0 +1,56 @@
1
+ require 'dalli'
2
+ require 'excursion/datasources/datasource'
3
+ require 'excursion/exceptions/memcache'
4
+
5
+ module Excursion
6
+ module Datasources
7
+ class Memcache < Datasource
8
+
9
+ def read(key)
10
+ @client.get(key.to_s)
11
+ rescue Dalli::RingError => e
12
+ rescue_from_dalli_ring_error(e) && retry
13
+ end
14
+ alias_method :get, :read
15
+
16
+ def write(key, value)
17
+ @client.set(key.to_s, value)
18
+ rescue Dalli::RingError => e
19
+ rescue_from_dalli_ring_error(e) && retry
20
+ end
21
+ alias_method :set, :write
22
+
23
+ def delete(key)
24
+ @client.delete(key)
25
+ rescue Dalli::RingError => e
26
+ rescue_from_dalli_ring_error(e) && retry
27
+ end
28
+ alias_method :unset, :delete
29
+
30
+ protected
31
+
32
+ def initialize(server=nil, options={})
33
+ server ||= Excursion.configuration.memcache_server
34
+ raise MemcacheConfigurationError, "You must configure the :memcache datasource with a memcache_server" if server.nil?
35
+ @client = Dalli::Client.new(server, options)
36
+ end
37
+
38
+ # TODO if we're using memcache, and the server goes away, it might be a good idea
39
+ # to make sure to re-register this app in the pool when it comes back, just in case
40
+ # the server crashed and the pool is lost.
41
+ def rescue_from_dalli_ring_error(e)
42
+ @dalli_retries ||= 0
43
+
44
+ if @dalli_retries >= Excursion.configuration.retry_limit
45
+ retries = @dalli_retries
46
+ @dalli_retries = 0
47
+ raise MemcacheServerError, "Excursion memcache server is down! Retried #{retries} times."
48
+ end
49
+
50
+ STDERR.puts "Excursion memcache server has gone away! Retrying..."
51
+ sleep 1 # give it a chance to come back
52
+ @dalli_retries += 1
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,7 @@
1
+ module Excursion
2
+ class DatasourceError < Error; end
3
+ class NoDatasourceError < DatasourceError; end
4
+ class InvalidDatasourceError < DatasourceError; end
5
+
6
+ class DatasourceConfigurationError < DatasourceError; end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Excursion
2
+ class MemcacheError < Error; end
3
+ class MemcacheServerError < MemcacheError; end
4
+ class MemcacheConfigurationError < MemcacheError; end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Excursion
2
+ class NotInPool < Error; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module Excursion
2
+ class Error < StandardError; end
3
+ end
@@ -1,24 +1,35 @@
1
1
  module Excursion
2
2
  module Helpers
3
- class ApplicationHelper
4
-
5
- def routes
6
- @application.routes
3
+ module ApplicationHelper
4
+
5
+ # Returns an Excursion::Helpers::ApplicationHelper if the app exists in the route pool.
6
+ #
7
+ # Raises an exception if the requested app is not in the route pool.
8
+ def excursion(app_name)
9
+ raise NotInPool, "Application is not registered in the excursion route pool: '#{app_name}'" unless app_exists?(app_name)
10
+
11
+ return Helpers.helper(app_name) unless Helpers.helper(app_name).nil?
12
+ Helpers.register_helper(UrlHelper.new(excursion_app(app_name)))
7
13
  end
8
14
 
9
15
  def method_missing(meth, *args)
10
- if meth.to_s.match(/\A(#{routes.collect { |name,route| name }.join("|")})_(url|path)\Z/)
11
- ActionDispatch::Routing::RouteSet::NamedRouteCollection::UrlHelper.create(routes.get($1.to_sym), @application.default_url_options).call(Rails.application.routes, args)
16
+ if app_exists?(meth.to_s)
17
+ excursion(meth.to_s)
12
18
  else
13
19
  super
14
20
  end
15
21
  end
16
22
 
17
- protected
23
+ def app_exists?(app_name)
24
+ !excursion_app(app_name).nil?
25
+ end
18
26
 
19
- def initialize(app)
20
- @application = app
27
+ def excursion_app(app_name)
28
+ Pool.application(app_name)
21
29
  end
22
30
  end
23
31
  end
24
32
  end
33
+
34
+ ActionController::Base.send :include, Excursion::Helpers::ApplicationHelper
35
+ ActionController::Base.send :helper, Excursion::Helpers::ApplicationHelper
@@ -0,0 +1,25 @@
1
+ module Excursion
2
+ module Helpers
3
+ class UrlHelper
4
+ attr_reader :application
5
+
6
+ def routes
7
+ @application.routes
8
+ end
9
+
10
+ def method_missing(meth, *args)
11
+ if meth.to_s.match(/\A(#{routes.collect { |name,route| name }.join("|")})_(url|path)\Z/)
12
+ ActionDispatch::Routing::RouteSet::NamedRouteCollection::UrlHelper.create(routes.get($1.to_sym), @application.default_url_options).call(Rails.application.routes, args)
13
+ else
14
+ super
15
+ end
16
+ end
17
+
18
+ protected
19
+
20
+ def initialize(app)
21
+ @application = app
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,7 +1,31 @@
1
+ require 'excursion/helpers/url_helper'
1
2
  require 'excursion/helpers/application_helper'
2
- require 'excursion/helpers/helper'
3
3
 
4
- module Exursion
4
+ module Excursion
5
5
  module Helpers
6
+ def self.helpers
7
+ @helpers ||= {}
8
+ end
9
+
10
+ def self.helper(name)
11
+ helpers[name]
12
+ end
13
+
14
+ # Helpers register themselves here when they're created so they can be shared
15
+ # between different instances (like the StaticHelper below and ActionController)
16
+ def self.register_helper(h)
17
+ @helpers ||= {}
18
+ @helpers[h.application.name] = h
19
+ h
20
+ end
21
+ end
22
+
23
+ class StaticHelper
24
+ include Helpers::ApplicationHelper
25
+ end
26
+
27
+ # Provides quick global access to url helpers with using the StaticHelper
28
+ def self.url_helpers
29
+ @url_helpers ||= StaticHelper.new
6
30
  end
7
31
  end
@@ -1,4 +1,6 @@
1
1
  require 'excursion/pool/application'
2
+ require 'excursion/exceptions/pool'
3
+ require 'excursion/exceptions/datasources'
2
4
 
3
5
  module Excursion
4
6
  module Pool
@@ -20,11 +22,14 @@ module Excursion
20
22
  end
21
23
 
22
24
  def self.datasource
23
- raise if Excursion.configuration.datasource.nil?
25
+ raise NoDatasourceError, "You must configure excursion with a datasource." if Excursion.configuration.datasource.nil?
24
26
  require "excursion/datasources/#{Excursion.configuration.datasource.to_s}"
25
27
  @@datasource ||= "Excursion::Datasources::#{Excursion.configuration.datasource.to_s.camelize}".constantize.new
26
- rescue
27
- raise "Could not initialize your datasource. Make sure you have properly configured it"
28
+ #rescue NoDatasourceError => e
29
+ #raise e
30
+ rescue StandardError => e
31
+ raise e
32
+ #raise InvalidDatasourceError, "Could not initialize your datasource. Make sure you have properly configured it"
28
33
  end
29
34
  end
30
35
  end
@@ -1,3 +1,3 @@
1
1
  module Excursion
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/lib/excursion.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'excursion/exceptions'
1
2
  require 'excursion/configuration'
2
3
 
3
4
  module Excursion
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: excursion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Rebec
@@ -48,13 +48,18 @@ extensions: []
48
48
  extra_rdoc_files: []
49
49
  files:
50
50
  - lib/excursion/pool.rb
51
+ - lib/excursion/exceptions/pool.rb
52
+ - lib/excursion/exceptions/datasources.rb
53
+ - lib/excursion/exceptions/memcache.rb
51
54
  - lib/excursion/pool/application.rb
52
55
  - lib/excursion/railtie.rb
53
56
  - lib/excursion/configuration.rb
54
57
  - lib/excursion/datasources/file.rb
55
58
  - lib/excursion/datasources/datasource.rb
59
+ - lib/excursion/datasources/memcache.rb
60
+ - lib/excursion/helpers/url_helper.rb
56
61
  - lib/excursion/helpers/application_helper.rb
57
- - lib/excursion/helpers/helper.rb
62
+ - lib/excursion/exceptions.rb
58
63
  - lib/excursion/helpers.rb
59
64
  - lib/excursion/version.rb
60
65
  - lib/excursion.rb
@@ -1,22 +0,0 @@
1
- module Excursion
2
- module Helpers
3
- module Helper
4
-
5
- def method_missing(meth, *args)
6
- if !(app = Pool.application(meth.to_s)).nil?
7
- @application_helpers ||= {}
8
- @application_helpers[app.name] ||= ApplicationHelper.new(app)
9
- else
10
- begin
11
- super
12
- rescue NoMethodError => e
13
- raise "Excursion URL helper method does not exist: #{meth}"
14
- end
15
- end
16
- end
17
- end
18
- end
19
- end
20
-
21
- ActionController::Base.send :include, Excursion::Helpers::Helper
22
- ActionController::Base.send :helper, Excursion::Helpers::Helper