snapimage 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Snapimage
2
2
 
3
- TODO: Write a gem description
3
+ SnapImage is a Rack middleware that supports server-side image uploading for [SnapEditor](http://snapeditor.com), an online HTML5 WYSIWYG text editor. It also provides a self-contained server that is ready to roll.
4
4
 
5
5
  ## Installation
6
6
 
@@ -16,9 +16,34 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install snapimage
18
18
 
19
+ ## Configuration
20
+
21
+ Generate a config file (default is "config/snapimage\_config.yml"). SnapImage comes with a script to do that.
22
+
23
+ $ snapimage_generate_config <local root>
24
+
25
+ The local root argument is a path that tells SnapImage where to store the uploaded files. For other options, use the -h flag.
26
+
27
+ $ snapimage_generate_config -h
28
+
19
29
  ## Usage
20
30
 
21
- TODO: Write usage instructions here
31
+ The middleware class is SnapImage::Middleware. It takes the following options.
32
+
33
+ path: The URL path that SnapImage listens to and accepts image uploads from (default is "/snapimage_api").
34
+ config: The path to the config file (default is "config/snapimage_config.yml").
35
+
36
+ ### Rails
37
+
38
+ Add the following to application.rb.
39
+
40
+ config.middleware.use SnapImage::Middleware
41
+
42
+ ### Other Rack Applications
43
+
44
+ Add the following to your server.
45
+
46
+ use SnapImage::Middleware
22
47
 
23
48
  ## Contributing
24
49
 
@@ -1,9 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "optparse"
4
+ require "fileutils"
4
5
 
5
6
  options = {
6
- file: "snapimage_config.yml",
7
+ file: "config/snapimage_config.yml",
7
8
  size: 10485760,
8
9
  force: false
9
10
  }
@@ -17,7 +18,7 @@ optparse = OptionParser.new do |opts|
17
18
  opts.separator ""
18
19
  opts.separator "Options:"
19
20
 
20
- opts.on("-f", "--file FILE", "File to generate (Default: snapimage_config.yml)") do |file|
21
+ opts.on("-f", "--file FILE", "File to generate (Default: config/snapimage_config.yml)") do |file|
21
22
  options[:file] = file
22
23
  end
23
24
 
@@ -51,6 +52,7 @@ if !options[:force] && File.exists?(options[:file])
51
52
  exit
52
53
  end
53
54
 
55
+ FileUtils.mkdir_p(File.dirname(options[:file]))
54
56
  File.open(options[:file], "w") do |f|
55
57
  f.write(<<-EOF
56
58
  directory: "#{local_root}"
@@ -7,7 +7,7 @@ options = {
7
7
  }
8
8
 
9
9
  optparse = OptionParser.new do |opts|
10
- opts.banner = "Usage: snapimage_server config [options]"
10
+ opts.banner = "Usage: snapimage_server [options]"
11
11
 
12
12
  opts.on("-p", "--port PORT", "Set the port (Default: 54321)") do |port|
13
13
  options[:port] = port.to_i
@@ -17,6 +17,10 @@ optparse = OptionParser.new do |opts|
17
17
  options[:path] = path
18
18
  end
19
19
 
20
+ opts.on("-c", "--config CONFIG", "Config file (Default: config/snapimage_config.yml") do |config|
21
+ options[:config] = config
22
+ end
23
+
20
24
  opts.on("-h", "--help", "Display the help screen") do
21
25
  puts opts
22
26
  exit
@@ -24,12 +28,6 @@ optparse = OptionParser.new do |opts|
24
28
  end
25
29
  optparse.parse!
26
30
 
27
- unless ARGV.length == 1
28
- puts optparse.help
29
- exit
30
- end
31
- options[:config] = ARGV[0]
32
-
33
31
  require "sinatra"
34
32
  require "snapimage"
35
33
 
@@ -3,16 +3,15 @@ module SnapImage
3
3
  class Middleware
4
4
  # Arguments:
5
5
  # * app:: Rack application
6
+ # * options:: Options for the middleware
7
+ #
8
+ # Options:
6
9
  # * path:: The URL path to access the SnapImage API (defaults to "/snapimage_api")
7
- # * config:: Filename of the YAML or JSON config file or a config Hash
10
+ # * config:: Filename of the YAML or JSON config file or a config Hash # (defaults to "config/snapimage_config.yml")
8
11
  def initialize(app, options = {})
9
12
  @app = app
10
13
  @path = options[:path] || "/snapimage_api"
11
- # TODO: If no config is given, set defaults.
12
- # For example, if it's a Rails app, set the filename to
13
- # config/snapimage.yml.
14
- raise SnapImage::MissingConfig, "Missing config." if options[:config].nil?
15
- @config = SnapImage::Config.new(options[:config])
14
+ @config = SnapImage::Config.new(options[:config] || "config/snapimage_config.yml")
16
15
  end
17
16
 
18
17
  def call(env)
@@ -1,3 +1,3 @@
1
1
  module SnapImage
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -23,7 +23,7 @@ describe "Upload" do
23
23
  SnapImage::Middleware.new(
24
24
  app,
25
25
  path: "/snapimage_api",
26
- config: { "directory" => File.join(RSpec.root, "storage") }
26
+ config: { "directory" => File.join(RSpec.root, "storage"), "max_file_size" => 600 }
27
27
  )
28
28
  end
29
29
 
@@ -41,34 +41,28 @@ describe "Upload" do
41
41
  end
42
42
 
43
43
  it "stores the image" do
44
- json = JSON.parse(last_response.body)
45
44
  path = File.join(@local_root, @directory, File.basename(@image_path))
46
45
  File.exist?(path).should be_true
47
46
  end
48
47
  end
49
48
 
50
- #context "upload too large" do
51
- #before do
52
- #json = { action: "generate_image", resource_identifier: @resource_id }.to_json
53
- #post "/snapimage_api", "file" => Rack::Test::UploadedFile.new(@large_image_path, "image/png"), "json" => json
54
- #end
49
+ context "upload too large" do
50
+ before do
51
+ post "/snapimage_api", "file" => Rack::Test::UploadedFile.new(@large_image_path, "image/png"), "directory" => @directory
52
+ end
55
53
 
56
- #it "resizes successfully" do
57
- #last_response.should be_successful
58
- #last_response["Content-Type"].should eq "text/json"
59
- #json = JSON.parse(last_response.body)
60
- #json["status_code"].should eq 200
61
- #json["message"].should eq "Get Modified Image Successful"
62
- #json["image_url"].should match Regexp.new("^//example.com/images/abc/123/[a-z0-9]{8}-1024x50.png$")
63
- #json["image_width"].should eq 1024
64
- #json["image_height"].should eq 50
65
- #end
54
+ it "fails" do
55
+ last_response.should be_successful
56
+ last_response["Content-Type"].should eq "text/json"
57
+ json = JSON.parse(last_response.body)
58
+ json["status_code"].should eq 405
59
+ json["message"].should eq "File Too Large"
60
+ end
66
61
 
67
- #it "stores the image" do
68
- #json = JSON.parse(last_response.body)
69
- #path = File.join(@local_root, @resource_id, File.basename(json["image_url"]))
70
- #File.exist?(path).should be_true
71
- #end
72
- #end
62
+ it "does not store the image" do
63
+ path = File.join(@local_root, @directory, File.basename(@image_path))
64
+ File.exist?(path).should be_false
65
+ end
66
+ end
73
67
  end
74
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snapimage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-03 00:00:00.000000000 Z
12
+ date: 2012-12-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -222,7 +222,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
222
222
  version: '0'
223
223
  segments:
224
224
  - 0
225
- hash: -1529708753880950657
225
+ hash: 2769920436023059955
226
226
  required_rubygems_version: !ruby/object:Gem::Requirement
227
227
  none: false
228
228
  requirements:
@@ -231,7 +231,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
231
231
  version: '0'
232
232
  segments:
233
233
  - 0
234
- hash: -1529708753880950657
234
+ hash: 2769920436023059955
235
235
  requirements: []
236
236
  rubyforge_project:
237
237
  rubygems_version: 1.8.23