dragonfly 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of dragonfly might be problematic. Click here for more details.

Files changed (157) hide show
  1. data/.gitignore +2 -0
  2. data/.specopts +2 -0
  3. data/.yardopts +11 -5
  4. data/Gemfile +22 -0
  5. data/Gemfile.rails.2.3.5 +13 -0
  6. data/History.md +49 -0
  7. data/README.md +18 -28
  8. data/Rakefile +24 -36
  9. data/VERSION +1 -1
  10. data/config.ru +4 -1
  11. data/dragonfly.gemspec +85 -99
  12. data/extra_docs/Analysers.md +66 -30
  13. data/extra_docs/Caching.md +22 -0
  14. data/extra_docs/Configuration.md +116 -0
  15. data/extra_docs/DataStorage.md +114 -14
  16. data/extra_docs/Encoding.md +62 -37
  17. data/extra_docs/GeneralUsage.md +118 -0
  18. data/extra_docs/Generators.md +92 -0
  19. data/extra_docs/Heroku.md +51 -0
  20. data/extra_docs/Index.md +8 -9
  21. data/extra_docs/MimeTypes.md +18 -17
  22. data/extra_docs/Models.md +251 -0
  23. data/extra_docs/Processing.md +94 -70
  24. data/extra_docs/Rack.md +53 -0
  25. data/extra_docs/Rails2.md +44 -0
  26. data/extra_docs/Rails3.md +51 -0
  27. data/extra_docs/Sinatra.md +21 -0
  28. data/extra_docs/URLs.md +114 -0
  29. data/features/images.feature +6 -7
  30. data/features/no_processing.feature +0 -6
  31. data/features/rails_2.3.5.feature +1 -1
  32. data/features/rails_3.0.0.rc.feature +8 -0
  33. data/features/steps/dragonfly_steps.rb +14 -12
  34. data/features/steps/rails_steps.rb +20 -9
  35. data/features/support/env.rb +10 -11
  36. data/fixtures/files/app/views/albums/new.html.erb +4 -4
  37. data/fixtures/files/app/views/albums/show.html.erb +1 -1
  38. data/fixtures/files/features/manage_album_images.feature +1 -1
  39. data/fixtures/files/features/step_definitions/{album_steps.rb → image_steps.rb} +4 -3
  40. data/fixtures/files/features/support/paths.rb +2 -0
  41. data/fixtures/files/features/text_images.feature +7 -0
  42. data/fixtures/rails_3.0.0.rc/template.rb +21 -0
  43. data/irbrc.rb +2 -1
  44. data/lib/dragonfly.rb +4 -16
  45. data/lib/dragonfly/{active_record_extensions.rb → active_model_extensions.rb} +1 -1
  46. data/lib/dragonfly/active_model_extensions/attachment.rb +146 -0
  47. data/lib/dragonfly/{active_record_extensions → active_model_extensions}/class_methods.rb +5 -6
  48. data/lib/dragonfly/{active_record_extensions → active_model_extensions}/instance_methods.rb +1 -1
  49. data/lib/dragonfly/{active_record_extensions → active_model_extensions}/validations.rb +5 -9
  50. data/lib/dragonfly/analyser.rb +59 -0
  51. data/lib/dragonfly/analysis/file_command_analyser.rb +1 -1
  52. data/lib/dragonfly/analysis/r_magick_analyser.rb +46 -31
  53. data/lib/dragonfly/app.rb +138 -173
  54. data/lib/dragonfly/config/heroku.rb +19 -0
  55. data/lib/dragonfly/config/r_magick.rb +37 -0
  56. data/lib/dragonfly/config/{rails_defaults.rb → rails.rb} +6 -7
  57. data/lib/dragonfly/configurable.rb +30 -27
  58. data/lib/dragonfly/core_ext/object.rb +1 -1
  59. data/lib/dragonfly/data_storage/file_data_store.rb +59 -26
  60. data/lib/dragonfly/data_storage/mongo_data_store.rb +65 -0
  61. data/lib/dragonfly/data_storage/s3data_store.rb +31 -12
  62. data/lib/dragonfly/encoder.rb +13 -0
  63. data/lib/dragonfly/encoding/r_magick_encoder.rb +10 -19
  64. data/lib/dragonfly/endpoint.rb +43 -0
  65. data/lib/dragonfly/function_manager.rb +65 -0
  66. data/lib/dragonfly/{processing/r_magick_text_processor.rb → generation/r_magick_generator.rb} +25 -11
  67. data/lib/dragonfly/generator.rb +9 -0
  68. data/lib/dragonfly/job.rb +290 -0
  69. data/lib/dragonfly/job_builder.rb +39 -0
  70. data/lib/dragonfly/job_definitions.rb +26 -0
  71. data/lib/dragonfly/job_endpoint.rb +17 -0
  72. data/lib/dragonfly/loggable.rb +28 -0
  73. data/lib/dragonfly/middleware.rb +21 -14
  74. data/lib/dragonfly/processing/r_magick_processor.rb +71 -48
  75. data/lib/dragonfly/processor.rb +9 -0
  76. data/lib/dragonfly/r_magick_utils.rb +24 -0
  77. data/lib/dragonfly/rails/images.rb +10 -7
  78. data/lib/dragonfly/routed_endpoint.rb +42 -0
  79. data/lib/dragonfly/serializer.rb +32 -0
  80. data/lib/dragonfly/simple_cache.rb +23 -0
  81. data/lib/dragonfly/simple_endpoint.rb +64 -0
  82. data/lib/dragonfly/temp_object.rb +77 -45
  83. data/spec/argument_matchers.rb +7 -17
  84. data/spec/dragonfly/active_model_extensions/active_model_setup.rb +97 -0
  85. data/spec/dragonfly/active_model_extensions/active_record_setup.rb +85 -0
  86. data/spec/dragonfly/{active_record_extensions → active_model_extensions}/model_spec.rb +282 -244
  87. data/spec/dragonfly/active_model_extensions/spec_helper.rb +11 -0
  88. data/spec/dragonfly/analyser_spec.rb +123 -0
  89. data/spec/dragonfly/analysis/file_command_analyser_spec.rb +2 -2
  90. data/spec/dragonfly/analysis/r_magick_analyser_spec.rb +10 -1
  91. data/spec/dragonfly/app_spec.rb +175 -69
  92. data/spec/dragonfly/configurable_spec.rb +14 -0
  93. data/spec/dragonfly/data_storage/data_store_spec.rb +36 -9
  94. data/spec/dragonfly/data_storage/file_data_store_spec.rb +61 -38
  95. data/spec/dragonfly/data_storage/mongo_data_store_spec.rb +18 -0
  96. data/spec/dragonfly/data_storage/s3_data_store_spec.rb +34 -39
  97. data/spec/dragonfly/deprecation_spec.rb +20 -0
  98. data/spec/dragonfly/function_manager_spec.rb +154 -0
  99. data/spec/dragonfly/generation/r_magick_generator_spec.rb +119 -0
  100. data/spec/dragonfly/job_builder_spec.rb +37 -0
  101. data/spec/dragonfly/job_definitions_spec.rb +35 -0
  102. data/spec/dragonfly/job_endpoint_spec.rb +66 -0
  103. data/spec/dragonfly/job_spec.rb +605 -0
  104. data/spec/dragonfly/loggable_spec.rb +80 -0
  105. data/spec/dragonfly/middleware_spec.rb +37 -17
  106. data/spec/dragonfly/processing/r_magick_processor_spec.rb +182 -166
  107. data/spec/dragonfly/routed_endpoint_spec.rb +48 -0
  108. data/spec/dragonfly/serializer_spec.rb +61 -0
  109. data/spec/dragonfly/simple_cache_spec.rb +27 -0
  110. data/spec/dragonfly/simple_endpoint_spec.rb +78 -0
  111. data/spec/dragonfly/temp_object_spec.rb +154 -119
  112. data/spec/simple_matchers.rb +22 -0
  113. data/spec/spec_helper.rb +28 -4
  114. data/yard/templates/default/layout/html/layout.erb +18 -11
  115. metadata +89 -190
  116. data/config.rb +0 -5
  117. data/extra_docs/ActiveRecord.md +0 -196
  118. data/extra_docs/ExampleUseCases.md +0 -189
  119. data/extra_docs/GettingStarted.md +0 -114
  120. data/extra_docs/Shortcuts.md +0 -118
  121. data/extra_docs/UsingWithRails.md +0 -81
  122. data/features/rails_3.0.0.beta3.feature +0 -7
  123. data/fixtures/rails_3.0.0.beta3/template.rb +0 -16
  124. data/lib/dragonfly/active_record_extensions/attachment.rb +0 -170
  125. data/lib/dragonfly/analyser_list.rb +0 -9
  126. data/lib/dragonfly/analysis/base.rb +0 -10
  127. data/lib/dragonfly/belongs_to_app.rb +0 -24
  128. data/lib/dragonfly/config/heroku_rails_images.rb +0 -23
  129. data/lib/dragonfly/config/r_magick_images.rb +0 -69
  130. data/lib/dragonfly/config/r_magick_text.rb +0 -25
  131. data/lib/dragonfly/config/rails_images.rb +0 -13
  132. data/lib/dragonfly/data_storage/base.rb +0 -21
  133. data/lib/dragonfly/data_storage/base64_data_store.rb +0 -23
  134. data/lib/dragonfly/data_storage/transparent_data_store.rb +0 -21
  135. data/lib/dragonfly/delegatable.rb +0 -14
  136. data/lib/dragonfly/delegator.rb +0 -62
  137. data/lib/dragonfly/encoder_list.rb +0 -9
  138. data/lib/dragonfly/encoding/base.rb +0 -14
  139. data/lib/dragonfly/encoding/transparent_encoder.rb +0 -14
  140. data/lib/dragonfly/extended_temp_object.rb +0 -120
  141. data/lib/dragonfly/parameters.rb +0 -163
  142. data/lib/dragonfly/processing/base.rb +0 -10
  143. data/lib/dragonfly/processor_list.rb +0 -9
  144. data/lib/dragonfly/url_handler.rb +0 -147
  145. data/spec/dragonfly/active_record_extensions/attachment_spec.rb +0 -8
  146. data/spec/dragonfly/active_record_extensions/migration.rb +0 -42
  147. data/spec/dragonfly/active_record_extensions/models.rb +0 -6
  148. data/spec/dragonfly/active_record_extensions/spec_helper.rb +0 -24
  149. data/spec/dragonfly/belongs_to_app_spec.rb +0 -55
  150. data/spec/dragonfly/delegatable_spec.rb +0 -32
  151. data/spec/dragonfly/delegator_spec.rb +0 -145
  152. data/spec/dragonfly/extended_temp_object_spec.rb +0 -71
  153. data/spec/dragonfly/parameters_spec.rb +0 -298
  154. data/spec/dragonfly/processing/r_magick_text_processor_spec.rb +0 -84
  155. data/spec/dragonfly/url_handler_spec.rb +0 -247
  156. data/spec/dragonfly_spec.rb +0 -16
  157. data/spec/ginger_scenarios.rb +0 -13
@@ -0,0 +1,39 @@
1
+ module Dragonfly
2
+ class JobBuilder
3
+
4
+ def initialize(&block)
5
+ @block = block
6
+ end
7
+
8
+ def build(job, *args)
9
+ evaluate_block(job, false, *args)
10
+ end
11
+
12
+ def build!(job, *args)
13
+ evaluate_block(job, true, *args)
14
+ end
15
+
16
+ Job.step_names.each do |step|
17
+
18
+ # fetch, process, etc.
19
+ define_method step do |*args|
20
+ if @perform_with_bangs
21
+ @job.send("#{step}!", *args)
22
+ else
23
+ @job = @job.send(step, *args)
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ private
30
+
31
+ def evaluate_block(job, perform_with_bangs, *args)
32
+ @job = job
33
+ @perform_with_bangs = perform_with_bangs
34
+ instance_exec(*args, &@block)
35
+ @job
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,26 @@
1
+ module Dragonfly
2
+ class JobDefinitions < Module
3
+
4
+ def initialize
5
+ @job_definitions = {}
6
+ end
7
+
8
+ def add(name, &definition_proc)
9
+ job_definitions[name] = JobBuilder.new(&definition_proc)
10
+ jd = job_definitions # Needed because we're about to change 'self'
11
+
12
+ define_method name do |*args|
13
+ jd[name].build(self, *args)
14
+ end
15
+
16
+ define_method "#{name}!" do |*args|
17
+ jd[name].build!(self, *args)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :job_definitions
24
+
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ module Dragonfly
2
+ class JobEndpoint
3
+
4
+ include Endpoint
5
+
6
+ def initialize(job)
7
+ @job = job
8
+ end
9
+
10
+ def call(env={})
11
+ response_for_job(job, env)
12
+ end
13
+
14
+ attr_reader :job
15
+
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ require 'logger'
2
+
3
+ module Dragonfly
4
+ module Loggable
5
+
6
+ def log
7
+ case @log_object
8
+ when nil
9
+ @log_object = Logger.new($stdout)
10
+ when Proc
11
+ @log_object[]
12
+ when Logger
13
+ @log_object
14
+ end
15
+ end
16
+
17
+ def log=(object)
18
+ @log_object = object
19
+ end
20
+
21
+ attr_reader :log_object
22
+
23
+ def use_same_log_as(object)
24
+ self.log = proc{ object.log }
25
+ end
26
+
27
+ end
28
+ end
@@ -1,27 +1,34 @@
1
1
  module Dragonfly
2
-
3
2
  class Middleware
4
-
5
- def initialize(app, dragonfly_app_name)
3
+
4
+ def initialize(app, dragonfly_app_name, path_prefix)
6
5
  @app = app
7
- @dragonfly_app_name = dragonfly_app_name
6
+ @endpoint = Rack::Builder.new {
7
+ map path_prefix do
8
+ run Dragonfly[dragonfly_app_name]
9
+ end
10
+ }.to_app
8
11
  end
9
-
12
+
10
13
  def call(env)
11
- response = endpoint.call(env)
12
- if response[0] == 404
14
+ response = @endpoint.call(env)
15
+ if route_not_found?(response)
13
16
  @app.call(env)
14
17
  else
15
18
  response
16
19
  end
17
20
  end
18
-
21
+
19
22
  private
20
-
21
- def endpoint
22
- App[@dragonfly_app_name]
23
+
24
+ def route_not_found?(response)
25
+ response[1]['X-Cascade'] == 'pass' ||
26
+ (rack_version_doesnt_support_x_cascade? && response[0] == 404)
27
+ end
28
+
29
+ def rack_version_doesnt_support_x_cascade?
30
+ Rack.version < '1.1'
23
31
  end
24
-
25
- end
26
32
 
27
- end
33
+ end
34
+ end
@@ -2,8 +2,8 @@ require 'RMagick'
2
2
 
3
3
  module Dragonfly
4
4
  module Processing
5
- class RMagickProcessor < Base
6
-
5
+ class RMagickProcessor
6
+
7
7
  GRAVITIES = {
8
8
  'nw' => Magick::NorthWestGravity,
9
9
  'n' => Magick::NorthGravity,
@@ -16,14 +16,14 @@ module Dragonfly
16
16
  'se' => Magick::SouthEastGravity
17
17
  }
18
18
 
19
- def generate(width, height, format='png')
20
- image = Magick::Image.read("plasma:fractal"){self.size = "#{width}x#{height}"}.first
21
- image.format = format.to_s
22
- image.to_blob
23
- end
24
-
25
- # Processing methods
26
-
19
+ # Geometry string patterns
20
+ RESIZE_GEOMETRY = /^\d*x\d*[><%^!]?$|^\d+@$/ # e.g. '300x200!'
21
+ CROPPED_RESIZE_GEOMETRY = /^(\d+)x(\d+)#(\w{1,2})?$/ # e.g. '20x50#ne'
22
+ CROP_GEOMETRY = /^(\d+)x(\d+)([+-]\d+)?([+-]\d+)?(\w{1,2})?$/ # e.g. '30x30+10+10'
23
+ THUMB_GEOMETRY = Regexp.union RESIZE_GEOMETRY, CROPPED_RESIZE_GEOMETRY, CROP_GEOMETRY
24
+
25
+ include RMagickUtils
26
+
27
27
  def crop(temp_object, opts={})
28
28
  x = opts[:x].to_i
29
29
  y = opts[:y].to_i
@@ -31,49 +31,79 @@ module Dragonfly
31
31
  width = opts[:width].to_i
32
32
  height = opts[:height].to_i
33
33
 
34
- image = rmagick_image(temp_object)
34
+ rmagick_image(temp_object) do |image|
35
+ # RMagick throws an error if the cropping area is bigger than the image,
36
+ # when the gravity is something other than nw
37
+ width = image.columns - x if x + width > image.columns
38
+ height = image.rows - y if y + height > image.rows
39
+ image.crop(gravity, x, y, width, height)
40
+ end
41
+ end
35
42
 
36
- # RMagick throws an error if the cropping area is bigger than the image,
37
- # when the gravity is something other than nw
38
- width = image.columns - x if x + width > image.columns
39
- height = image.rows - y if y + height > image.rows
43
+ def flip(temp_object)
44
+ rmagick_image(temp_object) do |image|
45
+ image.flip!
46
+ end
47
+ end
40
48
 
41
- image.crop(gravity, x, y, width, height).to_blob
49
+ def flop(temp_object)
50
+ rmagick_image(temp_object) do |image|
51
+ image.flop!
52
+ end
42
53
  end
43
-
54
+
44
55
  def greyscale(temp_object, opts={})
45
56
  depth = opts[:depth] || 256
46
- rmagick_image(temp_object).quantize(depth, Magick::GRAYColorspace).to_blob
57
+ rmagick_image(temp_object) do |image|
58
+ image.quantize(depth, Magick::GRAYColorspace)
59
+ end
47
60
  end
48
61
  alias grayscale greyscale
49
-
50
- def resize(temp_object, opts={})
51
- rmagick_image(temp_object).change_geometry!(opts[:geometry]) do |cols, rows, img|
52
- img.resize!(cols, rows)
53
- end.to_blob
62
+
63
+ def resize(temp_object, geometry)
64
+ rmagick_image(temp_object) do |image|
65
+ image.change_geometry!(geometry) do |cols, rows, img|
66
+ img.resize!(cols, rows)
67
+ end
68
+ end
54
69
  end
55
70
 
56
71
  def resize_and_crop(temp_object, opts={})
57
- image = rmagick_image(temp_object)
58
-
59
- width = opts[:width] ? opts[:width].to_i : image.columns
60
- height = opts[:height] ? opts[:height].to_i : image.rows
61
- gravity = GRAVITIES[opts[:gravity]] || Magick::CenterGravity
72
+ rmagick_image(temp_object) do |image|
62
73
 
63
- image.crop_resized(width, height, gravity).to_blob
74
+ width = opts[:width] ? opts[:width].to_i : image.columns
75
+ height = opts[:height] ? opts[:height].to_i : image.rows
76
+ gravity = GRAVITIES[opts[:gravity]] || Magick::CenterGravity
77
+
78
+ image.crop_resized(width, height, gravity)
79
+ end
64
80
  end
65
81
 
66
- def rotate(temp_object, opts={})
67
- if opts[:amount]
68
- args = [opts[:amount].to_f]
69
- args << opts[:qualifier] if opts[:qualifier]
70
- image = rmagick_image(temp_object)
82
+ def rotate(temp_object, amount, opts={})
83
+ args = [amount.to_f]
84
+ args << opts[:qualifier] if opts[:qualifier]
85
+ rmagick_image(temp_object) do |image|
71
86
  image.background_color = opts[:background_colour] if opts[:background_colour]
72
87
  image.background_color = opts[:background_color] if opts[:background_color]
73
- rotated_image = image.rotate(*args)
74
- rotated_image ? rotated_image.to_blob : temp_object
75
- else
76
- temp_object
88
+ image.rotate(*args) || temp_object
89
+ end
90
+ end
91
+
92
+ def thumb(temp_object, geometry)
93
+ case geometry
94
+ when RESIZE_GEOMETRY
95
+ resize(temp_object, geometry)
96
+ when CROPPED_RESIZE_GEOMETRY
97
+ resize_and_crop(temp_object, :width => $1, :height => $2, :gravity => $3)
98
+ when CROP_GEOMETRY
99
+ crop(temp_object,
100
+ :width => $1,
101
+ :height => $2,
102
+ :x => $3,
103
+ :y => $4,
104
+ :gravity => $5
105
+ )
106
+ else raise ArgumentError, "Didn't recognise the geometry string #{geometry}"
77
107
  end
78
108
  end
79
109
 
@@ -83,16 +113,9 @@ module Dragonfly
83
113
  radius = opts[:radius].to_f || 0.0
84
114
  sigma = opts[:sigma].to_f || 10.0
85
115
 
86
- rmagick_image(temp_object).vignette(x, y, radius, sigma).to_blob
87
- end
88
-
89
- private
90
-
91
- def rmagick_image(temp_object)
92
- Magick::Image.from_blob(temp_object.data).first
93
- rescue Magick::ImageMagickError => e
94
- log.warn("Unable to handle content in #{self.class} - got:\n#{e}")
95
- throw :unable_to_handle
116
+ rmagick_image(temp_object) do |image|
117
+ image.vignette(x, y, radius, sigma)
118
+ end
96
119
  end
97
120
 
98
121
  end
@@ -0,0 +1,9 @@
1
+ module Dragonfly
2
+ class Processor < FunctionManager
3
+
4
+ def process(temp_object, method, *args)
5
+ call_last(method, temp_object, *args)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,24 @@
1
+ module Dragonfly
2
+ module RMagickUtils
3
+
4
+ private
5
+
6
+ def rmagick_image(temp_object, &block)
7
+ image = Magick::Image.from_blob(temp_object.data).first
8
+ result = block[image]
9
+ case result
10
+ when Magick::Image, Magick::ImageList
11
+ content = result.to_blob
12
+ result.destroy!
13
+ else
14
+ content = result
15
+ end
16
+ image.destroy! unless image.destroyed?
17
+ content
18
+ rescue Magick::ImageMagickError => e
19
+ log.warn("Unable to handle content in #{self.class} - got:\n#{e}")
20
+ throw :unable_to_handle
21
+ end
22
+
23
+ end
24
+ end
@@ -1,19 +1,22 @@
1
1
  require 'dragonfly'
2
2
  require 'rack/cache'
3
+ require 'uri'
3
4
 
4
5
  ### The dragonfly app ###
5
- app = Dragonfly::App[:images]
6
- app.configure_with(Dragonfly::Config::RailsImages)
6
+ app = Dragonfly[:images]
7
+ app.configure_with(:rails)
8
+ app.configure_with(:rmagick)
7
9
 
8
10
  ### Extend active record ###
9
- Dragonfly.active_record_macro(:image, app)
11
+ app.define_macro(ActiveRecord::Base, :image_accessor)
10
12
 
11
13
  ### Insert the middleware ###
12
14
  # Where the middleware is depends on the version of Rails
13
15
  middleware = Rails.respond_to?(:application) ? Rails.application.middleware : ActionController::Dispatcher.middleware
14
- middleware.insert_after Rack::Lock, Dragonfly::Middleware, :images
15
- middleware.insert_before Dragonfly::Middleware, Rack::Cache, {
16
+
17
+ middleware.insert 0, Dragonfly::Middleware, :images, app.url_path_prefix
18
+ middleware.insert 0, Rack::Cache, {
16
19
  :verbose => true,
17
- :metastore => "file:#{Rails.root}/tmp/dragonfly/cache/meta",
18
- :entitystore => "file:#{Rails.root}/tmp/dragonfly/cache/body"
20
+ :metastore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/meta"), # URI encoded because Windows
21
+ :entitystore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/body") # has problems with spaces
19
22
  }
@@ -0,0 +1,42 @@
1
+ module Dragonfly
2
+ class RoutedEndpoint
3
+
4
+ include Endpoint
5
+
6
+ class NoRoutingParams < RuntimeError; end
7
+
8
+ def initialize(app, &block)
9
+ @app = app
10
+ @block = block
11
+ end
12
+
13
+ def call(env)
14
+ params = symbolize_keys_of Rack::Request.new(env).params
15
+ job = @block.call(params.merge(routing_params(env)), @app)
16
+ response_for_job(job, env)
17
+ rescue Job::NoSHAGiven => e
18
+ [400, {"Content-Type" => 'text/plain'}, ["You need to give a SHA parameter"]]
19
+ rescue Job::IncorrectSHA => e
20
+ [400, {"Content-Type" => 'text/plain'}, ["The SHA parameter you gave (#{e}) is incorrect"]]
21
+ end
22
+
23
+ private
24
+
25
+ def routing_params(env)
26
+ env['rack.routing_args'] ||
27
+ env['action_dispatch.request.path_parameters'] ||
28
+ env['router.params'] ||
29
+ env['usher.params'] ||
30
+ env['dragonfly.params'] ||
31
+ raise(NoRoutingParams, "couldn't find any routing parameters in env #{env.inspect}")
32
+ end
33
+
34
+ def symbolize_keys_of(hash)
35
+ hash.inject({}) do |h, (key, value)|
36
+ h[(key.to_sym rescue key) || key] = value
37
+ h
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ require 'base64'
3
+
4
+ module Dragonfly
5
+ module Serializer
6
+
7
+ # Exceptions
8
+ class BadString < RuntimeError; end
9
+
10
+ extend self # So we can do Serializer.b64_encode, etc.
11
+
12
+ def b64_encode(string)
13
+ Base64.encode64(string).tr("\n=",'')
14
+ end
15
+
16
+ def b64_decode(string)
17
+ padding_length = string.length % 4
18
+ Base64.decode64(string + '=' * padding_length)
19
+ end
20
+
21
+ def marshal_encode(object)
22
+ b64_encode(Marshal.dump(object))
23
+ end
24
+
25
+ def marshal_decode(string)
26
+ Marshal.load(b64_decode(string))
27
+ rescue TypeError, ArgumentError => e
28
+ raise BadString, "couldn't decode #{string} - got #{e}"
29
+ end
30
+
31
+ end
32
+ end