libpixel 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +11 -0
  4. data/README.md +90 -2
  5. data/Rakefile +0 -1
  6. data/lib/libpixel.rb +18 -0
  7. data/lib/libpixel/client.rb +36 -5
  8. data/lib/libpixel/railtie.rb +10 -0
  9. data/lib/libpixel/version.rb +1 -1
  10. data/lib/libpixel/view_helpers.rb +35 -0
  11. data/libpixel.gemspec +5 -4
  12. data/test/dummy/README.rdoc +28 -0
  13. data/test/dummy/Rakefile +6 -0
  14. data/test/dummy/app/assets/images/.keep +0 -0
  15. data/test/dummy/app/assets/javascripts/application.js +13 -0
  16. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  17. data/test/dummy/app/controllers/application_controller.rb +5 -0
  18. data/test/dummy/app/controllers/concerns/.keep +0 -0
  19. data/test/dummy/app/helpers/application_helper.rb +2 -0
  20. data/test/dummy/app/mailers/.keep +0 -0
  21. data/test/dummy/app/models/.keep +0 -0
  22. data/test/dummy/app/models/concerns/.keep +0 -0
  23. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  24. data/test/dummy/bin/bundle +3 -0
  25. data/test/dummy/bin/rails +4 -0
  26. data/test/dummy/bin/rake +4 -0
  27. data/test/dummy/bin/setup +29 -0
  28. data/test/dummy/config.ru +4 -0
  29. data/test/dummy/config/application.rb +32 -0
  30. data/test/dummy/config/boot.rb +5 -0
  31. data/test/dummy/config/environment.rb +5 -0
  32. data/test/dummy/config/environments/development.rb +41 -0
  33. data/test/dummy/config/environments/production.rb +79 -0
  34. data/test/dummy/config/environments/test.rb +42 -0
  35. data/test/dummy/config/initializers/assets.rb +11 -0
  36. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  37. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  38. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  39. data/test/dummy/config/initializers/inflections.rb +16 -0
  40. data/test/dummy/config/initializers/mime_types.rb +4 -0
  41. data/test/dummy/config/initializers/session_store.rb +3 -0
  42. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  43. data/test/dummy/config/locales/en.yml +23 -0
  44. data/test/dummy/config/routes.rb +56 -0
  45. data/test/dummy/config/secrets.yml +22 -0
  46. data/test/dummy/lib/assets/.keep +0 -0
  47. data/test/dummy/public/404.html +67 -0
  48. data/test/dummy/public/422.html +67 -0
  49. data/test/dummy/public/500.html +66 -0
  50. data/test/dummy/public/favicon.ico +0 -0
  51. data/test/minitest_helper.rb +5 -0
  52. data/test/test_client.rb +43 -0
  53. data/test/test_helper.rb +18 -0
  54. data/test/test_rails.rb +93 -0
  55. metadata +109 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 371f5707b83500219bf589fec16a37d1390cd6e6
4
- data.tar.gz: 6d1f6141025ec621708552bff558aa49c364c56b
3
+ metadata.gz: 8920757faf3f137c4b288deaf5dbd740dde38d5b
4
+ data.tar.gz: ef94228a86b9cde78f6da8c2f3d0a4d4b73fa8e4
5
5
  SHA512:
6
- metadata.gz: d6f2bc027f67b5c6cd43f3724f48b072c997ac1d76604e4e4b16c668f13efe90097c96c5016336b1bffb5a9760ebb72bac6c645a1e62a87a8f3c6111867af186
7
- data.tar.gz: 8f5b3e5ac05bbf2cddc21a627311f6d5655cea7efe48f4117482c6e7e496dbac175b74f7d6c1c3020ab1e3a826ddf238fb10aa1bdf072a6daa376884d3d4aa5d
6
+ metadata.gz: 28dd2bc72c9f6ba10daefde005cff93130d3bf2d4277a730d4d65db58ab29a3adac3d6987f34e4832463bf2ab32bd05130e8035e682be389b903fd72bd29a1aa
7
+ data.tar.gz: 7ec30753b454d2dfb4ed97d5b2d66d0fcc3e4917d4f0121f47de1494a76732fc02f5bf4c59dab331351fbb6826439d5a01144d9c09989082ab0eeea22a296143
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ /test/dummy/log/
data/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
2
2
  All notable changes to this project will be documented in this file.
3
3
  This project adheres to [Semantic Versioning](http://semver.org/).
4
4
 
5
+ ## [1.2.0] - 2015-09-19
6
+ ### Added
7
+ - Add `libpixel_image_tag` Rails view helper.
8
+ - Add `https` option to `LibPixel::Client.url`.
9
+ - Set host automatically in clients from `LIBPIXEL_HOST` env var.
10
+ - Set secret automatically in clients from `LIBPIXEL_SECRET` env var.
11
+
12
+ ### Changed
13
+ - `LibPixel::Client.url` raises an exception if host is undefined.
14
+ - `LibPixel::Client.sign` raises an exception if secret is undefined.
15
+
5
16
  ## [1.1.0] - 2015-08-12
6
17
  ### Changed
7
18
  - Make path optional in LibPixel::Client#url [@matiaskorhonen](https://github.com/matiaskorhonen).
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/libpixel/libpixel-ruby.svg?branch=master)](https://travis-ci.org/libpixel/libpixel-ruby)
4
4
 
5
- Ruby library to generate and sign LibPixel URLs.
5
+ Ruby library and Rails plugin to generate and sign LibPixel URLs.
6
6
 
7
7
  ## Installation
8
8
 
@@ -29,9 +29,12 @@ LibPixel.setup do |config|
29
29
  config.host = "test.libpx.com" # Your LibPixel domain. Required.
30
30
  config.https = true # Generates HTTPS URLs. Optional. Default is false.
31
31
  config.secret = "..." # Auth secret for your LibPixel account. Required for signing requests.
32
+ config.default_source = "us-east-1/source" # optional source to be used, can be overriden
32
33
  end
33
34
  ```
34
35
 
36
+ The configuration for host and secret will be automatically set from the environment variables LIBPIXEL_HOST and LIBPIXEL_SECRET if they are present.
37
+
35
38
  ### Sign URLs
36
39
 
37
40
  You can sign an existing URL using the `sign` function:
@@ -45,7 +48,7 @@ url = LibPixel.sign("http://test.libpx.com/images/1.jpg?width=400")
45
48
  You can also generate and sign URLs at the same time with the `url` function:
46
49
 
47
50
  ```ruby
48
- url = LibPixel.url("/images/1.jpg", height: 400, blur: 20, saturation: -80)
51
+ url = LibPixel.url("/us-east-1/images/1.jpg", height: 400, blur: 20, saturation: -80)
49
52
  ```
50
53
 
51
54
  If you're using the `src` parameter, you can skip the path:
@@ -54,6 +57,31 @@ If you're using the `src` parameter, you can skip the path:
54
57
  url = LibPixel.url(src: "http://...", width: 300)
55
58
  ```
56
59
 
60
+ But even simpler, if the library sees a url beginning with http or https it knows what to do:
61
+
62
+ ```ruby
63
+ url = LibPixel.url("http://...", width: 300)
64
+ ```
65
+
66
+ You can specify whether you what an http or https url in your call:
67
+
68
+ ```ruby
69
+ url = LibPixel.url("/us-east-1/images/1.jpg", height: 400, blur: 20, saturation: -80, https: true)
70
+ ```
71
+
72
+ If you are using a default_source, you don't need to specify it in the path:
73
+
74
+ ```ruby
75
+ url = LibPixel.url("1.jpg", height: 400, blur: 20, saturation: -80)
76
+ ```
77
+
78
+ But you can override it with the source parameter:
79
+
80
+ ```ruby
81
+ url = LibPixel.url("1.jpg", height: 400, blur: 20, saturation: -80, source: "us-west-1/source2")
82
+ ```
83
+
84
+
57
85
  ### Multiple clients
58
86
 
59
87
  It's also possible to have multiple instances of LibPixel clients (e.g. when dealing with multiple accounts):
@@ -64,6 +92,66 @@ client = LibPixel::Client.new(host: "test.libpx.com", https: true, secret: "..."
64
92
 
65
93
  You may then call the `#url` and `#sign` methods on the client object.
66
94
 
95
+ ## Ruby on Rails
96
+
97
+ The LibPixel gem includes a Rails plugin that provides a view helper that you can use in place of normal calls to `image_tag`. The libpixel host and libpixel secret settings should be set in an initializer, or will be automatically picked up from the environment variables `LIBPIXEL_HOST` and `LIBPIXEL_SECRET`.
98
+
99
+ ```ruby
100
+ libpixel_image_tag("eu-west-1/source/foo.jpg")
101
+ => "<img src=\"http://example.libpx.com/eu-west-1/source/foo.jpg\" alt=\"Foo\" />"
102
+ ```
103
+
104
+ You specify the libpixel processing parameters in a hash within the normal options hash, denoted by the key `:libpixel`.
105
+
106
+ ```ruby
107
+ libpixel_image_tag("eu-west-1/source/foo.jpg", libpixel: {width: 300})
108
+ ```
109
+
110
+ The normal parameters that Rails uses will also work.
111
+
112
+ ```ruby
113
+ libpixel_image_tag("eu-west-1/source/foo.jpg", libpixel: {width: 300, dpr: 2}, size: "300x250")
114
+ ```
115
+
116
+ If all your images are from the same source, it's helpful to configure a `default_source` in an initializer.
117
+
118
+ ```ruby
119
+ LibPixel.default_source = "eu-west-1/source"
120
+ ```
121
+
122
+ Then you can omit the source in your tags.
123
+
124
+ ```ruby
125
+ libpixel_image_tag("foo.jpg", libpixel: {width: 300, dpr: 2}, size: "300x250")
126
+ ```
127
+
128
+ If you need to override the default source, you can do that using the source parameter.
129
+
130
+ ```ruby
131
+ libpixel_image_tag("foo.jpg", libpixel: {width: 300, dpr: 2, source: "eu-west-1/source2"}, size: "300x250")
132
+ ```
133
+
134
+ Referring to an image outside of your configured sources is also possible.
135
+
136
+ ```ruby
137
+ libpixel_image_tag("http://example.com/images/foo.jpg", libpixel: {width: 300, dpr: 2}, size: "300x250")
138
+ ```
139
+
140
+ You can configure your generated image src urls to use https or http in your initializer.
141
+
142
+ ```ruby
143
+ LibPixel.https = true # default is false
144
+ ```
145
+
146
+ And you can specify it on a per-tag basis.
147
+
148
+ ```ruby
149
+ libpixel_image_tag("us-east-1/source/foo.jpg", libpixel: {https: true})
150
+ => "<img src=\"https://example.libpx.com/us-east-1/source/foo.jpg\" alt=\"Foo\" />"
151
+ ```
152
+
153
+
154
+
67
155
  ## License
68
156
 
69
157
  [MIT](LICENSE)
data/Rakefile CHANGED
@@ -6,4 +6,3 @@ Rake::TestTask.new(:test) do |t|
6
6
  end
7
7
 
8
8
  task :default => :test
9
-
data/lib/libpixel.rb CHANGED
@@ -9,6 +9,22 @@ module LibPixel
9
9
  yield @@default_client
10
10
  end
11
11
 
12
+ def self.host=(host)
13
+ @@default_client.host = host
14
+ end
15
+
16
+ def self.secret=(secret)
17
+ @@default_client.secret = secret
18
+ end
19
+
20
+ def self.https=(https)
21
+ @@default_client.https = https
22
+ end
23
+
24
+ def self.default_source=(default_source)
25
+ @@default_client.default_source = default_source
26
+ end
27
+
12
28
  def self.sign(uri)
13
29
  @@default_client.sign(uri)
14
30
  end
@@ -18,3 +34,5 @@ module LibPixel
18
34
  end
19
35
 
20
36
  end
37
+
38
+ require 'libpixel/railtie' if defined?(Rails)
@@ -3,15 +3,22 @@ require "uri"
3
3
 
4
4
  module LibPixel
5
5
  class Client
6
- attr_accessor :host, :secret, :https
6
+ attr_accessor :host, :secret, :https, :default_source
7
7
 
8
8
  def initialize(options={})
9
9
  options.each do |key, value|
10
10
  send("#{key}=", value)
11
11
  end
12
+
13
+ self.host ||= ENV["LIBPIXEL_HOST"]
14
+ self.secret ||= ENV["LIBPIXEL_SECRET"]
12
15
  end
13
16
 
14
17
  def sign(uri)
18
+ if secret.nil?
19
+ raise "Your LibPixel secret must be defined (e.g. LibPixel.secret = 'SECRET')"
20
+ end
21
+
15
22
  uri = URI.parse(uri) unless uri.kind_of?(URI::Generic)
16
23
 
17
24
  query = uri.query
@@ -30,22 +37,46 @@ module LibPixel
30
37
 
31
38
  def url(path_or_options, options={})
32
39
  path = nil
40
+
41
+ if host.nil?
42
+ raise "Your LibPixel host name must be defined (e.g. LibPixel.host = 'example.libpx.com')"
43
+ end
33
44
 
34
45
  if path_or_options.respond_to? :fetch
35
46
  options = path_or_options
36
- else
37
- path = path_or_options
47
+ elsif !path_or_options.nil?
48
+ uri = URI(path_or_options)
49
+ if uri.scheme == "http" || uri.scheme == "https"
50
+ options[:src] = path_or_options
51
+ else
52
+ path = path_or_options
53
+ end
38
54
  end
39
55
 
40
- path = "/" if path.nil? || path.empty?
56
+ source = options.fetch(:source) {default_source}
57
+ options = options.reject {|k| k == :source}
58
+
59
+ use_https = options.fetch(:https) {https}
60
+ options = options.reject {|k| k == :https}
61
+
41
62
  query = options.map { |k,v| "#{k}=#{URI.encode_www_form_component(v)}" }.join("&")
42
63
 
43
64
  if query == ""
44
65
  query = nil
45
66
  end
67
+
68
+ if !source.nil? && !source.empty?
69
+ source_clean = source.gsub(/^\//, "").gsub(/\/$/, "")
70
+ path_clean = (path || "").gsub(/^\//, "")
71
+ path = "/#{source_clean}/#{path_clean}"
72
+ else
73
+ if path.nil? || path !~ /^\//
74
+ path = "/#{path}"
75
+ end
76
+ end
46
77
 
47
78
  uri = URI::Generic.new(
48
- (https ? "https" : "http"), nil, host, nil, nil, path, nil, query, nil
79
+ (use_https ? "https" : "http"), nil, host, nil, nil, path, nil, query, nil
49
80
  )
50
81
 
51
82
  if secret
@@ -0,0 +1,10 @@
1
+ require 'libpixel/view_helpers'
2
+
3
+ module LibPixel
4
+ class Railtie < Rails::Railtie
5
+
6
+ ActiveSupport.on_load(:action_view) do
7
+ include LibPixel::ViewHelpers
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module LibPixel
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -0,0 +1,35 @@
1
+ module LibPixel
2
+ module ViewHelpers
3
+ #
4
+ # libpixel_image_tag("http://example.com/image.jpg", :width=>200, :libpixel=>{:width=>200, :dpr=>2, :source=>"images"})
5
+ # => <img width="200" alt="image" src="http://example.libpx.com/images/?width=200&dpr=2&src=http%3A%2F%2Fexample.com%2Fimage.jpg" />
6
+ #
7
+ # LibPixel.default_source = "source"
8
+ # libpixel_image_tag("image.jpg", :libpixel=>{:width=>200})
9
+ # => <img alt="image" src="http://example.libpx.com/source/image.jpg?width=200" />
10
+ #
11
+ def libpixel_image_tag(source, options = {})
12
+ image_tag_options = nil
13
+
14
+ if source.respond_to? :fetch
15
+ libpixel_url = LibPixel.url(source[:libpixel])
16
+ image_tag_options = source.reject {|k| k == :libpixel}
17
+ else
18
+ source = URI::escape(source)
19
+ libpixel_url = LibPixel.url(source, options.fetch(:libpixel) {{}})
20
+ image_tag_options = options.reject {|k| k == :libpixel}
21
+ end
22
+
23
+ uri = URI(libpixel_url)
24
+ if uri.query
25
+ query_items = CGI.parse(uri.query)
26
+ src = query_items["src"].first
27
+ if src
28
+ image_tag_options[:alt] = image_tag_options.fetch(:alt) {image_alt(src)}
29
+ end
30
+ end
31
+
32
+ image_tag(libpixel_url.html_safe, image_tag_options)
33
+ end
34
+ end
35
+ end
data/libpixel.gemspec CHANGED
@@ -6,9 +6,9 @@ require 'libpixel/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "libpixel"
8
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.}
9
+ spec.authors = ["Joao Carlos", "Stephen Sykes"]
10
+ spec.email = ["support@libpixel.com"]
11
+ spec.summary = %q{Ruby library and Rails plugin to generate and sign LibPixel URLs.}
12
12
  spec.homepage = "https://github.com/libpixel/libpixel-ruby"
13
13
  spec.license = "MIT"
14
14
 
@@ -19,5 +19,6 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_development_dependency "bundler", "~> 1.7"
21
21
  spec.add_development_dependency "rake", "~> 10.0"
22
- spec.add_development_dependency "minitest"
22
+ spec.add_development_dependency "minitest", "~> 5.8.0"
23
+ spec.add_development_dependency "rails", "~> 4.2.3"
23
24
  end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
File without changes
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
File without changes
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
File without changes
File without changes
File without changes