picfit 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +83 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/picfit.rb +33 -0
- data/lib/picfit/client.rb +78 -0
- data/lib/picfit/configuration.rb +40 -0
- data/lib/picfit/version.rb +3 -0
- data/picfit.gemspec +25 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c442064d0c59868a391f192a9763a0a98eabb4fd
|
4
|
+
data.tar.gz: e9278df0364f51f76751aec27669299f1e900014
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e5c528a70d3e552fab005c32ba9d1fb97b4024712a52a4656671ee07abe34e878de296390442f5a10df4a010a3ddacb4328f131805d0f75ef899d4f0dc537504
|
7
|
+
data.tar.gz: 0029a7d6acaceacae49af28b5331c86cd5634e31d0f28089b08212acfa8b4833bda2dda6082dd551ec7b8768de505debaaf592583c165fe0286e25ea148b4567
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Edgar Gonzalez
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# Picfit [](https://travis-ci.org/edgar/picfit-ruby) [](http://badge.fury.io/rb/picfit)
|
2
|
+
|
3
|
+
A Ruby library for generating URLs with [picfit](https://github.com/thoas/picfit).
|
4
|
+
|
5
|
+
Inspired by [picfit-go](https://github.com/ulule/picfit-go)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'picfit'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install picfit
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
# Initialize Picfit with a block
|
27
|
+
Picfit.configure do |config|
|
28
|
+
config.base_url = "http://mypicfitserver.com",
|
29
|
+
config.secret_key = "abcdef"
|
30
|
+
end
|
31
|
+
|
32
|
+
# Or initialize using a Hash
|
33
|
+
Picfit.init(base_url: "http://mypicfitserver.com", secret_key: "abcdef")
|
34
|
+
|
35
|
+
# Get the image url
|
36
|
+
Picfit.image_url(
|
37
|
+
width: 100,
|
38
|
+
height: 100,
|
39
|
+
path: "path/to/file",
|
40
|
+
operation: :resize
|
41
|
+
)
|
42
|
+
=> "http://mypicfitserver.com/display?path=path%2Fto%2Ffile&op=resize&w=100&h=100&sig=f45506bf02ca3ba84eb34a2a066af7aa315a11c2"
|
43
|
+
|
44
|
+
# Get the image path (without base_url)
|
45
|
+
Picfit.image_path(
|
46
|
+
width: 100,
|
47
|
+
height: 100,
|
48
|
+
path: "path/to/file",
|
49
|
+
operation: :resize,
|
50
|
+
)
|
51
|
+
=> "display?path=path%2Fto%2Ffile&op=resize&w=100&h=100&sig=f45506bf02ca3ba84eb34a2a066af7aa315a11c2"
|
52
|
+
```
|
53
|
+
|
54
|
+
### General parameters
|
55
|
+
|
56
|
+
The `Hash` parameter supplied to both `image_path` and `image_url` methods support the following keys (as a `Symbol`:
|
57
|
+
|
58
|
+
- **:path** - The filepath to load the image using your source storage
|
59
|
+
- **:operation** - The operation to perform, see (Operations)[https://github.com/thoas/picfit#operations]
|
60
|
+
- **:secret_key** - Your secret key, see (Security)[https://github.com/thoas/picfit#security]
|
61
|
+
- **:method** - The method to perform, see (Methods)[https://github.com/thoas/picfit#methods]
|
62
|
+
- **:url** - The url of the image to generate (not required if ``path`` provided)
|
63
|
+
- **:width** - The desired width of the image, if ``0`` is provided the service will calculate the ratio with ``height``
|
64
|
+
- **:height** - The desired height of the image, if ``0`` is provided the service will calculate the ratio with ``width``
|
65
|
+
- **:upscale** - If your image is smaller than your desired dimensions, the service will upscale it by default to fit your dimensions, you can disable this behavior by providing ``0``
|
66
|
+
- **format** - The output format to save the image, by default the format will be the source format (a ``GIF`` image source will be saved as ``GIF``), see Formats_
|
67
|
+
- **quality** - The quality to save the image, by default the quality will be the highest possible, it will be only applied on ``JPEG`` format
|
68
|
+
- **degree** - The degree (``90``, ``180``, ``270``) to rotate the image
|
69
|
+
- **position** - The position to flip the image
|
70
|
+
|
71
|
+
## Development
|
72
|
+
|
73
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
74
|
+
|
75
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
76
|
+
|
77
|
+
## Contributing
|
78
|
+
|
79
|
+
1. Fork it ( https://github.com/[my-github-username]/picfit/fork )
|
80
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
81
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
82
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
83
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "picfit"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/picfit.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "picfit/version"
|
2
|
+
require "picfit/configuration"
|
3
|
+
require "picfit/client"
|
4
|
+
|
5
|
+
module Picfit
|
6
|
+
extend Configuration
|
7
|
+
|
8
|
+
def self.init(options={})
|
9
|
+
@client = Picfit::Client.new(options)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Get the common client
|
13
|
+
def self.client
|
14
|
+
@client ||= init
|
15
|
+
end
|
16
|
+
|
17
|
+
# Set the common client
|
18
|
+
def self.client=(client)
|
19
|
+
@client = client
|
20
|
+
end
|
21
|
+
|
22
|
+
# Delegate to Picfit::Client
|
23
|
+
def self.method_missing(method, *args, &block)
|
24
|
+
return super unless client.respond_to?(method)
|
25
|
+
client.send(method, *args, &block)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Delegate to Picfit::Client
|
29
|
+
def self.respond_to?(method)
|
30
|
+
return client.respond_to?(method) || super
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
|
3
|
+
module Picfit
|
4
|
+
class Client
|
5
|
+
attr_accessor *Configuration::VALID_OPTIONS_KEYS
|
6
|
+
|
7
|
+
PICFIT_PARAMS_MAP = {
|
8
|
+
path: :path,
|
9
|
+
operation: :op,
|
10
|
+
url: :url,
|
11
|
+
width: :w,
|
12
|
+
height: :h,
|
13
|
+
upscale: :upscale,
|
14
|
+
format: :fmt,
|
15
|
+
quality: :q,
|
16
|
+
degree: :deg,
|
17
|
+
position: :pos
|
18
|
+
}.freeze
|
19
|
+
|
20
|
+
def initialize(options={})
|
21
|
+
options = Picfit.options.merge(options)
|
22
|
+
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
23
|
+
send("#{key}=", options[key])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def config
|
28
|
+
conf = {}
|
29
|
+
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
30
|
+
conf[key] = send key
|
31
|
+
end
|
32
|
+
conf
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# Return a full image url
|
37
|
+
#
|
38
|
+
def image_url(options = {})
|
39
|
+
base_url = options[:base_url] || self.base_url || Configuration.DEFAULT_BASE_URL
|
40
|
+
base_url << "/" if base_url[-1,1] != "/"
|
41
|
+
base_url << image_path(options)
|
42
|
+
end
|
43
|
+
|
44
|
+
#
|
45
|
+
# Return the image path (not include the host)
|
46
|
+
#
|
47
|
+
def image_path(options = {})
|
48
|
+
path = (options[:method] || self.method).to_s
|
49
|
+
secret_key = options[:secret_key] || self.secret_key
|
50
|
+
|
51
|
+
# lets convert the options to picfit params
|
52
|
+
params = PICFIT_PARAMS_MAP.inject({}) do |result, (key, param_key)|
|
53
|
+
result[param_key] = options[key] if options[key]
|
54
|
+
result
|
55
|
+
end
|
56
|
+
# Create a query string from the params
|
57
|
+
query_string = params.map{|k,v| [CGI.escape(k.to_s), "=", CGI.escape(v.to_s)]}.map(&:join).join("&")
|
58
|
+
if secret_key
|
59
|
+
# lets use << instead of + or interpolation
|
60
|
+
query_string << "&sig="
|
61
|
+
query_string << sign_string(secret_key, query_string)
|
62
|
+
end
|
63
|
+
path << "?"
|
64
|
+
path << query_string
|
65
|
+
end
|
66
|
+
|
67
|
+
def sign_string(secret_key, query_string)
|
68
|
+
hmac = OpenSSL::HMAC.new(secret_key, sha_digest)
|
69
|
+
hmac << query_string
|
70
|
+
hmac.hexdigest
|
71
|
+
end
|
72
|
+
|
73
|
+
def sha_digest
|
74
|
+
@sha_digest ||= OpenSSL::Digest::SHA1.new
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Picfit
|
2
|
+
module Configuration
|
3
|
+
# An array of valid keys in the options hash
|
4
|
+
VALID_OPTIONS_KEYS = [
|
5
|
+
:base_url,
|
6
|
+
:method,
|
7
|
+
:secret_key,
|
8
|
+
].freeze
|
9
|
+
|
10
|
+
DEFAULT_METHOD = :display
|
11
|
+
DEFAULT_BASE_URL = ""
|
12
|
+
DEFAULT_SECRET_KEY = nil
|
13
|
+
|
14
|
+
attr_accessor *VALID_OPTIONS_KEYS
|
15
|
+
|
16
|
+
# When this module is extended, set all configuration options to their default values
|
17
|
+
def self.extended(base)
|
18
|
+
base.reset
|
19
|
+
end
|
20
|
+
|
21
|
+
# Convenience method to allow configuration options to be set in a block
|
22
|
+
def configure
|
23
|
+
yield self
|
24
|
+
end
|
25
|
+
|
26
|
+
# Create a hash of options and their values
|
27
|
+
def options
|
28
|
+
VALID_OPTIONS_KEYS.inject({}) do |option, key|
|
29
|
+
option.merge!(key => send(key))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Reset all configuration options to defaults
|
34
|
+
def reset
|
35
|
+
self.base_url = DEFAULT_BASE_URL
|
36
|
+
self.method = DEFAULT_METHOD
|
37
|
+
self.secret_key = DEFAULT_SECRET_KEY
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/picfit.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'picfit/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "picfit"
|
8
|
+
spec.version = Picfit::VERSION
|
9
|
+
spec.authors = ["Edgar Gonzalez"]
|
10
|
+
spec.email = ["edgargonzalez@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A Ruby library for generating URLs with picfit.}
|
13
|
+
spec.description = %q{A Ruby library for generating URLs with picfit.}
|
14
|
+
spec.homepage = "http://github.com/edgar/picfit-ruby"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: picfit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Edgar Gonzalez
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-17 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.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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'
|
55
|
+
description: A Ruby library for generating URLs with picfit.
|
56
|
+
email:
|
57
|
+
- edgargonzalez@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/picfit.rb
|
72
|
+
- lib/picfit/client.rb
|
73
|
+
- lib/picfit/configuration.rb
|
74
|
+
- lib/picfit/version.rb
|
75
|
+
- picfit.gemspec
|
76
|
+
homepage: http://github.com/edgar/picfit-ruby
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.4.7
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: A Ruby library for generating URLs with picfit.
|
100
|
+
test_files: []
|