imager 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 56ce5179ec4ee910013a72451b268ee141a7bb07
4
+ data.tar.gz: 62092eb90e5d9d6ab5e3675ec9aeabcc4de44889
5
+ SHA512:
6
+ metadata.gz: 26ebfba5221636a4bbb8ea45334d568a441fcd0e85f6aed87354ca8ab838cee7a7e4bcf6173b870101efac83627d141fe471fe226632611919ef5cab02a886e9
7
+ data.tar.gz: c584666fbacb84d2ba4e39c806bc29f35537cf6261749e0731e3f3859a5a09f71402d053c289b901de0ff3e4405d8e31f74b70cee6f8ecd0573a7b1b3b161eaf
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in Imager.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Imager
2
+
3
+ Use with [ImagerServer](https://github.com/guilherme-otran/ImagerServer), a storange and resizer server for images.
4
+ Check the test directory for know how the server works.
5
+ You can save your images in other domain running ImagerServer.
6
+
7
+ ## Please contribute!
8
+ A lot of the features are not implemented yet (except for the server)
9
+
10
+ 1. Fork it
11
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
12
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
13
+ 4. Push to the branch (`git push origin my-new-feature`)
14
+ 5. Create new Pull Request
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ gem 'Imager'
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install Imager
29
+
30
+ ## Usage
31
+
32
+ Imager.configure do |c|
33
+ c.base_uri = "http://files.myserver.com"
34
+ c.manager_path = "manager"
35
+ c.collections_path = "images"
36
+ c.auth_code = ""
37
+ end
38
+
39
+ c.auth_code is the `$YOUR_AUTH_CODE = '';` you setted in the [server] (https://github.com/guilherme-otran/ImagerServer).
40
+ This is for post and delete authentication (manager).
41
+
42
+ ### Sending the images
43
+ Imager::ServerInterface.post("Collection", "Album", File.new("test/test_image.png"), small: { width: 100})
44
+
45
+ ### Removing the images
46
+ Not yet.
47
+
48
+ ### Using the images
49
+ Imager::LinkHelper.link_for("Collection", "Album", :small)
50
+ Will return:
51
+ "http://files.myserver.com/images/collection/album/test_image/small.jpg"
52
+ Since the server ALWAYS save the images as jpg.
53
+
54
+ You can use Collection as model name(product) and album as id(1) and get the result:
55
+ "http://files.myserver.com/images/product/1/test_image/small.jpg"
56
+ Just save as:
57
+ Imager::ServerInterface.post("product", "1", File.new("test/test_image.png"), small: { width: 100})
58
+
59
+ ## Notes about saving and sizes
60
+ Saving first as `"product", "1", File.new("test/test_image.png"), small: { width: 100})` and after `"product", "1", File.new("test/other_image.png"), small: { width: 90})` don't changes the size of test_image. Beware!
61
+
62
+ ### Sizes Explain:
63
+ The server accepts the following combinations:
64
+ ```
65
+ YourSizeName: { width: 100 } # Will resize (maintein main aspect) the image for 100px of width
66
+ YourSizeName: { height: 100 } # Will resize (maintein main aspect) the image for 100px of height
67
+ 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.
69
+ ```
70
+ You can have many sizes when posting a image:
71
+ ```
72
+ sizes = [
73
+ small: { width: 100 }
74
+ gallery: { height: 300 }
75
+ mini-home: { width: 50, height: 50 }
76
+ original: true
77
+ ]
78
+ ```
79
+ ### Compression
80
+ The images always are compressed to 70%. Except for the original size (50%).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
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
data/imager.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'imager/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "imager"
8
+ spec.version = Imager::VERSION
9
+ spec.authors = ["Guilherme Otranto"]
10
+ spec.email = ["guilherme_otran@hotmail.com"]
11
+ spec.description = %q{A remote storange and resizer client for images.}
12
+ spec.summary = %q{Imager Client API}
13
+ spec.homepage = "https://github.com/guilherme-otran/Imager"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency "httmultiparty"
24
+ end
@@ -0,0 +1,5 @@
1
+ require 'httmultiparty'
2
+
3
+ class ServerClient
4
+ include HTTMultiParty
5
+ end
@@ -0,0 +1,74 @@
1
+ require 'debugger'
2
+ require 'openssl'
3
+ require 'cgi'
4
+
5
+ module Imager
6
+ class ServerInterface
7
+ def self.post(collection, album, file, sizes)
8
+ query = {}
9
+ query[:collection] = collection
10
+ query[:album] = album
11
+ query[:sizes] = sizes
12
+
13
+ auth = auth_token(query, file)
14
+ query[:file] = File.new(file)
15
+ query[:auth] = auth
16
+
17
+ return parse(client.post('/post.php', { query: query }))
18
+ end
19
+
20
+ def self.delete(collection, album, file_id)
21
+ query = {}
22
+ query[:collection] = collection
23
+ query[:album] = album
24
+ query[:file_id] = file_id
25
+ query[:auth] = auth_token(query)
26
+
27
+ return parse(client.post('/delete.php', { query: query }))
28
+ end
29
+
30
+ def self.client
31
+ unless ServerClient.base_uri
32
+ ServerClient.base_uri Imager.base_uri + '/' + Imager.manager_path
33
+ end
34
+ ServerClient
35
+ end
36
+
37
+ private
38
+
39
+ def self.parse(response)
40
+ case response.code
41
+ when 200..299
42
+ return true
43
+ when 400
44
+ raise ArgumentError, response.body, caller
45
+ when 401
46
+ raise Error, "Authentication failed."
47
+ else
48
+ raise Error, "The server returned an error."
49
+ end
50
+ end
51
+
52
+ def self.auth_token(query, file=nil)
53
+ query_hash = query.clone
54
+ if file
55
+ query_hash[:file_md5] = Digest::MD5.file(file)
56
+ query_hash[:file_sha1] = Digest::SHA1.file(file)
57
+ query_hash[:file_name] = File.basename(file)
58
+ end
59
+
60
+ query_hash = to_query(query_hash)
61
+ OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('md5'), Imager.auth_code, query_hash)
62
+ end
63
+
64
+ def self.to_query(hash, namespace=false)
65
+ if(hash.is_a? Hash)
66
+ hash.collect do |k, v|
67
+ to_query(v, namespace ? "#{namespace}[#{k}]" : k)
68
+ end.join '&'
69
+ else
70
+ CGI.escape(namespace.to_s) + "=" + CGI.escape(hash.to_s)
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module Imager
2
+ VERSION = "0.0.1"
3
+ end
data/lib/imager.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "imager/version"
2
+ require "imager/server_client"
3
+ require "imager/server_interface"
4
+
5
+ module Imager
6
+
7
+ class << self
8
+ attr_accessor :manager_path, :collection_path, :auth_code, :base_uri
9
+
10
+ def configure
11
+ yield self
12
+ end
13
+ end
14
+
15
+ end
data/test/image.jpg ADDED
Binary file
@@ -0,0 +1,104 @@
1
+ require 'test/unit'
2
+ require 'imager'
3
+
4
+
5
+ class ImagerTest < Test::Unit::TestCase
6
+ def your_base_uri; "http://localhost/projects/imager_server"; end;
7
+ def your_manager_path; "manager"; end;
8
+ def your_collections_path; "images"; end;
9
+ def your_auth_code; ""; 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("", "", File.new("test/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
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Guilherme Otranto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httmultiparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A remote storange and resizer client for images.
56
+ email:
57
+ - guilherme_otran@hotmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - imager.gemspec
68
+ - lib/imager.rb
69
+ - lib/imager/server_client.rb
70
+ - lib/imager/server_interface.rb
71
+ - lib/imager/version.rb
72
+ - test/image.jpg
73
+ - test/test_imager.rb
74
+ homepage: https://github.com/guilherme-otran/Imager
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.0.3
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Imager Client API
98
+ test_files:
99
+ - test/image.jpg
100
+ - test/test_imager.rb