resizor 0.0.3 → 0.0.4

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/lib/resizor/asset.rb CHANGED
@@ -1,21 +1,21 @@
1
1
  module Resizor
2
2
  class Asset
3
3
  attr_accessor :id, :name, :mime_type, :size, :width, :height, :path
4
-
4
+
5
5
  def initialize(options={})
6
- options.each { |k,v| send "#{k}=".to_sym, v }
6
+ options.each { |k,v| send "#{k}=".to_sym, v }
7
7
  end
8
-
8
+
9
9
  def url(options={})
10
10
  options = {:size => '200', :format => 'jpg'}.merge(options)
11
11
  "#{Resizor.api_url(true)}/assets/#{id}.#{options[:format]}?size=#{options[:size]}&token=#{resize_token_for(options)}"
12
12
  end
13
-
13
+
14
14
  def resize_token_for(options={})
15
15
  options = {:size => '200', :format => 'jpg'}.merge(options)
16
16
  Digest::SHA1.hexdigest("#{Resizor.api_key}-#{id}-#{options[:size]}-#{options[:format]}")
17
17
  end
18
-
18
+
19
19
  def save_to_resizor
20
20
  if path && File.exists?(path)
21
21
  ret = Resizor.post('/assets.json', :file => File.open(path))
@@ -32,19 +32,19 @@ module Resizor
32
32
  end
33
33
  return true
34
34
  end
35
-
35
+
36
36
  def destroy
37
37
  ret = Resizor.delete("/assets/#{id}.json")
38
38
  if ret.code == 200
39
39
  return true
40
- end
40
+ end
41
41
  end
42
42
  end
43
-
43
+
44
44
  class AttachedAsset < Resizor::Asset
45
-
45
+
46
46
  attr_accessor :attachment_name, :instance, :file
47
-
47
+
48
48
  def initialize(attachment_name, instance, options = {})
49
49
  @attachment_name = attachment_name
50
50
  @instance = instance
@@ -56,7 +56,7 @@ module Resizor
56
56
  @width = instance_read("width")
57
57
  @height = instance_read("height")
58
58
  end
59
-
59
+
60
60
  def assign in_file
61
61
  if in_file.is_a?(Resizor::Asset)
62
62
  @id = in_file.id
@@ -69,12 +69,12 @@ module Resizor
69
69
  @file = in_file
70
70
  end
71
71
  end
72
-
72
+
73
73
  def save
74
74
  if file
75
75
  @path = File.join(Dir.tmpdir, file.original_filename)
76
- if file.is_a?(Tempfile)
77
- FileUtils.move(file.local_path, @path)
76
+ if file.is_a?(Tempfile) || file.respond_to?(:path)
77
+ FileUtils.move(file.path, @path)
78
78
  else
79
79
  File.open(@path, 'wb') { |f| f.write(file.read) }
80
80
  end
@@ -93,7 +93,7 @@ module Resizor
93
93
  instance_write(:height, @height)
94
94
  return true
95
95
  end
96
-
96
+
97
97
  def delete
98
98
  destroy
99
99
  end
@@ -109,14 +109,14 @@ module Resizor
109
109
  end
110
110
  ret
111
111
  end
112
-
113
- protected
112
+
113
+ protected
114
114
  def instance_write(_attr, value)
115
115
  setter = :"#{attachment_name}_#{_attr}="
116
116
  self.instance_variable_set("@#{_attr.to_s.chop}", value)
117
117
  instance.send(setter, value) if instance.respond_to?(setter)
118
118
  end
119
-
119
+
120
120
  def instance_read(_attr)
121
121
  getter = :"#{attachment_name}_#{_attr}"
122
122
  instance.send(getter) if instance.respond_to?(getter)
@@ -9,40 +9,40 @@ module Resizor
9
9
  @api_key = options[:api_key] || options['api_key']
10
10
  @use_ssl = options[:use_ssl] || options['use_ssl'] || true
11
11
  end
12
-
12
+
13
13
  def get(request_uri, params={}, append_api_key=true)
14
14
  params[:api_key] = @api_key if append_api_key
15
15
  query = make_query(params)
16
16
  do_request do
17
17
  make_response_for resource[request_uri + '?' + query].get
18
18
  end
19
- end
20
-
19
+ end
20
+
21
21
  def post(request_uri, params={}, append_api_key=true)
22
22
  params[:api_key] = @api_key if append_api_key
23
- do_request do
23
+ do_request do
24
24
  make_response_for resource[request_uri].post params
25
25
  end
26
- end
27
-
26
+ end
27
+
28
28
  def delete(request_uri, params={}, append_api_key=true)
29
29
  params[:api_key] = @api_key if append_api_key
30
30
  query = make_query(params)
31
- do_request do
31
+ do_request do
32
32
  make_response_for resource[request_uri + '?' + query].delete
33
33
  end
34
34
  end
35
-
35
+
36
36
  def api_url(force_http = false)
37
- @api_url ||= "#{(@use_ssl == true && force_http == false) ? 'https' : 'http'}://#{@api_host}:#{@api_port}"
37
+ @api_url ||= "#{(@use_ssl == true && force_http == false) ? 'https' : 'http'}://#{@api_host}:#{(@use_ssl == true && force_http == false) ? '443' : @api_port}"
38
38
  end
39
-
39
+
40
40
  protected
41
-
41
+
42
42
  def resource
43
43
  @resource ||= RestClient::Resource.new(api_url)
44
44
  end
45
-
45
+
46
46
  def make_query(params)
47
47
  params.collect {|key, value| [CGI.escape(key.to_s), CGI.escape(value.to_s)].join("=") }.join("&")
48
48
  end
@@ -50,7 +50,7 @@ module Resizor
50
50
  def make_response_for(http_response)
51
51
  Resizor::Response.new(http_response.code, http_response.body)
52
52
  end
53
-
53
+
54
54
  def do_request(&block)
55
55
  begin
56
56
  yield
@@ -59,7 +59,7 @@ module Resizor
59
59
  end
60
60
  end
61
61
  end
62
-
62
+
63
63
  class Response
64
64
  attr_accessor :code, :body, :format
65
65
  def initialize(_code, _body, _format = 'json')
@@ -71,7 +71,7 @@ module Resizor
71
71
  else
72
72
  JSON.parse(_body)
73
73
  end
74
- else
74
+ else
75
75
  _body
76
76
  end
77
77
  end
@@ -1,3 +1,3 @@
1
1
  module Resizor
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/resizor.rb CHANGED
@@ -5,3 +5,15 @@ require 'resizor/connection'
5
5
  require 'resizor/asset'
6
6
  require 'resizor/version'
7
7
  require 'resizor/railtie'
8
+
9
+ if defined? ActionDispatch::Http::UploadedFile
10
+ module ActionDispatch
11
+ module Http
12
+ class UploadedFile
13
+ def path
14
+ @tempfile.path
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
data/resizor.gemspec CHANGED
@@ -14,8 +14,8 @@ Gem::Specification.new do |s|
14
14
  s.required_rubygems_version = ">= 1.3.6"
15
15
 
16
16
  s.add_development_dependency "bundler", ">= 1.0.0"
17
-
18
- s.add_dependency(%q<rest-client>, [">= 1.4"])
17
+
18
+ s.add_dependency(%q<rest-client>, ["~> 1.4.2"])
19
19
  s.add_dependency(%q<json>, [">= 1.2"])
20
20
 
21
21
  s.files = `git ls-files`.split("\n")
data/test/asset_test.rb CHANGED
@@ -4,13 +4,13 @@ class AssetTest < Test::Unit::TestCase
4
4
  context "A Asset" do
5
5
  setup do
6
6
  setup_resizor
7
- @asset = Resizor::Asset.new(:id => 10,
8
- :name => 'my_file.jpg',
9
- :mime_type => 'image/jpeg',
10
- :width => 200,
11
- :height => 300,
7
+ @asset = Resizor::Asset.new(:id => 10,
8
+ :name => 'my_file.jpg',
9
+ :mime_type => 'image/jpeg',
10
+ :width => 200,
11
+ :height => 300,
12
12
  :size => 123456)
13
- end
13
+ end
14
14
  should "have assigned attributes" do
15
15
  assert_equal @asset.id, 10
16
16
  assert_equal @asset.name, 'my_file.jpg'
@@ -19,40 +19,40 @@ class AssetTest < Test::Unit::TestCase
19
19
  assert_equal @asset.height, 300
20
20
  assert_equal @asset.size, 123456
21
21
  end
22
-
22
+
23
23
  should 'generate url for size c200x300' do
24
24
  assert_equal 'http://resizor.test:80/assets/10.jpg?size=c200x300&token=b8bb7c4c7c4fc1006c904f011f32f50f69730e5e', @asset.url(:size => 'c200x300')
25
25
  end
26
-
26
+
27
27
  should 'generate url for size c200x300 format png' do
28
28
  assert_equal 'http://resizor.test:80/assets/10.png?size=c200x300&token=0cf27070e89c44a40aee85decca2cd2d98af1dc2', @asset.url(:size => 'c200x300', :format => 'png')
29
29
  end
30
-
30
+
31
31
  should 'generate resize token for size c200x300 and format jpg' do
32
32
  assert_equal 'b8bb7c4c7c4fc1006c904f011f32f50f69730e5e', @asset.resize_token_for(:size => 'c200x300', :format => 'jpg')
33
33
  end
34
-
34
+
35
35
  context 'when saving to resizor' do
36
36
  setup do
37
37
  @jpg_file = Tempfile.new('image.jpg')
38
38
  @asset.path = @jpg_file.path
39
39
  end
40
40
  teardown { @jpg_file.unlink }
41
-
41
+
42
42
  context 'on success' do
43
- setup do
44
- stub_http_request(:post, "https://resizor.test:80/assets.json").
43
+ setup do
44
+ stub_http_request(:post, "https://resizor.test:443/assets.json").
45
45
  with { |request|
46
- request.body.include?("Content-Disposition: form-data; name=\"file\"; filename=\"image.jpg")
46
+ request.body.include?("Content-Disposition: form-data; name=\"file\"; filename=\"image.jpg")
47
47
  }.
48
- to_return(:status => 201,
48
+ to_return(:status => 201,
49
49
  :body => '{"asset": { "id":1, "name":"i", "extension":"jpg", "mime_type":"image/jpeg", "height":7, "width":8, "file_size":9, "created_at":"2010-10-23T13:07:25Z"}}')
50
50
  @asset.save_to_resizor
51
51
  end
52
-
52
+
53
53
  should 'make create call to Resizor on save with file' do
54
- assert_requested(:post, "https://resizor.test:80/assets.json",
55
- :times => 1) { |request|
54
+ assert_requested(:post, "https://resizor.test:443/assets.json",
55
+ :times => 1) { |request|
56
56
  request.body.include?("Content-Disposition: form-data; name=\"file\"; filename=\"image.jpg")
57
57
  }
58
58
  end
@@ -66,10 +66,10 @@ class AssetTest < Test::Unit::TestCase
66
66
  assert_equal 9, @asset.size
67
67
  end
68
68
  end
69
-
69
+
70
70
  context 'on failure' do
71
71
  should 'return false' do
72
- stub_http_request(:post, "https://resizor.test:80/assets.json").to_return(:status => 422)
72
+ stub_http_request(:post, "https://resizor.test:443/assets.json").to_return(:status => 422)
73
73
  assert !@asset.save_to_resizor
74
74
  end
75
75
  end
@@ -77,18 +77,18 @@ class AssetTest < Test::Unit::TestCase
77
77
 
78
78
  context 'when destroying an asset' do
79
79
  should 'make delete call to Resizor on destroy' do
80
- stub_http_request(:delete, "https://resizor.test:80/assets/10.json?api_key=test-api-key").to_return(:status => 200)
80
+ stub_http_request(:delete, "https://resizor.test:443/assets/10.json?api_key=test-api-key").to_return(:status => 200)
81
81
  @asset.destroy
82
- assert_requested :delete, "https://resizor.test:80/assets/10.json?api_key=test-api-key", :times => 1
82
+ assert_requested :delete, "https://resizor.test:443/assets/10.json?api_key=test-api-key", :times => 1
83
83
  end
84
84
 
85
85
  should 'return true on success' do
86
- stub_http_request(:delete, "https://resizor.test:80/assets/10.json?api_key=test-api-key").to_return(:status => 200)
86
+ stub_http_request(:delete, "https://resizor.test:443/assets/10.json?api_key=test-api-key").to_return(:status => 200)
87
87
  assert @asset.destroy
88
88
  end
89
89
 
90
90
  should 'return false on failure' do
91
- stub_http_request(:delete, "https://resizor.test:80/assets/10.json?api_key=test-api-key").to_return(:status => 404)
91
+ stub_http_request(:delete, "https://resizor.test:443/assets/10.json?api_key=test-api-key").to_return(:status => 404)
92
92
  assert !@asset.destroy
93
93
  end
94
94
  end
@@ -1,41 +1,41 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
2
 
3
3
  class IntegrationTest < Test::Unit::TestCase
4
-
4
+
5
5
  context 'Including Resizor in a Rails project' do
6
6
  setup do
7
7
  setup_resizor
8
8
  end
9
-
9
+
10
10
  should 'add has_resizor_asset to ActiveRecord::Base' do
11
11
  assert ActiveRecord::Base.methods.include?('has_resizor_asset')
12
12
  end
13
- end
14
-
13
+ end
14
+
15
15
  context 'A ActiveRecord model that has_resizor_asset' do
16
- setup do
17
- build_model
16
+ setup do
17
+ build_model
18
18
  @item = Item.new(:name => 'my test item')
19
19
  end
20
-
20
+
21
21
  context 'with a file attached' do
22
22
  setup do
23
23
  @image_fixture_path = File.join(File.dirname(__FILE__), 'fixtures', 'image.jpg')
24
24
  File.open(@image_fixture_path, 'w') {|f| f.write('JPEG data') }
25
25
  @file = File.new(@image_fixture_path, 'rb')
26
26
  @item.image = @file
27
- stub_http_request(:post, "https://resizor.test:80/assets.json").
27
+ stub_http_request(:post, "https://resizor.test:443/assets.json").
28
28
  with { |request| request.body.include?("Content-Disposition: form-data; name=\"file\"; filename=\"image.jpg") }.
29
29
  to_return(:status => 201, :body => '{"asset": { "id":1, "name":"i", "extension":"jpg", "mime_type":"image/jpeg", "height":7, "width":8, "file_size":9, "created_at":"2010-10-23T13:07:25Z"}}')
30
- stub_http_request(:delete, "https://resizor.test:80/assets/1.json?api_key=test-api-key").to_return(:status => 200)
30
+ stub_http_request(:delete, "https://resizor.test:443/assets/1.json?api_key=test-api-key").to_return(:status => 200)
31
31
  end
32
-
33
- teardown { File.unlink(@image_fixture_path) }
32
+
33
+ teardown { File.unlink(@image_fixture_path) if File.exists?(@image_fixture_path) }
34
34
 
35
35
  should 'save attached asset to Resizor on save' do
36
36
  assert @item.save
37
- assert_requested(:post, "https://resizor.test:80/assets.json", :times => 1) do |request|
38
- request.body.include?("Content-Disposition: form-data; name=\"file\"; filename=\"image.jpg")
37
+ assert_requested(:post, "https://resizor.test:443/assets.json", :times => 1) do |request|
38
+ request.body.include?("Content-Disposition: form-data; name=\"file\"; filename=\"image.jpg")
39
39
  end
40
40
  end
41
41
 
@@ -75,14 +75,14 @@ class IntegrationTest < Test::Unit::TestCase
75
75
  [:image_resizor_id, :image_name, :image_mime_type, :image_size, :image_width, :image_height].each do |_attr|
76
76
  assert_nil @item.send(_attr)
77
77
  end
78
- assert_requested :delete, "https://resizor.test:80/assets/1.json?api_key=test-api-key", :times => 1
78
+ assert_requested :delete, "https://resizor.test:443/assets/1.json?api_key=test-api-key", :times => 1
79
79
  end
80
-
80
+
81
81
  should 'delete resizor asset when model instance is deleted' do
82
82
  @item.save
83
83
  @item.reload
84
84
  assert @item.destroy
85
- assert_requested :delete, "https://resizor.test:80/assets/1.json?api_key=test-api-key", :times => 1
85
+ assert_requested :delete, "https://resizor.test:443/assets/1.json?api_key=test-api-key", :times => 1
86
86
  end
87
87
 
88
88
  end
data/test/resizor_test.rb CHANGED
@@ -20,54 +20,54 @@ class ResizorTest < Test::Unit::TestCase
20
20
  assert Resizor.use_ssl
21
21
  end
22
22
  should 'return a API https url when use_ssl is set to true' do
23
- assert_equal 'https://resizor.test:80', Resizor.api_url
23
+ assert_equal 'https://resizor.test:443', Resizor.api_url
24
24
  end
25
25
 
26
26
  should 'return a API http url when use_ssl is set to false' do
27
27
  Resizor.connection.use_ssl = false
28
28
  assert_equal 'http://resizor.test:80', Resizor.api_url
29
29
  end
30
-
30
+
31
31
  should 'make a GET request to Resizor server with API key' do
32
- stub_http_request(:get, "https://resizor.test:80/assets.json?api_key=test-api-key").to_return(:body => '{"a": "123"}')
32
+ stub_http_request(:get, "https://resizor.test:443/assets.json?api_key=test-api-key").to_return(:body => '{"a": "123"}')
33
33
  Resizor.get('/assets.json').tap do |r|
34
34
  assert_equal 200, r.code
35
35
  assert_equal Hash['a', '123'], r.body
36
36
  end
37
37
  end
38
38
 
39
- should 'make a POST request to Resizor server with API key' do
40
- stub_http_request(:post, "https://resizor.test:80/assets.json").with(:body => 'api_key=test-api-key').to_return(:body => '{"a": "123"}', :status => 201)
39
+ should 'make a POST request to Resizor server with API key' do
40
+ stub_http_request(:post, "https://resizor.test:443/assets.json").with(:body => 'api_key=test-api-key').to_return(:body => '{"a": "123"}', :status => 201)
41
41
  Resizor.post('/assets.json').tap do |r|
42
42
  assert_equal 201, r.code
43
43
  assert_equal Hash['a', '123'], r.body
44
44
  end
45
45
  end
46
46
 
47
- should 'make a DELETE request to Resizor server with API key' do
48
- stub_http_request(:delete, "https://resizor.test:80/assets/1.json?api_key=test-api-key")
47
+ should 'make a DELETE request to Resizor server with API key' do
48
+ stub_http_request(:delete, "https://resizor.test:443/assets/1.json?api_key=test-api-key")
49
49
  Resizor.delete('/assets/1.json').tap do |r|
50
50
  assert_equal 200, r.code
51
- assert_equal nil, r.body
51
+ assert_nil r.body
52
52
  end
53
53
  end
54
-
54
+
55
55
  should 'add params along with API key when generating GET URL' do
56
- stub_http_request(:get, "https://resizor.test:80/assets.json?api_key=test-api-key&id=1")
56
+ stub_http_request(:get, "https://resizor.test:443/assets.json?api_key=test-api-key&id=1")
57
57
  Resizor.get('/assets.json', :id => 1)
58
- assert_requested :get, "https://resizor.test:80/assets.json?api_key=test-api-key&id=1"
58
+ assert_requested :get, "https://resizor.test:443/assets.json?api_key=test-api-key&id=1"
59
59
  end
60
-
61
- should 'add params along with API key when generating POST URL' do
62
- stub_http_request(:post, "https://resizor.test:80/assets.json").with(:body => 'api_key=test-api-key&id=1')
60
+
61
+ should 'add params along with API key when generating POST URL' do
62
+ stub_http_request(:post, "https://resizor.test:443/assets.json").with(:body => 'api_key=test-api-key&id=1')
63
63
  Resizor.post('/assets.json', :id => 1)
64
- assert_requested :post, "https://resizor.test:80/assets.json"
64
+ assert_requested :post, "https://resizor.test:443/assets.json"
65
65
  end
66
-
66
+
67
67
  should 'add params along with API key when generating DELETE URL' do
68
- stub_http_request(:delete, "https://resizor.test:80/assets.json?api_key=test-api-key&id=1")
68
+ stub_http_request(:delete, "https://resizor.test:443/assets.json?api_key=test-api-key&id=1")
69
69
  Resizor.delete('/assets.json', :id => 1)
70
- assert_requested :delete, "https://resizor.test:80/assets.json?api_key=test-api-key&id=1"
70
+ assert_requested :delete, "https://resizor.test:443/assets.json?api_key=test-api-key&id=1"
71
71
  end
72
72
 
73
73
  end
data/test/test_helper.rb CHANGED
@@ -16,7 +16,21 @@ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
16
16
  ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new(File.dirname(__FILE__) + "/debug.log")
17
17
  ActiveRecord::Base.establish_connection(config['test'])
18
18
 
19
- ActiveRecord::Base.send(:include, Resizor)
19
+ if defined?(Rails::Railtie)
20
+ class Railtie < Rails::Railtie
21
+ initializer 'resizor.insert_into_active_record' do
22
+ ActiveSupport.on_load :active_record do
23
+ Resizor::Railtie.insert
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ class Railtie
30
+ def self.insert
31
+ ActiveRecord::Base.send(:include, Resizor)
32
+ end
33
+ end
20
34
 
21
35
  def build_model
22
36
  ActiveRecord::Base.connection.create_table :items, :force => true do |t|
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resizor
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Winston Design
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-22 00:00:00 +01:00
18
+ date: 2011-02-17 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -40,13 +40,14 @@ dependencies:
40
40
  requirement: &id002 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ">="
43
+ - - ~>
44
44
  - !ruby/object:Gem::Version
45
- hash: 7
45
+ hash: 3
46
46
  segments:
47
47
  - 1
48
48
  - 4
49
- version: "1.4"
49
+ - 2
50
+ version: 1.4.2
50
51
  type: :runtime
51
52
  version_requirements: *id002
52
53
  - !ruby/object:Gem::Dependency