libpixel 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b0f22ebc9734b9ef50dca3c743461b6df558e065
4
+ data.tar.gz: 4841c3f1f7a9dc7c6b55537be6b8bce1f18846b8
5
+ SHA512:
6
+ metadata.gz: 04fd860a31cdd64dea9391343b18c593469747984682e630a29d8e96975777dead7356e7e6fab77794586d215e213d37e0792b481eb1383949f653009f9441e0
7
+ data.tar.gz: fd81c111aa5f13e2acd13c6eab081e946b104e491e5d6243459776ae6036b9ba34de7466cddf361458270b5de0c14b19d0b7230bd544f00c1897117e352617c9
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ cache: bundler
3
+ sudo: false
4
+ rvm:
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - 2.1.6
8
+ - 2.2.2
9
+ - jruby
10
+ - rbx
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in libpixel.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 LibPixel Oy
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.
@@ -0,0 +1,61 @@
1
+ # LibPixel
2
+
3
+ Ruby library to generate and sign LibPixel URLs.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'libpixel'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install libpixel
20
+
21
+ ## Usage
22
+
23
+ Configure the LibPixel client:
24
+
25
+ ```ruby
26
+ LibPixel.setup do |config|
27
+ config.host = "test.libpx.com" # Your LibPixel domain. Required.
28
+ config.https = true # Generates HTTPS URLs. Optional. Default is false.
29
+ config.secret = "..." # Auth secret for your LibPixel account. Required for signing requests.
30
+ end
31
+ ```
32
+
33
+ ### Sign URLs
34
+
35
+ You can sign an existing URL using the `sign` function:
36
+
37
+ ```ruby
38
+ url = LibPixel.sign("http://test.libpx.com/images/1.jpg?width=400")
39
+ ```
40
+
41
+ ### Generate URLs
42
+
43
+ You can also generate and sign URLs at the same time with the `url` function:
44
+
45
+ ```ruby
46
+ url = LibPixel.url("/images/1.jpg", height: 400, blur: 20, saturation: -80)
47
+ ```
48
+
49
+ ### Multiple clients
50
+
51
+ It's also possible to have multiple instances of LibPixel clients (e.g. when dealing with multiple accounts):
52
+
53
+ ```ruby
54
+ client = LibPixel::Client.new(host: "test.libpx.com", https: true, secret: "...")
55
+ ```
56
+
57
+ You may then call the `#url` and `#sign` methods on the client object.
58
+
59
+ ## License
60
+
61
+ [MIT](LICENSE)
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ end
7
+
8
+ task :default => :test
9
+
@@ -0,0 +1,20 @@
1
+ require "libpixel/version"
2
+ require "libpixel/client"
3
+
4
+ module LibPixel
5
+
6
+ @@default_client = Client.new
7
+
8
+ def self.setup
9
+ yield @@default_client
10
+ end
11
+
12
+ def self.sign(uri)
13
+ @@default_client.sign(uri)
14
+ end
15
+
16
+ def self.url(path, options={})
17
+ @@default_client.url(path, options)
18
+ end
19
+
20
+ end
@@ -0,0 +1,49 @@
1
+ require "openssl"
2
+ require "uri"
3
+
4
+ module LibPixel
5
+ class Client
6
+ attr_accessor :host, :secret, :https
7
+
8
+ def initialize(options={})
9
+ options.each do |key, value|
10
+ send("#{key}=", value)
11
+ end
12
+ end
13
+
14
+ def sign(uri)
15
+ uri = URI.parse(uri) unless uri.kind_of?(URI::Generic)
16
+
17
+ query = uri.query
18
+ data = uri.path
19
+ data += "?#{query}" if query && query != ""
20
+
21
+ digest = OpenSSL::Digest.new("sha1")
22
+ signature = OpenSSL::HMAC.hexdigest(digest, secret, data)
23
+
24
+ query += "&" if query && query != ""
25
+ query = "#{query}signature=#{signature}"
26
+ uri.query = query
27
+
28
+ uri.to_s
29
+ end
30
+
31
+ def url(path, options={})
32
+ query = options.map { |k,v| "#{k}=#{URI.encode_www_form_component(v)}" }.join("&")
33
+
34
+ if query == ""
35
+ query = nil
36
+ end
37
+
38
+ uri = URI::Generic.new(
39
+ (https ? "https" : "http"), nil, host, nil, nil, path, nil, query, nil
40
+ )
41
+
42
+ if secret
43
+ sign(uri)
44
+ else
45
+ uri.to_s
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module LibPixel
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'libpixel/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "libpixel"
8
+ spec.version = LibPixel::VERSION
9
+ spec.authors = ["Joao Carlos"]
10
+ spec.email = ["joao@libpixel.com"]
11
+ spec.summary = %q{Ruby library to generate and sign LibPixel URLs.}
12
+ spec.homepage = "http://libpixel.com"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "minitest"
23
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'libpixel'
3
+
4
+ require 'minitest/autorun'
@@ -0,0 +1,66 @@
1
+ require "minitest_helper"
2
+
3
+ describe LibPixel::Client do
4
+
5
+ before do
6
+ @client = LibPixel::Client.new({
7
+ host: "test.libpx.com", https: false, secret: "LibPixel"
8
+ })
9
+ end
10
+
11
+ describe "#sign" do
12
+ it "adds the query string with the signature" do
13
+ url = "http://test.libpx.com/images/1.jpg"
14
+ signature = "bd5634c055d707c1638eff93eb88ff31277958f0"
15
+ assert_equal "#{url}?signature=#{signature}", @client.sign(url)
16
+ end
17
+
18
+ it "appends the signature to an existing query string" do
19
+ url = "http://test.libpx.com/images/2.jpg?width=400"
20
+ signature = "baa12c05ed279dbc623ffc8b74b183f6044e5998"
21
+ assert_equal "#{url}&signature=#{signature}", @client.sign(url)
22
+ end
23
+
24
+ it "ignores the '?' separator if there's no query string" do
25
+ url = "http://test.libpx.com/images/1.jpg"
26
+ assert_equal @client.sign(url), @client.sign("#{url}?")
27
+ end
28
+
29
+ it "supports URLs with a query string and a fragment" do
30
+ assert_equal "http://test.libpx.com/images/3.jpg?width=300&height=220&signature=500ad73bdf2d9e77d6bb94f0ca1c72f9c1f495f8#image",
31
+ @client.sign("http://test.libpx.com/images/3.jpg?width=300&height=220#image")
32
+ end
33
+
34
+ it "supports URLs with a fragment but no query string" do
35
+ assert_equal "http://test.libpx.com/images/1.jpg?signature=bd5634c055d707c1638eff93eb88ff31277958f0#test",
36
+ @client.sign("http://test.libpx.com/images/1.jpg#test")
37
+ end
38
+ end
39
+
40
+ describe "#url" do
41
+ it "constructs a URL for a given path" do
42
+ client = LibPixel::Client.new(host: "test.libpx.com")
43
+ url = "http://test.libpx.com/images/5.jpg"
44
+ assert_equal url, client.url("/images/5.jpg")
45
+ end
46
+
47
+ it "turns options into a query string" do
48
+ client = LibPixel::Client.new(host: "test.libpx.com")
49
+ url = "http://test.libpx.com/images/101.jpg?width=200&height=400"
50
+ assert_equal url, client.url("/images/101.jpg", width: 200, height: 400)
51
+ end
52
+
53
+ it "uses HTTPS if the https option is set to true" do
54
+ client = LibPixel::Client.new(host: "test.libpx.com", https: true)
55
+ url = "https://test.libpx.com/images/1.jpg"
56
+ assert_equal url, client.url("/images/1.jpg")
57
+ end
58
+
59
+ it "signs the request if a secret is given" do
60
+ client = LibPixel::Client.new(host: "test.libpx.com", secret: "LibPixel")
61
+ url = "http://test.libpx.com/images/1.jpg?width=600&signature=dfcaec7b88d53a7a932e8a6a00d10b4f9ff1336b"
62
+ assert_equal url, client.url("/images/1.jpg", width: 600)
63
+ end
64
+ end
65
+
66
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: libpixel
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Joao Carlos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-11 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: minitest
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:
56
+ email:
57
+ - joao@libpixel.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - lib/libpixel.rb
69
+ - lib/libpixel/client.rb
70
+ - lib/libpixel/version.rb
71
+ - libpixel.gemspec
72
+ - test/minitest_helper.rb
73
+ - test/test_client.rb
74
+ homepage: http://libpixel.com
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.4.5
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Ruby library to generate and sign LibPixel URLs.
98
+ test_files:
99
+ - test/minitest_helper.rb
100
+ - test/test_client.rb
101
+ has_rdoc: