smeargle 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in smeargle.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 David Ratajczak
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.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # Smeargle
2
+
3
+ Smeargle is an image collection tool that will return an object
4
+ containing images from a given URL. Smeargle was designed to be
5
+ a lightweight and simple solution for collecting images.
6
+
7
+ This library does provide some basic logic for helping you find
8
+ a suitable image from any given url. Smeargle uses rmagick for
9
+ filtering images based on size.
10
+
11
+ ![Smeargle](http://www.toplessrobot.com/smeargle.gif)
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'smeargle'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install smeargle
26
+
27
+ ## Usage
28
+
29
+ ### Getting Started
30
+
31
+ When initializing a smeargle object, you're able to provide a variety of
32
+ options to help with image selection.
33
+
34
+ ```
35
+ # Initialize a collection with a minimum image width of 200
36
+ Smeargle::Sketch.new 'http://mockra.com', min_width: 200
37
+ ```
38
+
39
+ The available options are:
40
+
41
+ ```
42
+ Minimum images dimentions returned
43
+ :min_width, :min_height
44
+ ```
45
+
46
+ ### Images
47
+
48
+ Images returned by your URL are included in the images array. Depending
49
+ on the number of images included in the body, this could take some time
50
+ to run. You may want to have it run in the background.
51
+
52
+ ```
53
+ s = Smeargle::Sketch.new 'google.com'
54
+
55
+ # returns an array filtered by provided options
56
+ s.images
57
+
58
+ s.images.each do |img|
59
+ img[:url]
60
+ img[:width]
61
+ img[:height]
62
+ end
63
+ ```
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,49 @@
1
+ require 'rmagick'
2
+
3
+ module Smeargle
4
+ module Image
5
+
6
+ def images
7
+ @images ||= filtered_images
8
+ end
9
+
10
+ def image_collection
11
+ images = []
12
+ response_body.css('img').each do |img|
13
+ images << img['src']
14
+ end
15
+ images.uniq
16
+ end
17
+
18
+ def formatted_images
19
+ formatted_images = []
20
+ image_collection.each do |img|
21
+ image = image_format img
22
+ formatted_images << image_details(image)
23
+ end
24
+ formatted_images
25
+ end
26
+
27
+ def image_format img
28
+ if URI(img).relative?
29
+ self.safe_url + img
30
+ else
31
+ img
32
+ end
33
+ end
34
+
35
+ def image_details img
36
+ remote_image = open img
37
+ image = Magick::Image::from_blob(remote_image.read).first
38
+ { url: img, height: image.columns, width: image.rows }
39
+ end
40
+
41
+ def filtered_images
42
+ images = formatted_images
43
+ images.reject! { |x| x[:width] < min_width } if min_width
44
+ images.reject! { |x| x[:height] < min_height } if min_height
45
+ images
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,32 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+ require "smeargle/image"
4
+
5
+ module Smeargle
6
+ class Sketch
7
+
8
+ include Smeargle::Image
9
+
10
+ attr_accessor :url, :min_width, :min_height
11
+
12
+ def initialize url, args = {}
13
+ @url = url
14
+ args.each do |key, value|
15
+ send "#{key}=", value
16
+ end
17
+ end
18
+
19
+ def safe_url
20
+ parsed_url = URI.parse url
21
+ if !parsed_url.scheme
22
+ @safe_url ||= 'http://' + url
23
+ end
24
+ @safe_url ||= url
25
+ end
26
+
27
+ def response_body
28
+ @response_body ||= Nokogiri::HTML(open safe_url)
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Smeargle
2
+ VERSION = "0.0.1"
3
+ end
data/lib/smeargle.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "smeargle/version"
2
+ require "smeargle/sketch"
3
+
4
+ module Smeargle
5
+ end
data/smeargle.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/smeargle/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["David Ratajczak"]
6
+ gem.email = ["david@mockra.com"]
7
+ gem.description = %q{Smeargle is a tool for collecting a set of images from a url.}
8
+ gem.summary = %q{The smeargle library is designed to return an object with
9
+ various methods to help you traverse the collection of images
10
+ returned by a url.}
11
+ gem.homepage = "http://mockra.com"
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.name = "smeargle"
17
+ gem.require_paths = ["lib"]
18
+ gem.version = Smeargle::VERSION
19
+
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_dependency 'nokogiri'
23
+ gem.add_dependency 'fakeweb'
24
+ gem.add_dependency 'rmagick'
25
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Smeargle::Image do
4
+
5
+ let(:smeargle) { Smeargle::Sketch.new 'google.com' }
6
+
7
+ before do
8
+ smeargle.stub!(:image_details).and_return(
9
+ { url: 'test.png', height: 10, width: 10 })
10
+
11
+ FakeWeb.register_uri :get, 'http://google.com',
12
+ body: "Google <img src='test.png' /> <img src='test.png' />"
13
+ end
14
+
15
+ describe 'images' do
16
+ it 'should return an array' do
17
+ smeargle.images.should be_a Array
18
+ end
19
+
20
+ it 'should contain the sample image' do
21
+ smeargle.images.first[:url].should =~ /test\.png/i
22
+ end
23
+
24
+ it 'should contain hashes' do
25
+ smeargle.images.first.should be_a Hash
26
+ end
27
+ end
28
+
29
+ describe 'image_format' do
30
+ it 'should format relative paths' do
31
+ e = smeargle.image_format '/test.png'
32
+ e.should == 'http://google.com/test.png'
33
+ end
34
+
35
+ it 'should not format absolute paths' do
36
+ e = smeargle.image_format 'http://google.com/test.png'
37
+ e.should == 'http://google.com/test.png'
38
+ end
39
+ end
40
+
41
+ describe 'image_collection' do
42
+ it 'should not contain duplicates' do
43
+ smeargle.image_collection.count.should == 1
44
+ end
45
+ end
46
+
47
+ describe 'filtered_images' do
48
+ it 'should filter images on height' do
49
+ s = Smeargle::Sketch.new 'google.com', min_width: 20
50
+ s.stub!(:image_details).and_return(
51
+ { url: 'test.png', height: 10, width: 10 })
52
+ s.filtered_images.count.should == 0
53
+ end
54
+ end
55
+
56
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe Smeargle::Sketch do
4
+
5
+ let(:smeargle) { Smeargle::Sketch.new 'google.com' }
6
+
7
+ before do
8
+ FakeWeb.register_uri :get, 'http://google.com',
9
+ body: "Google <img src='test.png' />"
10
+ end
11
+
12
+ describe 'new' do
13
+ it 'should initialize url' do
14
+ s = Smeargle::Sketch.new 'http://google.com'
15
+ s.url.should == 'http://google.com'
16
+ end
17
+
18
+ it 'should set min width/height' do
19
+ s = Smeargle::Sketch.new 'http://google.com',
20
+ min_width: 200, min_height: 250
21
+ s.min_width.should == 200
22
+ s.min_height.should == 250
23
+ end
24
+ end
25
+
26
+ describe 'safe_url' do
27
+ it 'should format the url' do
28
+ smeargle.safe_url.should == 'http://google.com'
29
+ end
30
+
31
+ it 'should leave the url alone' do
32
+ s = Smeargle::Sketch.new 'http://google.com'
33
+ s.safe_url.should == 'http://google.com'
34
+ s.safe_url.should == s.url
35
+ end
36
+ end
37
+
38
+ describe 'response' do
39
+ it 'should have the correct body' do
40
+ smeargle.response_body.css('body').text.
41
+ should =~ /google/i
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe Smeargle do
4
+ end
@@ -0,0 +1,3 @@
1
+ require 'smeargle'
2
+ require 'fakeweb'
3
+ require 'nokogiri'
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smeargle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Ratajczak
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: nokogiri
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: fakeweb
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rmagick
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Smeargle is a tool for collecting a set of images from a url.
95
+ email:
96
+ - david@mockra.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE
104
+ - README.md
105
+ - Rakefile
106
+ - lib/smeargle.rb
107
+ - lib/smeargle/image.rb
108
+ - lib/smeargle/sketch.rb
109
+ - lib/smeargle/version.rb
110
+ - smeargle.gemspec
111
+ - spec/smeargle/image_spec.rb
112
+ - spec/smeargle/sketch_spec.rb
113
+ - spec/smeargle_spec.rb
114
+ - spec/spec_helper.rb
115
+ homepage: http://mockra.com
116
+ licenses: []
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ segments:
128
+ - 0
129
+ hash: 1742334504080811707
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ segments:
137
+ - 0
138
+ hash: 1742334504080811707
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 1.8.23
142
+ signing_key:
143
+ specification_version: 3
144
+ summary: The smeargle library is designed to return an object with various methods
145
+ to help you traverse the collection of images returned by a url.
146
+ test_files:
147
+ - spec/smeargle/image_spec.rb
148
+ - spec/smeargle/sketch_spec.rb
149
+ - spec/smeargle_spec.rb
150
+ - spec/spec_helper.rb