image_info 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: 41902fde9c8f38c8f056e8c7713d9c674762143d
4
+ data.tar.gz: 29b70c445221d0cbbbdff692b4662089d7c5e6f2
5
+ SHA512:
6
+ metadata.gz: ed3ddfc150811e6a644e897638b821a32f33e4c41d127a9bf147fccae82fb761130c7c8b04d8aa6eecd727224504fbcbcc010bb9d24c330529441d73f880abb7
7
+ data.tar.gz: cdb46ae585dffe7cf6f635583655c09888bf5b41c315ac13ed963a0ab859f1adb1e6f529c222e7b5b8ca293fb2b141ce1ce2de967a5b44b79ddb85514e92dd46
@@ -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
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,85 @@
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
+
7
+ ImageInfo finds the size and type of a single or multiple images from the web by fetching as little as needed in batches.
8
+
9
+ ## Why
10
+
11
+ In [LinkThumbnailer](https://github.com/gottfrois/link_thumbnailer) I needed to find images sizes not only for one image.
12
+ A well known gem like FastImage was not enough so I decided to build my own.
13
+ The gem use [typhoeus](https://github.com/typhoeus/typhoeus) under the hood to perform http requests in parallel.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'image_info'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install image_info
30
+
31
+ ## Usage
32
+
33
+ For a single image:
34
+
35
+ ```ruby
36
+ image = ImageInfo.from('http://foo.com/foo.png').first
37
+ image.size
38
+ => [256, 256]
39
+ image.type
40
+ => :png
41
+ image.uri
42
+ => #<URI::HTTP:0x007fe7dccc1390 URL:http://foo.com/foo.png>
43
+ ```
44
+
45
+ For multiple images:
46
+
47
+ ```ruby
48
+ images = ImageInfo.from(['http://foo.com/foo.png', 'http://foo.com/bar.jpg'])
49
+ images.map &:size
50
+ => [[256, 256], [128, 128]]
51
+ images.map &:type
52
+ => [:png, :jpeg]
53
+ ```
54
+
55
+ ## Configuration
56
+
57
+ You can configure the `max_concurrency` value (20 by default) used to fetch images in parallel:
58
+
59
+ ```ruby
60
+ ImageInfo.configure do |config|
61
+ config.max_concurrency = 10
62
+ end
63
+ ```
64
+
65
+ or at runtime:
66
+
67
+ ```ruby
68
+ ImageInfo.from('http://foo.com/foo.png', max_concurrency: 10)
69
+ ```
70
+
71
+ ## Development
72
+
73
+ 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.
74
+
75
+ To install this gem onto your local machine, run `bundle exec rake install`.
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gottfrois/image_info.
80
+
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
85
+
@@ -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,19 @@
1
+ require 'uri'
2
+
3
+ module ImageInfo
4
+ class Image
5
+
6
+ attr_reader :uri
7
+ attr_accessor :size, :type
8
+
9
+ def initialize(uri)
10
+ @uri = ::URI.parse(uri.to_s)
11
+ @size = []
12
+ end
13
+
14
+ def valid?
15
+ uri.is_a?(::URI::HTTP)
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ require 'image_size'
2
+
3
+ module ImageInfo
4
+ class Parser
5
+
6
+ attr_reader :image, :bytes, :parser
7
+
8
+ def initialize(image, data)
9
+ @image = image
10
+ @bytes = data.bytes
11
+ @parser = ::ImageSize.new(data)
12
+ end
13
+
14
+ def call
15
+ set_image_size
16
+ set_image_type
17
+ end
18
+
19
+ private
20
+
21
+ def set_image_size
22
+ image.size = [parser.width, parser.height]
23
+ end
24
+
25
+ def set_image_type
26
+ image.type = parser.format
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,31 @@
1
+ require 'typhoeus'
2
+
3
+ require 'image_info/image'
4
+ require 'image_info/parser'
5
+ require 'image_info/request_handler'
6
+
7
+ module ImageInfo
8
+ class Processor
9
+
10
+ attr_reader :images, :options
11
+
12
+ def initialize(urls, options = {})
13
+ @images = Array(urls).map { |uri| ::ImageInfo::Image.new(uri) }.keep_if(&:valid?)
14
+ @options = options
15
+ end
16
+
17
+ def process
18
+ images.each { |image| hydra.queue(::ImageInfo::RequestHandler.new(image).build) }
19
+ hydra.run
20
+
21
+ images
22
+ end
23
+
24
+ private
25
+
26
+ def hydra
27
+ @hydra ||= ::Typhoeus::Hydra.new(max_concurrency: options[:max_concurrency])
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ module ImageInfo
2
+ class RequestHandler
3
+
4
+ attr_reader :image
5
+
6
+ def initialize(image)
7
+ @image = image
8
+ end
9
+
10
+ def build
11
+ ::Typhoeus::Request.new(image.uri.to_s, followlocation: true, accept_encoding: :gzip).tap do |request|
12
+ request.on_complete { |response| on_complete(response) }
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def on_complete(response)
19
+ ::ImageInfo::Parser.new(image, response.body).call if response.success?
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module ImageInfo
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: image_info
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Pierre-Louis Gottfrois
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-03 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
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - image_info.gemspec
115
+ - lib/image_info.rb
116
+ - lib/image_info/configurable.rb
117
+ - lib/image_info/configuration.rb
118
+ - lib/image_info/image.rb
119
+ - lib/image_info/parser.rb
120
+ - lib/image_info/processor.rb
121
+ - lib/image_info/request_handler.rb
122
+ - lib/image_info/version.rb
123
+ homepage: https://github.com/gottfrois/image_info
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.4.6
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: ImageInfo finds the size and type of a single or multiple images from the
147
+ web by fetching as little as needed.
148
+ test_files: []