photograph 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 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in photograph.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 JH. Chabran
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/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2007, Tammer Saleh, Thoughtbot, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Photograph
2
+
3
+ Photograph solves the issue of generating previews or thumbnails of
4
+ web-based documents. Think of generating a preview of a Google
5
+ Spreadsheet. As a such document is dom based, it may be difficult to get a preview
6
+ that truly reflects the original content.
7
+
8
+ Photograph solves that problem by firing a chrome instance thanks to
9
+ capybara-webkit and sinatra to provides an easy way to interface it to
10
+ your needs.
11
+
12
+ Obviously, it supports cropping to avoid reworking the image afterward.
13
+
14
+ Please remind that having a chrome instance, even if it is being reused
15
+ by all requests is still taking some time, aroung 600ms after the first
16
+ request on my development machine. *Consider using photograph only if
17
+ you expect the same exact rendering of your 'web documents'*.
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ gem 'photograph'
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install photograph
32
+
33
+ ## Usage
34
+
35
+ Photograph can be used either directly through the Photograph::Artist
36
+ class or by its little sinata app.
37
+
38
+ @artist = Photograph::Artist.new("http://github.com")
39
+ @artist.shoot!
40
+
41
+ @artist.image
42
+ # => MiniMagick instance you can toy with
43
+
44
+ Or
45
+
46
+ $ bundle exec photograph -h 127.0.0.1 -p 4567
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
55
+
56
+ ## Credits
57
+
58
+
59
+ Photograph is maintained and funded by Tactilize.
60
+
61
+ Contributors :
62
+
63
+ * Jean-Hadrien Chabran
64
+
65
+ The names and logos for Tactilize are trademarks of Tactilize.
66
+
67
+ ## License
68
+
69
+ Photograph is Copyright © 2012 Tactilize. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task :default => :spec
data/bin/config.ru ADDED
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
2
+ require 'photograph'
3
+
4
+ run Photograph::Service.new
data/bin/photograph ADDED
@@ -0,0 +1,26 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "bundler/setup"
5
+
6
+ require 'optparse'
7
+ require 'thin'
8
+
9
+ require 'photograph'
10
+
11
+ options = {:port => 3001, :host => "127.0.0.1"}
12
+
13
+ OptionParser.new do |opts|
14
+ opts.banner = "Usage : photograph [options]"
15
+
16
+ opts.on "-h", "--host HOST", "Listen on the specified host" do |host|
17
+ options[:host] = host
18
+ end
19
+ opts.on "-p", "--port PORT", "Listen on the specified port" do |port|
20
+ options[:port] = port.to_i
21
+ end
22
+ end.parse!
23
+
24
+ Thin::Server.start options[:host], options[:port] do
25
+ run Photograph::Service.new
26
+ end
@@ -0,0 +1,79 @@
1
+ require 'capybara-webkit'
2
+ require 'mini_magick'
3
+
4
+
5
+ Capybara.default_wait_time = 5
6
+
7
+ module Photograph
8
+ class Artist
9
+ attr_accessor :options
10
+ attr_reader :image
11
+
12
+ MissingUrlError = Class.new(Exception)
13
+ DefaultOptions = {
14
+ :x => 0, # top left position
15
+ :y => 0,
16
+ :w => 1280, # bottom right position
17
+ :h => 1024,
18
+
19
+ :wait => 0.5, # if selector is nil, wait 1 seconds before taking the screenshot
20
+ :selector => nil # wait until the selector matches to take the screenshot
21
+ }
22
+
23
+ def self.browser
24
+ @browser ||= Capybara::Session.new :webkit
25
+ end
26
+
27
+ def browser
28
+ self.class.browser
29
+ end
30
+
31
+ def initialize options={}
32
+ raise MissingUrlError unless options[:url]
33
+
34
+ @options = DefaultOptions.merge(options)
35
+ end
36
+
37
+ def shoot!
38
+ @image = capture
39
+ end
40
+
41
+ def capture
42
+ browser.visit @options[:url]
43
+
44
+ if @options[:selector]
45
+ browser.wait_until do
46
+ browser.has_css? @options[:selector]
47
+ end
48
+ else
49
+ sleep @options[:wait]
50
+ end
51
+
52
+ @tempfile = Tempfile.new(['photograph','.png'])
53
+
54
+ browser.driver.render @tempfile.path,
55
+ :width => options[:w] + options[:x],
56
+ :height => options[:h] + options[:y]
57
+
58
+ @image = adjust_image
59
+ end
60
+
61
+ def adjust_image
62
+ image = MiniMagick::Image.read @tempfile
63
+
64
+ if options[:h] && options[:w]
65
+ image.crop "#{options[:w]}x#{options[:h]}+#{options[:x]}+#{options[:y]}"
66
+
67
+ image.write @tempfile
68
+
69
+ end
70
+
71
+ image
72
+ end
73
+
74
+ def clean!
75
+ @tempfile.unlink
76
+ end
77
+ end
78
+ end
79
+
@@ -0,0 +1,33 @@
1
+ require 'sinatra/base'
2
+ require 'sinatra/json'
3
+
4
+ module Photograph
5
+ # Preload the chrome instance
6
+ Artist.browser
7
+
8
+ class Service < ::Sinatra::Base
9
+ helpers Sinatra::JSON
10
+
11
+ get '/' do
12
+ json :version => Photograph::VERSION
13
+ end
14
+
15
+ get '/shoot' do
16
+ artist = Artist.new :url => params["url"],
17
+ :x => params["x"].to_i,
18
+ :y => params["y"].to_i,
19
+ :w => params["w"].to_i,
20
+ :h => params["h"].to_i,
21
+ :wait => params["wait"].to_f,
22
+ :selector => params["selector"]
23
+
24
+ artist.shoot!
25
+
26
+ send_file artist.image.path,
27
+ :type => :png
28
+
29
+ artist.clean!
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,3 @@
1
+ module Photograph
2
+ VERSION = "0.0.1"
3
+ end
data/lib/photograph.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "photograph/version"
2
+ require 'photograph/artist'
3
+ require 'photograph/service'
4
+
5
+ module Photograph
6
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/photograph/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["JH. Chabran"]
6
+ gem.email = ["jh@chabran.fr"]
7
+ gem.description = %q{Webservice that screenshots any url}
8
+ gem.summary = %q{Webservice that screenshots any url}
9
+ gem.homepage = "https://github.com/TactilizeTeam/photograph"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "photograph"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Photograph::VERSION
17
+
18
+ gem.add_dependency 'capybara-webkit'
19
+ gem.add_dependency 'mini_magick'
20
+ gem.add_dependency 'sinatra'
21
+ gem.add_dependency 'sinatra-contrib'
22
+ gem.add_dependency 'thin'
23
+
24
+ gem.add_development_dependency 'rspec', '~> 2.6'
25
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ module Photograph
4
+ describe Artist do
5
+ URL = "http://rubygems.org"
6
+
7
+ it "should instanciate a new artist" do
8
+ Artist.new :url => URL
9
+ end
10
+
11
+ it "should raise an error without an url" do
12
+ expect { Artist.new }.should raise_error(Artist::MissingUrlError)
13
+ end
14
+
15
+ describe "Default size values" do
16
+ before(:each) { @artist = Artist.new :url => URL }
17
+
18
+ it "should have default values for x,y : 0,0" do
19
+ @artist.options[:x].should == 0
20
+ @artist.options[:y].should == 0
21
+ end
22
+
23
+ it"should have default values for h,w : 1280, 1024" do
24
+ @artist.options[:w].should == 1280
25
+ @artist.options[:h].should == 1024
26
+ end
27
+ end
28
+
29
+ describe "Cropping" do
30
+ before(:each) { @artist = Artist.new :url => URL }
31
+
32
+ it "should take a screenshot large enough to crop later" do
33
+ pending
34
+
35
+ @artist = Artist.new :url => URL, :x => 200, :y => 100, :h => 400, :w => 400
36
+
37
+ Artist.browser.driver.stub :render
38
+
39
+ @artist.shoot!
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,4 @@
1
+ require 'photograph'
2
+
3
+ RSpec.configure do |config|
4
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: photograph
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - JH. Chabran
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: capybara-webkit
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
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: mini_magick
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
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: sinatra
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: sinatra-contrib
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: thin
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
+ - !ruby/object:Gem::Dependency
95
+ name: rspec
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: '2.6'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: '2.6'
110
+ description: Webservice that screenshots any url
111
+ email:
112
+ - jh@chabran.fr
113
+ executables:
114
+ - config.ru
115
+ - photograph
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - Gemfile
121
+ - LICENSE
122
+ - MIT-LICENSE
123
+ - README.md
124
+ - Rakefile
125
+ - bin/config.ru
126
+ - bin/photograph
127
+ - lib/photograph.rb
128
+ - lib/photograph/artist.rb
129
+ - lib/photograph/service.rb
130
+ - lib/photograph/version.rb
131
+ - photograph.gemspec
132
+ - spec/photograph/artist_spec.rb
133
+ - spec/spec_helper.rb
134
+ homepage: https://github.com/TactilizeTeam/photograph
135
+ licenses: []
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ segments:
147
+ - 0
148
+ hash: 4589063138671751824
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ! '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ segments:
156
+ - 0
157
+ hash: 4589063138671751824
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 1.8.23
161
+ signing_key:
162
+ specification_version: 3
163
+ summary: Webservice that screenshots any url
164
+ test_files:
165
+ - spec/photograph/artist_spec.rb
166
+ - spec/spec_helper.rb