ruby_imaginary 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8383208cd621d1d6595a360286fdd417683f5b13f641ce5e423df7b495b5976d
4
+ data.tar.gz: 50d684c30c146702a9368dc1607eb296ca0c2d128ca218421b2fc7f924e1152e
5
+ SHA512:
6
+ metadata.gz: 1eabcf57165061ec8266e71ad9e272d545cf69ad7408a6dcfd7827eeee945789013e1375ae88079bb0e36efb1dc4816bef1eb48a77dac9da42262200dec94067
7
+ data.tar.gz: 3a13cf8530e59b527bc54e9e67386300b6c7780f040cee38f9a8f062ec3424102b2fcf0bf5e2fcdbbe3593d03db47825d24318b2edd1a816c39c4fb6826c8355
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Imaginary Ruby
2
+ Minimalist ruby gem which is programmatic stream capable interface for [imaginary](https://github.com/h2non/imaginary) server.
3
+
4
+ Supports multiple image operations such as resize, crop, zoom, watermark, rotate... and both local and remote URL based image source processing.
5
+
6
+ ## Installation
7
+
8
+ `gem install ruby_imaginary`
9
+
10
+ or add the line below to your Gemfile
11
+
12
+ `gem 'ruby_imaginary'`
13
+
14
+ ## Configurations
15
+
16
+ You need to set the imaginary server address like this.
17
+ ```
18
+ Imaginary.configure do |config|
19
+ # You could set it directly or getting it from ENV variables
20
+ config.server = 'http://localhost:9000/'
21
+ end
22
+ ```
23
+
24
+ Also for rails application you could create an initializer at `config/initializers/imaginary.rb`
25
+
26
+ ## Operations
27
+
28
+ - Resize
29
+ - Enlarge
30
+ - Crop
31
+ - SmartCrop (based on libvips built-in algorithm)
32
+ - Rotate (with auto-rotate based on EXIF orientation)
33
+ - Flip (with auto-flip based on EXIF metadata)
34
+ - Flop
35
+ - Zoom
36
+ - Thumbnail
37
+ - Fit
38
+ - Blur
39
+ - Watermark (customizable by text)
40
+ - WatermarkImage
41
+
42
+ All the options are as same as the imaginary server documentation.
43
+
44
+ ## How to Use
45
+
46
+ Each of the operation above has a class under `Imaginary` module which accepts a hash for initializing.
47
+
48
+ ```
49
+ options = {
50
+ file: './images/1.jpg',
51
+ text: 'MyBrand',
52
+ }
53
+ operation = Imaginary::Watermark.new(options)
54
+
55
+ result = operation.result
56
+
57
+ File.write('result.jpg', result)
58
+ ```
59
+
60
+ Also before running the operation you could user the `valid?` method to make sure all the options are correct for this operation and avoid any failures.
61
+
62
+ ```
63
+ operation.valid?
64
+ ```
data/lib/imaginary.rb ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'imaginary/validators/key_validator.rb'
4
+ require_relative 'imaginary/validators/option.rb'
5
+ require_relative 'imaginary/validators/key_presence_validator.rb'
6
+
7
+ require_relative 'imaginary/configuration.rb'
8
+ require_relative 'imaginary/request_handler.rb'
9
+ require_relative 'imaginary/operation.rb'
10
+
11
+ require_relative 'imaginary/operations/blur.rb'
12
+ require_relative 'imaginary/operations/convert.rb'
13
+ require_relative 'imaginary/operations/crop.rb'
14
+ require_relative 'imaginary/operations/enlarge.rb'
15
+ require_relative 'imaginary/operations/extract.rb'
16
+ require_relative 'imaginary/operations/fit.rb'
17
+ require_relative 'imaginary/operations/flip.rb'
18
+ require_relative 'imaginary/operations/flop.rb'
19
+ require_relative 'imaginary/operations/resize.rb'
20
+ require_relative 'imaginary/operations/rotate.rb'
21
+ require_relative 'imaginary/operations/smart_crop.rb'
22
+ require_relative 'imaginary/operations/thumbnail.rb'
23
+ require_relative 'imaginary/operations/zoom.rb'
24
+ require_relative 'imaginary/operations/watermark.rb'
25
+ require_relative 'imaginary/operations/watermark_image.rb'
26
+
27
+ require_relative 'imaginary/hash.rb'
28
+
29
+ require_relative 'imaginary/exceptions/invalid_option.rb'
30
+
31
+ require 'rest-client'
32
+
33
+ module Imaginary
34
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module
4
+ module Imaginary
5
+ class << self
6
+ attr_accessor :configuration
7
+ end
8
+
9
+ def self.configure
10
+ self.configuration ||= Configuration.new
11
+ yield(configuration)
12
+ end
13
+
14
+ # configure class
15
+ class Configuration
16
+ attr_accessor :server
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Imaginary
4
+ class InvalidOption < StandardError
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Hash
4
+ def to_query_params
5
+ map do |k, v|
6
+ "#{k}=#{v}"
7
+ end.join('&')
8
+ end
9
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Imaginary
4
+ class Operation
5
+ attr_accessor :options
6
+ attr_accessor :file_input_address
7
+
8
+ include Imaginary::RequestHandler
9
+ extend Imaginary::Validators::KeyPresenceValidator
10
+
11
+ def file_input?
12
+ !file_input_address.nil?
13
+ end
14
+
15
+ def initialize(options)
16
+ self.file_input_address = options[:file]
17
+ options.delete(:file)
18
+ self.options = options
19
+ end
20
+
21
+ def valid?
22
+ self.class.validators.each do |v|
23
+ return false unless v.valid?(options)
24
+ end
25
+
26
+ true
27
+ end
28
+
29
+ def remote_action
30
+ self.class.name.split('::').last.downcase
31
+ end
32
+
33
+ def url
34
+ [
35
+ Imaginary.configuration.server,
36
+ remote_action,
37
+ '?',
38
+ options.to_query_params
39
+ ].join
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module extended for crop support
4
+ module Imaginary
5
+ class Convert < Operation
6
+ exists? :type
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module extended for crop support
4
+ module Imaginary
5
+ class Blur < Operation
6
+ exists? :sigma
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module extended for crop support
4
+ module Imaginary
5
+ class Crop < Operation
6
+ exists? :wdith, or: :height
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module extended for crop support
4
+ module Imaginary
5
+ class Enlarge < Operation
6
+ exists? :wdith, and: :height
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module extended for crop support
4
+ module Imaginary
5
+ class Extract < Operation
6
+ exists? :top, and: :areawidth
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module extended for crop support
4
+ module Imaginary
5
+ class Fit < Operation
6
+ exists? :width, and: :height
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module extended for crop support
4
+ module Imaginary
5
+ class Flip < Operation
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module extended for crop support
4
+ module Imaginary
5
+ class Flop < Operation
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module extended for crop support
4
+ module Imaginary
5
+ class Resize < Operation
6
+ exists? :width, or: :height
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module extended for crop support
4
+ module Imaginary
5
+ class Rotate < Operation
6
+ exists? :rotate
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module extended for crop support
4
+ module Imaginary
5
+ class SmartCrop < Operation
6
+ exists? :width, or: :height
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module extended for crop support
4
+ module Imaginary
5
+ class Thumbnail < Operation
6
+ exists? :width, and: :height
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module extended for crop support
4
+ module Imaginary
5
+ class Watermark < Operation
6
+ exists? :text
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module extended for crop support
4
+ module Imaginary
5
+ class WatermarkImage < Operation
6
+ exists? :image
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Imaginary module extended for crop support
4
+ module Imaginary
5
+ class Zoom < Operation
6
+ exists? :factor
7
+ end
8
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Imaginary
4
+ module RequestHandler
5
+ def result
6
+ raise InvalidOption unless valid?
7
+ return execute_with_file if file_input?
8
+
9
+ execute_with_url
10
+ end
11
+
12
+ private
13
+
14
+ def execute_with_file
15
+ RestClient.post url, file: File.new(file_input_address, 'rb')
16
+ end
17
+
18
+ def execute_with_url
19
+ RestClient.get url
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Imaginary
4
+ module Validators
5
+ module KeyPresenceValidator
6
+ def exists?(key, options = {})
7
+ validator_options = []
8
+ options.keys.each do |type|
9
+ validator_options << Option.new(type, options[type])
10
+ end
11
+ validators << KeyValidator.new(key, validator_options)
12
+ end
13
+
14
+ def validators
15
+ @validators ||= []
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Imaginary
4
+ module Validators
5
+ class KeyValidator
6
+ attr_accessor :options
7
+ attr_accessor :key
8
+
9
+ def initialize(key, options)
10
+ self.key = key
11
+ self.options = options
12
+ end
13
+
14
+ def valid?(object)
15
+ validation_resut = object.include?(key)
16
+ options.each do |option|
17
+ validation_resut = option.append(validation_resut, object)
18
+ end
19
+
20
+ validation_resut
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Imaginary
4
+ module Validators
5
+ class Option
6
+ attr_accessor :type
7
+ attr_accessor :key
8
+
9
+ def initialize(type, key)
10
+ raise 'Invalid Validator Option Type' unless %i[and or].include? type
11
+
12
+ self.type = type
13
+ self.key = key
14
+ end
15
+
16
+ def append(result, object)
17
+ return (result && object.include?(key)) if type == :and
18
+
19
+ result || object.include?(key)
20
+ end
21
+ end
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_imaginary
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Amirhosein Zolfaghari
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.2
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.2
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.2
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.2
33
+ description: Ruby client for imaginary service
34
+ email: amirhosein.zlf@gmail.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files:
38
+ - README.md
39
+ files:
40
+ - README.md
41
+ - lib/imaginary.rb
42
+ - lib/imaginary/configuration.rb
43
+ - lib/imaginary/exceptions/invalid_option.rb
44
+ - lib/imaginary/hash.rb
45
+ - lib/imaginary/operation.rb
46
+ - lib/imaginary/operations/blur.rb
47
+ - lib/imaginary/operations/convert.rb
48
+ - lib/imaginary/operations/crop.rb
49
+ - lib/imaginary/operations/enlarge.rb
50
+ - lib/imaginary/operations/extract.rb
51
+ - lib/imaginary/operations/fit.rb
52
+ - lib/imaginary/operations/flip.rb
53
+ - lib/imaginary/operations/flop.rb
54
+ - lib/imaginary/operations/resize.rb
55
+ - lib/imaginary/operations/rotate.rb
56
+ - lib/imaginary/operations/smart_crop.rb
57
+ - lib/imaginary/operations/thumbnail.rb
58
+ - lib/imaginary/operations/watermark.rb
59
+ - lib/imaginary/operations/watermark_image.rb
60
+ - lib/imaginary/operations/zoom.rb
61
+ - lib/imaginary/request_handler.rb
62
+ - lib/imaginary/validators/key_presence_validator.rb
63
+ - lib/imaginary/validators/key_validator.rb
64
+ - lib/imaginary/validators/option.rb
65
+ homepage: https://gitlab.com/amirhosein.zlf/ruby_imaginary
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '2.4'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubygems_version: 3.0.3
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Ruby client for imaginary service
88
+ test_files: []