imager 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef11a3f46f143082a1a0f76416edf5fdb8a17d72
4
- data.tar.gz: 540cb3e2fa09528f711339ed4ba55dec8e6ba9a7
3
+ metadata.gz: d30bc7d9057f78efe90d633324e31852c4c5ca60
4
+ data.tar.gz: e7dbe1bf69a88703cde2c1e672c396543ba48ba7
5
5
  SHA512:
6
- metadata.gz: fec4251a3c3f023ed8a5eb1c48396b293843025c7175680807b9d3d9c0b1acb885d2e2c450ae95839652bf565277d46fff3d9b5d09cec569899360f7ef8ceb10
7
- data.tar.gz: aa98a48c0d0d5d328b057dd3fe34bbdd48b1b320f42499b05e64750b6716562a0bc1ebba6fdd5c5a343437daf9a6916e3779c7693731bdb3bd6343ab678d0173
6
+ metadata.gz: e2d3700bf32ccbc0d3e930b8b9472f586b8a909652eff5d5f3f5dd327faf335dcc87bf47243facd0f783595da51210af4b3437da777c03b2ac7aee18ba88c06b
7
+ data.tar.gz: 2f5a859eb4cb534df31357ddf22510f8e8001f5d6735ef7010bb882851359224862843a731d14c3a02c027971054bc04152c31a25219625a46fff92de4e1f976
data/README.md CHANGED
@@ -17,7 +17,7 @@ A lot of the features are not implemented yet (except for the server)
17
17
 
18
18
  Add this line to your application's Gemfile:
19
19
 
20
- gem 'Imager'
20
+ gem 'imager'
21
21
 
22
22
  And then execute:
23
23
 
@@ -25,7 +25,7 @@ And then execute:
25
25
 
26
26
  Or install it yourself as:
27
27
 
28
- $ gem install Imager
28
+ $ gem install imager
29
29
 
30
30
  ## Usage
31
31
 
@@ -40,41 +40,50 @@ Or install it yourself as:
40
40
  This is for post and delete authentication (manager).
41
41
 
42
42
  ### Sending the images
43
+
43
44
  Imager::ServerInterface.post("Collection", "Album", "test/image.png", small: { width: 100 })
44
45
 
45
46
  ### Removing the images
47
+
46
48
  Imager::ServerInterface.delete("Collection", "Album", "image")
47
49
 
48
50
  ### Using the images
51
+
49
52
  Imager::LinkHelper.link_for("Collection", "Album", "image", :small)
53
+
50
54
  Will return:
51
55
  "http://files.myserver.com/images/collection/album/image/small.jpg"
52
- Since the server ALWAYS save the images as jpg.
56
+ Since the server ALWAYS save the images as jpg.
53
57
 
54
58
  You can use Collection as model name(product) and album as id(1) and get the result:
59
+
55
60
  "http://files.myserver.com/images/product/1/image/small.jpg"
56
61
  Just save as:
57
- Imager::ServerInterface.post("product", "1", "test/image.png", small: { width: 100 })
62
+ `Imager::ServerInterface.post("product", "1", "test/image.png", small: { width: 100 })`
58
63
 
59
64
  ## Notes about saving and sizes
60
65
  Saving first as `"product", "1", "test/image.png", small: { width: 100 })` and after `"product", "1", "test/otherimage.png", small: { width: 90 })` don't changes the size of image. Beware!
61
66
 
62
67
  ### Sizes Explain:
63
68
  The server accepts the following combinations:
69
+
64
70
  ```
65
71
  YourSizeName: { width: 100 } # Will resize (maintein main aspect) the image for 100px of width
66
72
  YourSizeName: { height: 100 } # Will resize (maintein main aspect) the image for 100px of height
67
73
  YourSizeName: { width: 100, height: 150 } # Will resize to fit in 100x150 px
68
- YourSizeName: { original: true } # Will save the original size. Don't worry. The server compress to 50% of quality.
74
+ YourSizeName: { original: :original } # Will save the original size. Don't worry. The server compress to 50% of quality.
69
75
  ```
76
+
70
77
  You can have many sizes when posting a image:
78
+
71
79
  ```
72
80
  sizes = [
73
81
  small: { width: 100 }
74
82
  gallery: { height: 300 }
75
83
  mini-home: { width: 50, height: 50 }
76
- original: true
84
+ original: :original
77
85
  ]
78
86
  ```
87
+
79
88
  ### Compression
80
- The images always are compressed to 70%. Except for the original size (50%).
89
+ The images always are compressed to 70%. Except for the original size (50%).
data/Rakefile CHANGED
@@ -1,9 +1 @@
1
- require "bundler/gem_tasks"
2
- require 'rake/testtask'
3
-
4
- Rake::TestTask.new do |t|
5
- t.libs << 'test'
6
- end
7
-
8
- desc "Run tests"
9
- task :default => :test
1
+ require "bundler/gem_tasks"
data/imager.gemspec CHANGED
@@ -20,5 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
- spec.add_dependency "httmultiparty"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_dependency "httmultiparty", "~> 0.3.10"
25
+ spec.add_dependency "json"
24
26
  end
@@ -0,0 +1,4 @@
1
+ module Imager
2
+ class ImagerError < ArgumentError
3
+ end
4
+ end
@@ -2,7 +2,7 @@ module Imager
2
2
  class LinkHelper
3
3
  def self.link_for(collection, album, image, size)
4
4
  size = size.to_s
5
- "#{Imager.base_uri}#{Imager.collection_path}/#{collection}/#{album}/#{image}/#{size}.jpg"
5
+ "#{Imager.base_uri}/#{Imager.collection_path}/#{collection}/#{album}/#{image}/#{size}.jpg"
6
6
  end
7
7
  end
8
8
  end
@@ -1,5 +1,6 @@
1
1
  require 'openssl'
2
2
  require 'cgi'
3
+ require 'json'
3
4
 
4
5
  module Imager
5
6
  class ServerInterface
@@ -23,7 +24,7 @@ module Imager
23
24
  query[:file_id] = file_id
24
25
  query[:auth] = auth_token(query)
25
26
 
26
- return parse(client.post('/delete.php', { query: query }))
27
+ return parse(client.post('/delete.php', { query: query }), true)
27
28
  end
28
29
 
29
30
  def self.client
@@ -35,22 +36,43 @@ module Imager
35
36
 
36
37
  private
37
38
 
38
- def self.parse(response)
39
+ def self.parse(response, is_delete = false)
39
40
  case response.code
40
- when 200..299
41
+ when 204
41
42
  return true
43
+ when 200..299
44
+ parsed = begin
45
+ !!JSON.parse(response.body)
46
+ rescue
47
+ false
48
+ end
49
+
50
+ return true if parsed
51
+ # Something is wrong with the server
52
+ raise ArgumentError, "The server send an invalid response.", caller
42
53
  when 400
43
- raise ArgumentError, response.body, caller
54
+ raise ImagerError, response.body, caller
55
+ when 404
56
+ # We are deleting something that doesn't exist
57
+ if (is_delete && response.body == "Cannot find the file.")
58
+ raise ImagerError, response.body, caller
59
+ else
60
+ raise ArgumentError, "The server return an unexpected 404.", caller
61
+ end
44
62
  when 401
45
- raise Error, "Authentication failed."
63
+ # Authentication with the server failed
64
+ raise ArgumentError, "Authentication failed: " + response.body, caller
46
65
  else
47
- raise Error, "The server returned an error."
66
+ raise ArgumentError, "The server returned an error: " + response.body, caller
48
67
  end
49
68
  end
50
69
 
51
70
  def self.auth_token(query, file=nil)
52
71
  query_hash = query.clone
53
72
  if file
73
+ if !File.file?(file)
74
+ raise ImagerError, "Invalid image file", caller
75
+ end
54
76
  query_hash[:file_md5] = Digest::MD5.file(file)
55
77
  query_hash[:file_sha1] = Digest::SHA1.file(file)
56
78
  query_hash[:file_name] = File.basename(file)
@@ -1,3 +1,3 @@
1
1
  module Imager
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/imager.rb CHANGED
@@ -2,7 +2,7 @@ require "imager/version"
2
2
  require "imager/server_client"
3
3
  require "imager/server_interface"
4
4
  require "imager/link_helper"
5
-
5
+ require "imager/imager_error"
6
6
  module Imager
7
7
 
8
8
  class << self
File without changes
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Imager::LinkHelper do
4
+ describe ".link_for" do
5
+ it "returns a correct link" do
6
+ Imager.configure do |c|
7
+ c.base_uri = "http://base_uri"
8
+ c.collection_path = "images"
9
+ end
10
+
11
+ correct_link = "http://base_uri/images/testcollection/1/image/small.jpg"
12
+ result = described_class.link_for("testcollection", "1", "image", :small)
13
+
14
+ result.should eq correct_link
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'debugger'
3
+ describe Imager::ServerInterface do
4
+ describe ".post" do
5
+
6
+ context "when params are valid" do
7
+ it "returns true" do
8
+ described_class.post("test", "1", "spec/image.jpg", small: { width: 100 }).should be_true
9
+ end
10
+ end
11
+
12
+ context "when some param is not correct" do
13
+ it "raises an error" do
14
+ expect {
15
+ described_class.post("", "", "", size: { width: 100 })
16
+ }.to raise_error Imager::ImagerError
17
+ end
18
+ end
19
+ end
20
+
21
+ describe ".delete" do
22
+ context "valid params and existing image" do
23
+
24
+ it "returns true" do
25
+ described_class.post("test", "1", "spec/image.jpg", small: { width: 100 })
26
+ described_class.delete("test", "1", "image").should be_true
27
+ end
28
+ end
29
+
30
+ context "some invalid param" do
31
+ it "raises an error" do
32
+ expect {
33
+ described_class.delete("test", "1", "non_existing_image")
34
+ }.to raise_error Imager::ImagerError
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Imager do
4
+ describe ".configure" do
5
+
6
+ it "yields" do
7
+ described_class.configure do |c|
8
+ c.should be described_class
9
+ end
10
+ end
11
+
12
+ it "sets server_uri" do
13
+ described_class.configure do |c|
14
+ c.base_uri = "http://baseuri.bla/"
15
+ end
16
+
17
+ described_class.base_uri.should eq "http://baseuri.bla/"
18
+ end
19
+
20
+ it "sets manager_path" do
21
+ described_class.configure do |c|
22
+ c.manager_path = "manager_path"
23
+ end
24
+
25
+ described_class.manager_path.should eq "manager_path"
26
+ end
27
+
28
+ it "sets collection_path" do
29
+ described_class.configure do |c|
30
+ c.collection_path = "your_collections_path"
31
+ end
32
+
33
+ described_class.collection_path.should eq "your_collections_path"
34
+ end
35
+
36
+ it "sets auth_code" do
37
+ described_class.configure do |c|
38
+ c.auth_code = "your_auth_code"
39
+ end
40
+
41
+ described_class.auth_code.should eq "your_auth_code"
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,12 @@
1
+ require 'imager'
2
+
3
+ RSpec.configure do |c|
4
+ c.before do
5
+ Imager.configure do |c|
6
+ c.base_uri = "http://dev.local/imagerserver"
7
+ c.auth_code = "f8cecac2459d83eb9c8c703663e349ddbeb82a76230c7ca5ffc8434cfaab"
8
+ c.collection_path = "images"
9
+ c.manager_path = "manager"
10
+ end
11
+ end
12
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guilherme Otranto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-01 00:00:00.000000000 Z
11
+ date: 2013-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,8 +38,36 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: httmultiparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.3.10
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.3.10
69
+ - !ruby/object:Gem::Dependency
70
+ name: json
43
71
  requirement: !ruby/object:Gem::Requirement
44
72
  requirements:
45
73
  - - '>='
@@ -66,12 +94,16 @@ files:
66
94
  - Rakefile
67
95
  - imager.gemspec
68
96
  - lib/imager.rb
97
+ - lib/imager/imager_error.rb
69
98
  - lib/imager/link_helper.rb
70
99
  - lib/imager/server_client.rb
71
100
  - lib/imager/server_interface.rb
72
101
  - lib/imager/version.rb
73
- - test/image.jpg
74
- - test/test_imager.rb
102
+ - spec/image.jpg
103
+ - spec/imager/link_helper_spec.rb
104
+ - spec/imager/server_interface_spec.rb
105
+ - spec/imager_spec.rb
106
+ - spec/spec_helper.rb
75
107
  homepage: https://github.com/guilherme-otran/Imager
76
108
  licenses:
77
109
  - MIT
@@ -97,5 +129,8 @@ signing_key:
97
129
  specification_version: 4
98
130
  summary: Imager Client API
99
131
  test_files:
100
- - test/image.jpg
101
- - test/test_imager.rb
132
+ - spec/image.jpg
133
+ - spec/imager/link_helper_spec.rb
134
+ - spec/imager/server_interface_spec.rb
135
+ - spec/imager_spec.rb
136
+ - spec/spec_helper.rb
data/test/test_imager.rb DELETED
@@ -1,118 +0,0 @@
1
- require 'test/unit'
2
- require 'imager'
3
-
4
-
5
- class ImagerTest < Test::Unit::TestCase
6
- def your_base_uri; "http://dev.local/imagerserver/"; end;
7
- def your_manager_path; "manager"; end;
8
- def your_collections_path; "images"; end;
9
- def your_auth_code; "f8cecac2459d83eb9c8c703663e349ddbeb82a76230c7ca5ffc8434cfaab"; end;
10
-
11
- def test_configure
12
- Imager.configure do |c|
13
- assert_equal c, Imager
14
- end
15
- end
16
-
17
- def test_set_server_uri
18
- Imager.configure do |c|
19
- c.base_uri = your_base_uri
20
- end
21
-
22
- assert_equal your_base_uri, Imager.base_uri
23
- end
24
-
25
- def test_set_manager_path
26
- Imager.configure do |c|
27
- c.manager_path = your_manager_path
28
- end
29
-
30
- assert_equal your_manager_path, Imager.manager_path
31
- end
32
-
33
- def test_set_collection_path
34
- Imager.configure do |c|
35
- c.collection_path = your_collections_path
36
- end
37
-
38
- assert_equal your_collections_path, Imager.collection_path
39
- end
40
-
41
- def test_set_auth_code
42
- Imager.configure do |c|
43
- c.auth_code = your_auth_code
44
- end
45
-
46
- assert_equal your_auth_code, Imager.auth_code
47
- end
48
-
49
- def test_valid_post_image
50
- Imager.configure do |c|
51
- c.base_uri = your_base_uri
52
- c.auth_code = your_auth_code
53
- c.collection_path = your_collections_path
54
- c.manager_path = your_manager_path
55
- end
56
-
57
- response = Imager::ServerInterface.post("testcollection", "1", "test/image.jpg", small: { width: 100 })
58
- assert_equal true, response
59
- end
60
-
61
- def test_invalid_post_image
62
- response = true
63
- begin
64
- Imager.configure do |c|
65
- c.base_uri = your_base_uri
66
- c.auth_code = your_auth_code
67
- c.collection_path = your_collections_path
68
- c.manager_path = your_manager_path
69
- end
70
- Imager::ServerInterface.post("", "", "test/image.jpg", small: { width: 100 })
71
- rescue
72
- response = false
73
- end
74
- assert_equal false, response
75
- end
76
-
77
- def test_valid_delete_image
78
- Imager.configure do |c|
79
- c.base_uri = your_base_uri
80
- c.auth_code = your_auth_code
81
- c.collection_path = your_collections_path
82
- c.manager_path = your_manager_path
83
- end
84
-
85
- response = Imager::ServerInterface.delete("testcollection", "1", "image")
86
- assert_equal true, response
87
- end
88
-
89
- def test_invalid_delete_image
90
- response = true
91
- begin
92
- Imager.configure do |c|
93
- c.base_uri = your_base_uri
94
- c.auth_code = your_auth_code
95
- c.collection_path = your_collections_path
96
- c.manager_path = your_manager_path
97
- end
98
- Imager::ServerInterface.delete("testcollection", "1", "non_existing_image")
99
- rescue
100
- response = false
101
- end
102
- assert_equal false, response
103
- end
104
-
105
- def test_link_helper
106
- Imager.configure do |c|
107
- c.base_uri = your_base_uri
108
- c.auth_code = your_auth_code
109
- c.collection_path = your_collections_path
110
- c.manager_path = your_manager_path
111
- end
112
-
113
- result = Imager::LinkHelper.link_for("testcollection", "1", "image", :small)
114
- should_be = "#{your_base_uri}#{your_collections_path}/testcollection/1/image/small.jpg"
115
-
116
- assert_equal should_be, result
117
- end
118
- end