image_info 1.1.2

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: bc41310509a4e3d07a8e9a671875674e532f5e99
4
+ data.tar.gz: 8323109506dd885913623e9505dd5c51bddb0e51
5
+ SHA512:
6
+ metadata.gz: 46b4ddbece7f0a43cda5731cfddc80479112faec2cf7e1cdcdfb83a27f43092cd45634d2581eff2816f2bc85f78db6490e900745edef5d2ed1ab51f1e77d2320
7
+ data.tar.gz: 1551bf0913c31c88830b387b10c603c3ef0858add6f0a213f1222cc1fb6185f14f05b7c0b79911e2d5e197801e4e5f903b83ea0130820adb51584f686357899c
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.2.0
7
+ before_install: gem install bundler -v 1.10.4
@@ -0,0 +1,25 @@
1
+ * 1.1.2
2
+
3
+ - Fix issues with schemaless uri (ex: `//foo.com`)
4
+
5
+ * 1.1.1
6
+
7
+ Now only fetch partial image data to compute its size and type.
8
+
9
+ Thanks to [vdaubry](https://github.com/vdaubry) the gem now uses typhoeus
10
+ stream capability to fetch images and get its size and type, aborting the
11
+ request if necessary. Make sure to use typhoeus `>= 0.7.3` in order to benefit
12
+ from this update since there was an issue with previous typhoeus version when
13
+ aborting ongoing requests.
14
+
15
+ Fixes [#1](https://github.com/gottfrois/image_info/issues/1)
16
+
17
+ Also fix URL encoding issues.
18
+
19
+ * 1.1.0
20
+
21
+ - Introduces `width` and `height` method. Fixes [https://github.com/gottfrois/image_info/issues/2](https://github.com/gottfrois/image_info/issues/2)
22
+
23
+ * 1.0.0
24
+
25
+ - First release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in image_info.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Pierre-Louis Gottfrois
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.
@@ -0,0 +1,91 @@
1
+ # ImageInfo
2
+
3
+ [![Code Climate](https://codeclimate.com/github/gottfrois/image_info/badges/gpa.svg)](https://codeclimate.com/github/gottfrois/image_info)
4
+ [![Build Status](https://travis-ci.org/gottfrois/image_info.svg)](https://travis-ci.org/gottfrois/image_info)
5
+ [![Dependency Status](https://gemnasium.com/gottfrois/image_info.svg)](https://gemnasium.com/gottfrois/image_info)
6
+ [![Gem Version](https://badge.fury.io/rb/image_info.svg)](http://badge.fury.io/rb/image_info)
7
+
8
+ ImageInfo finds the size and type of a single or multiple images from the web by fetching as little data as needed (partial image) in batches.
9
+
10
+ ## Why
11
+
12
+ In [LinkThumbnailer](https://github.com/gottfrois/link_thumbnailer) I needed to find images sizes not only for one image.
13
+ A well known gem like FastImage was not enough so I decided to build my own.
14
+ The gem use [typhoeus](https://github.com/typhoeus/typhoeus)'s parallel requests and stream capability under the hood to
15
+ get images.
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'image_info'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install image_info
32
+
33
+ ## Usage
34
+
35
+ For a single image:
36
+
37
+ ```ruby
38
+ image = ImageInfo.from('http://foo.com/foo.png').first
39
+ image.width
40
+ => 256
41
+ image.height
42
+ => 256
43
+ image.size
44
+ => [256, 256]
45
+ image.type
46
+ => :png
47
+ image.uri
48
+ => #<URI::HTTP:0x007fe7dccc1390 URL:http://foo.com/foo.png>
49
+ ```
50
+
51
+ For multiple images:
52
+
53
+ ```ruby
54
+ images = ImageInfo.from(['http://foo.com/foo.png', 'http://foo.com/bar.jpg'])
55
+ images.map &:size
56
+ => [[256, 256], [128, 128]]
57
+ images.map &:type
58
+ => [:png, :jpeg]
59
+ ```
60
+
61
+ ## Configuration
62
+
63
+ You can configure the `max_concurrency` value (20 by default) used to fetch images in parallel:
64
+
65
+ ```ruby
66
+ ImageInfo.configure do |config|
67
+ config.max_concurrency = 10
68
+ end
69
+ ```
70
+
71
+ or at runtime:
72
+
73
+ ```ruby
74
+ ImageInfo.from('http://foo.com/foo.png', max_concurrency: 10)
75
+ ```
76
+
77
+ ## Development
78
+
79
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
80
+
81
+ To install this gem onto your local machine, run `bundle exec rake install`.
82
+
83
+ ## Contributing
84
+
85
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gottfrois/image_info.
86
+
87
+
88
+ ## License
89
+
90
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
91
+
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
7
+ task test: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "image_info"
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'image_info/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "image_info"
8
+ spec.version = ImageInfo::VERSION
9
+ spec.authors = ["Pierre-Louis Gottfrois"]
10
+ spec.email = ["pierrelouis.gottfrois@gmail.com"]
11
+
12
+ spec.summary = %q{ImageInfo finds the size and type of a single or multiple images from the web by fetching as little as needed.}
13
+ spec.description = %q{ImageInfo finds the size and type of a single or multiple images from the web by fetching as little as needed.}
14
+ spec.homepage = "https://github.com/gottfrois/image_info"
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_dependency "typhoeus"
23
+ spec.add_dependency "image_size"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "webmock"
29
+ end
@@ -0,0 +1,23 @@
1
+ require 'image_info/version'
2
+ require 'image_info/processor'
3
+ require 'image_info/configurable'
4
+
5
+ module ImageInfo
6
+ extend Configurable
7
+
8
+ # Returns type and size given an url or a list of urls.
9
+ #
10
+ # @example
11
+ # ImageInfo.from('http://foo.com/foo.png').first.size
12
+ # => [250, 86]
13
+ #
14
+ # @example
15
+ # ImageInfo.from(['http://foo.com/foo.png', 'http://foo.com/bar.jpeg']).map(&:type)
16
+ # => [:png, :jpeg]
17
+ #
18
+ # @return [Array<ImageInfo::Image>]
19
+ def self.from(urls, options = { max_concurrency: config.max_concurrency })
20
+ ::ImageInfo::Processor.new(urls, options).process
21
+ end
22
+
23
+ end
@@ -0,0 +1,15 @@
1
+ require 'image_info/configuration'
2
+
3
+ module ImageInfo
4
+ module Configurable
5
+
6
+ def config
7
+ @config ||= ::ImageInfo::Configuration.new
8
+ end
9
+
10
+ def configure
11
+ yield config if block_given?
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module ImageInfo
2
+ class Configuration
3
+
4
+ attr_accessor :max_concurrency
5
+
6
+ def initialize
7
+ @max_concurrency = 20
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,33 @@
1
+ require 'uri'
2
+
3
+ module ImageInfo
4
+ class Image
5
+
6
+ attr_reader :uri
7
+ attr_accessor :width, :height, :type
8
+
9
+ def initialize(uri)
10
+ @uri = ::URI.parse(::URI.encode(uri.to_s))
11
+ @uri.scheme = 'http' unless @uri.scheme
12
+ @uri.port = 80 unless @uri.port
13
+ rescue ::URI::InvalidURIError
14
+ @uri = NullUri.new
15
+ end
16
+
17
+ def size
18
+ [width, height].compact
19
+ end
20
+
21
+ def valid?
22
+ !!uri.host
23
+ end
24
+
25
+ private
26
+
27
+ class NullUri
28
+ def host
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,12 @@
1
+ module ImageInfo
2
+ class NullParser
3
+ def width
4
+ end
5
+
6
+ def height
7
+ end
8
+
9
+ def format
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,37 @@
1
+ require 'image_size'
2
+
3
+ module ImageInfo
4
+ class Parser
5
+
6
+ attr_reader :image, :data
7
+
8
+ def initialize(image, data)
9
+ @image = image
10
+ @data = data
11
+ end
12
+
13
+ def call
14
+ set_image_size
15
+ set_image_type
16
+ end
17
+
18
+ private
19
+
20
+ def set_image_size
21
+ image.width = parser.width
22
+ image.height = parser.height
23
+ end
24
+
25
+ def set_image_type
26
+ image.type = parser.format
27
+ end
28
+
29
+ private
30
+
31
+ def parser
32
+ @parser ||= ::ImageSize.new(data)
33
+ rescue ImageSize::FormatError
34
+ @parser ||= ::ImageInfo::NullParser.new
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,32 @@
1
+ require 'typhoeus'
2
+
3
+ require 'image_info/image'
4
+ require 'image_info/parser'
5
+ require 'image_info/null_parser'
6
+ require 'image_info/request_handler'
7
+
8
+ module ImageInfo
9
+ class Processor
10
+
11
+ attr_reader :images, :options
12
+
13
+ def initialize(urls, options = {})
14
+ @images = Array(urls).map { |uri| ::ImageInfo::Image.new(uri) }.keep_if(&:valid?)
15
+ @options = options
16
+ end
17
+
18
+ def process
19
+ images.each { |image| hydra.queue(::ImageInfo::RequestHandler.new(image).build) }
20
+ hydra.run
21
+
22
+ images
23
+ end
24
+
25
+ private
26
+
27
+ def hydra
28
+ @hydra ||= ::Typhoeus::Hydra.new(max_concurrency: options[:max_concurrency])
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,26 @@
1
+ module ImageInfo
2
+ class RequestHandler
3
+
4
+ attr_reader :image, :buffer
5
+
6
+ def initialize(image)
7
+ @image = image
8
+ @buffer = StringIO.new
9
+ end
10
+
11
+ def build
12
+ ::Typhoeus::Request.new(image.uri.to_s, followlocation: true, accept_encoding: :gzip).tap do |request|
13
+ request.on_body do |chunk|
14
+ buffer.write(chunk)
15
+ :abort if found_image_info?(buffer)
16
+ end
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def found_image_info?(chunk)
23
+ ::ImageInfo::Parser.new(image, chunk).call
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module ImageInfo
2
+ VERSION = '1.1.2'
3
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: image_info
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Pierre-Louis Gottfrois
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: typhoeus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: image_size
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: ImageInfo finds the size and type of a single or multiple images from
98
+ the web by fetching as little as needed.
99
+ email:
100
+ - pierrelouis.gottfrois@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - .rspec
107
+ - .travis.yml
108
+ - CHANGELOG.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - image_info.gemspec
116
+ - lib/image_info.rb
117
+ - lib/image_info/configurable.rb
118
+ - lib/image_info/configuration.rb
119
+ - lib/image_info/image.rb
120
+ - lib/image_info/null_parser.rb
121
+ - lib/image_info/parser.rb
122
+ - lib/image_info/processor.rb
123
+ - lib/image_info/request_handler.rb
124
+ - lib/image_info/version.rb
125
+ homepage: https://github.com/gottfrois/image_info
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.4.6
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: ImageInfo finds the size and type of a single or multiple images from the
149
+ web by fetching as little as needed.
150
+ test_files: []