imgix 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -1
- data/Gemfile +1 -0
- data/Readme.markdown +24 -0
- data/imgix.gemspec +2 -2
- data/lib/imgix.rb +1 -0
- data/lib/imgix/client.rb +44 -6
- data/lib/imgix/version.rb +1 -1
- data/test/units/domains_test.rb +53 -0
- data/test/units/path_test.rb +6 -2
- metadata +13 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57ebb7ae8df29e8673a3c0988392313412408f0e
|
4
|
+
data.tar.gz: 1e465ab4bc2c1e11896bf734c7ab33b9f4bbadf2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6755c1578a6677abb8bcc48c06c8886dec7202a54997a2618052bdddc96f7141a3d90b5c0707415c12df022cd7e7f65704c921db9e20ddd3cd90e0c23236b04
|
7
|
+
data.tar.gz: d131017e65866fb3cee2cf1adf012be943902b779eeffb147bacc46f26ffa0ced93c0e6c7928ada2a34c2281900a1e3a929b8fb299063e5adc01ab81062bd168
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/Readme.markdown
CHANGED
@@ -46,6 +46,30 @@ path.defaults.width(300).to_url # Resets parameters
|
|
46
46
|
path.rect(x: 0, y: 50, width: 200, height: 300).to_url # Rect helper
|
47
47
|
```
|
48
48
|
|
49
|
+
# Domain Sharded URLs
|
50
|
+
Domain sharding enables you to spread image requests across multiple domains. This allows you to bypass the requests-per-host limits of browsers. We recommend 2-3 domain shards maximum if you are going to use domain sharding.
|
51
|
+
|
52
|
+
In order to use domain sharding, you need to add multiple domains to your source. You then provide a list of these domains to a builder.
|
53
|
+
|
54
|
+
|
55
|
+
``` ruby
|
56
|
+
client = Imgix::Client.new(:hosts => ['your-subdomain-1.imgix.net', 'your-subdomain-2.imgix.net'])
|
57
|
+
```
|
58
|
+
By default, shards are calculated using a checksum so that the image path always resolves to the same domain. This improves caching in the browser. However, you can also specify cycle that simply cycles through the domains as you request them.
|
59
|
+
|
60
|
+
|
61
|
+
``` ruby
|
62
|
+
client = Imgix::Client.new(:hosts => ['your-subdomain-1.imgix.net', 'your-subdomain-2.imgix.net'], :shard_strategy => :cycle))
|
63
|
+
```
|
64
|
+
# Multiple Parameters
|
65
|
+
When the imgix api requires multiple parameters you have to use the method rather than an accessor.
|
66
|
+
For example to use the [noise reduction](http://www.imgix.com/docs/urlapi/enhance#nr-nrs) options
|
67
|
+
|
68
|
+
``` ruby
|
69
|
+
path.noise_reduction(50,50)
|
70
|
+
```
|
71
|
+
|
72
|
+
|
49
73
|
## Supported Ruby Versions
|
50
74
|
|
51
75
|
Imgix is tested under 1.9.2, 1.9.3, 2.0.0, JRuby 1.7.2 (1.9 mode), and Rubinius 2.0.0 (1.9 mode).
|
data/imgix.gemspec
CHANGED
@@ -6,8 +6,8 @@ require 'imgix/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = 'imgix'
|
8
8
|
spec.version = Imgix::VERSION
|
9
|
-
spec.authors = ['Sam Soffes', 'Ryan LeFevre']
|
10
|
-
spec.email = ['sam@soff.es', 'ryan@layervault.com']
|
9
|
+
spec.authors = ['Sam Soffes', 'Ryan LeFevre', 'Antony Denyer']
|
10
|
+
spec.email = ['sam@soff.es', 'ryan@layervault.com', 'email@antonydenyer.co.uk']
|
11
11
|
spec.description = 'Easily sign imgix URLs.'
|
12
12
|
spec.summary = 'Unofficial Ruby Gem for easily signing imgix URLs.'
|
13
13
|
spec.homepage = 'https://github.com/soffes/imgix-rb'
|
data/lib/imgix.rb
CHANGED
data/lib/imgix/client.rb
CHANGED
@@ -1,27 +1,65 @@
|
|
1
1
|
require 'digest'
|
2
2
|
require 'addressable/uri'
|
3
|
+
require 'zlib'
|
3
4
|
|
4
5
|
module Imgix
|
5
6
|
class Client
|
7
|
+
DEFAULTS = { secure: false, shard_strategy: :crc }
|
8
|
+
|
6
9
|
def initialize(options = {})
|
7
|
-
|
10
|
+
options = DEFAULTS.merge(options)
|
11
|
+
|
12
|
+
@hosts = Array(options[:host]) + Array(options[:hosts]) and validate_hosts!
|
8
13
|
@token = options[:token]
|
9
|
-
@secure = options[:secure]
|
14
|
+
@secure = options[:secure]
|
15
|
+
@shard_strategy = options[:shard_strategy] and validate_strategy!
|
10
16
|
end
|
11
17
|
|
12
18
|
def path(path)
|
13
|
-
Path.new(prefix, @token, path)
|
19
|
+
Path.new(prefix(path), @token, path)
|
14
20
|
end
|
15
21
|
|
16
|
-
def prefix
|
17
|
-
"#{@secure ? 'https' : 'http'}://#{
|
22
|
+
def prefix(path)
|
23
|
+
"#{@secure ? 'https' : 'http'}://#{get_host(path)}"
|
18
24
|
end
|
19
25
|
|
20
26
|
def sign_path(path)
|
21
27
|
uri = Addressable::URI.parse(path)
|
22
28
|
query = (uri.query || '')
|
23
29
|
signature = Digest::MD5.hexdigest(@token + uri.path + '?' + query)
|
24
|
-
"#{@secure ? 'https' : 'http'}://#{
|
30
|
+
"#{@secure ? 'https' : 'http'}://#{get_host(path)}#{uri.path}?#{query}&s=#{signature}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_host(path)
|
34
|
+
host = host_for_crc(path) if @shard_strategy == :crc
|
35
|
+
host = host_for_cycle if @shard_strategy == :cycle
|
36
|
+
host.gsub("http://","").gsub("https://","")
|
37
|
+
end
|
38
|
+
|
39
|
+
def host_for_crc(path)
|
40
|
+
crc = Zlib.crc32(path)
|
41
|
+
index = crc % @hosts.length - 1
|
42
|
+
@hosts[index]
|
43
|
+
end
|
44
|
+
|
45
|
+
def host_for_cycle
|
46
|
+
@hosts_cycle = @hosts.cycle unless @hosts_cycle
|
47
|
+
@hosts_cycle.next
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def validate_strategy!
|
53
|
+
unless STRATEGIES.include?(@shard_strategy)
|
54
|
+
raise ArgumentError.new("#{@shard_strategy} is not supported")
|
55
|
+
end
|
25
56
|
end
|
57
|
+
|
58
|
+
def validate_hosts!
|
59
|
+
unless @hosts.length > 0
|
60
|
+
raise ArgumentError, "The :host or :hosts option must be specified"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
26
64
|
end
|
27
65
|
end
|
data/lib/imgix/version.rb
CHANGED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DomainsTest < Imgix::Test
|
4
|
+
|
5
|
+
def test_deterministically_choosing_a_path
|
6
|
+
client = Imgix::Client.new(:hosts => [
|
7
|
+
"demos-1.imgix.net",
|
8
|
+
"demos-2.imgix.net",
|
9
|
+
"demos-3.imgix.net",
|
10
|
+
],
|
11
|
+
:token => '10adc394')
|
12
|
+
|
13
|
+
path = client.path('/bridge.png')
|
14
|
+
assert_equal 'http://demos-1.imgix.net/bridge.png?&s=13e68f249172e5f790344e85e7cdb14b', path.to_url
|
15
|
+
|
16
|
+
path = client.path('/flower.png')
|
17
|
+
assert_equal 'http://demos-2.imgix.net/flower.png?&s=7793669cc41d31fd21c26ede9709ef03', path.to_url
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_cycling_choosing_domain_in_order
|
21
|
+
client = Imgix::Client.new(:hosts => [
|
22
|
+
"demos-1.imgix.net",
|
23
|
+
"demos-2.imgix.net",
|
24
|
+
"demos-3.imgix.net",
|
25
|
+
],
|
26
|
+
:token => '10adc394',
|
27
|
+
:shard_strategy => :cycle)
|
28
|
+
|
29
|
+
path = client.path('/bridge.png')
|
30
|
+
assert_equal 'http://demos-1.imgix.net/bridge.png?&s=13e68f249172e5f790344e85e7cdb14b', path.to_url
|
31
|
+
|
32
|
+
path = client.path('/bridge.png')
|
33
|
+
assert_equal 'http://demos-2.imgix.net/bridge.png?&s=13e68f249172e5f790344e85e7cdb14b', path.to_url
|
34
|
+
|
35
|
+
path = client.path('/bridge.png')
|
36
|
+
assert_equal 'http://demos-3.imgix.net/bridge.png?&s=13e68f249172e5f790344e85e7cdb14b', path.to_url
|
37
|
+
|
38
|
+
path = client.path('/bridge.png')
|
39
|
+
assert_equal 'http://demos-1.imgix.net/bridge.png?&s=13e68f249172e5f790344e85e7cdb14b', path.to_url
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_strips_out_protocol
|
44
|
+
client = Imgix::Client.new(:host =>
|
45
|
+
"http://demos-1.imgix.net",
|
46
|
+
:token => '10adc394')
|
47
|
+
|
48
|
+
path = client.path('/bridge.png')
|
49
|
+
assert_equal 'http://demos-1.imgix.net/bridge.png?&s=13e68f249172e5f790344e85e7cdb14b', path.to_url
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/test/units/path_test.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class
|
3
|
+
class PathTest < Imgix::Test
|
4
4
|
def test_creating_a_path
|
5
5
|
path = client.path('/images/demo.png')
|
6
6
|
assert_equal 'http://demo.imgix.net/images/demo.png?&s=3c1d676d4daf28c044dd83e8548f834a', path.to_url
|
@@ -41,7 +41,11 @@ class UrlTest < Imgix::Test
|
|
41
41
|
assert_equal url, path.height(200).width(200).to_url
|
42
42
|
end
|
43
43
|
|
44
|
-
|
44
|
+
def test_host_is_required
|
45
|
+
assert_raises(ArgumentError) {Imgix::Client.new}
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
45
49
|
|
46
50
|
def client
|
47
51
|
@client ||= Imgix::Client.new(:host => 'demo.imgix.net', :token => '10adc394')
|
metadata
CHANGED
@@ -1,40 +1,42 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: imgix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Soffes
|
8
8
|
- Ryan LeFevre
|
9
|
+
- Antony Denyer
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date:
|
13
|
+
date: 2014-06-16 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: addressable
|
16
17
|
requirement: !ruby/object:Gem::Requirement
|
17
18
|
requirements:
|
18
|
-
- -
|
19
|
+
- - ">="
|
19
20
|
- !ruby/object:Gem::Version
|
20
21
|
version: '0'
|
21
22
|
type: :runtime
|
22
23
|
prerelease: false
|
23
24
|
version_requirements: !ruby/object:Gem::Requirement
|
24
25
|
requirements:
|
25
|
-
- -
|
26
|
+
- - ">="
|
26
27
|
- !ruby/object:Gem::Version
|
27
28
|
version: '0'
|
28
29
|
description: Easily sign imgix URLs.
|
29
30
|
email:
|
30
31
|
- sam@soff.es
|
31
32
|
- ryan@layervault.com
|
33
|
+
- email@antonydenyer.co.uk
|
32
34
|
executables: []
|
33
35
|
extensions: []
|
34
36
|
extra_rdoc_files: []
|
35
37
|
files:
|
36
|
-
- .gitignore
|
37
|
-
- .travis.yml
|
38
|
+
- ".gitignore"
|
39
|
+
- ".travis.yml"
|
38
40
|
- Contributing.markdown
|
39
41
|
- Gemfile
|
40
42
|
- LICENSE
|
@@ -47,6 +49,7 @@ files:
|
|
47
49
|
- lib/imgix/path.rb
|
48
50
|
- lib/imgix/version.rb
|
49
51
|
- test/test_helper.rb
|
52
|
+
- test/units/domains_test.rb
|
50
53
|
- test/units/path_test.rb
|
51
54
|
- test/units/url_test.rb
|
52
55
|
homepage: https://github.com/soffes/imgix-rb
|
@@ -59,21 +62,22 @@ require_paths:
|
|
59
62
|
- lib
|
60
63
|
required_ruby_version: !ruby/object:Gem::Requirement
|
61
64
|
requirements:
|
62
|
-
- -
|
65
|
+
- - ">="
|
63
66
|
- !ruby/object:Gem::Version
|
64
67
|
version: 1.9.0
|
65
68
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
69
|
requirements:
|
67
|
-
- -
|
70
|
+
- - ">="
|
68
71
|
- !ruby/object:Gem::Version
|
69
72
|
version: '0'
|
70
73
|
requirements: []
|
71
74
|
rubyforge_project:
|
72
|
-
rubygems_version: 2.
|
75
|
+
rubygems_version: 2.2.2
|
73
76
|
signing_key:
|
74
77
|
specification_version: 4
|
75
78
|
summary: Unofficial Ruby Gem for easily signing imgix URLs.
|
76
79
|
test_files:
|
77
80
|
- test/test_helper.rb
|
81
|
+
- test/units/domains_test.rb
|
78
82
|
- test/units/path_test.rb
|
79
83
|
- test/units/url_test.rb
|