screenshot_machine 0.0.1

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.
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 screenshot_machine.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Consti
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,79 @@
1
+ # ScreenshotMachine
2
+
3
+ A gem to create screenshots of webpages by using the [ScreenshotMachine.com](http://screenshotmachine.com) API.
4
+
5
+ You will need a (free) account to get started.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'screenshot_machine'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install screenshot_machine
22
+
23
+ ## Usage
24
+
25
+ Using ScreenshotMachine is quite simple
26
+
27
+ ```ruby
28
+ require 'screenshot_machine'
29
+
30
+ ScreenshotMachine.configure do |config|
31
+ config.key = '012345abcdefg'
32
+ end
33
+
34
+ website_url = "http://google.com/?q=great+gem"
35
+ sm = ScreenshotMachine::Generator.new(website_url)
36
+ # Returns a binary stream of the file
37
+ sm.screenshot
38
+ ```
39
+
40
+ You can also config all API Parameters
41
+
42
+ ```ruby
43
+ require 'screenshot_machine'
44
+
45
+ ScreenshotMachine.configure do |config|
46
+ config.key = '012345abcdefg'
47
+ config.size = 'X'
48
+ config.format = 'PNG'
49
+ config.cacheLimit = 2
50
+ config.timeout = 400
51
+ end
52
+ ```
53
+
54
+ ### Exceptions
55
+
56
+ ```ruby
57
+ # ScreenshotMachine::Exceptions::InvalidUrl
58
+ # Raised when the website_url can not be resolved by ScreenshotMachine.com
59
+ ScreenshotMachine::Generator.new("http://non-existing-example123.com").screenshot
60
+ # ScreenshotMachine::Exceptions::InvalidUrl: WARNING: Invalid URL
61
+
62
+ # ScreenshotMachine::Exceptions::InvalidKey
63
+ # Raised when your API key is not set or invalid
64
+ ScreenshotMachine::Generator.new("http://non-existing-example123.com").screenshot
65
+ # ScreenshotMachine::Exceptions::InvalidKey: ERROR: Invalid API KEY
66
+
67
+ # ScreenshotMachine::Exceptions::NoCredits
68
+ # Raised when there are not sufficient credits in your ScreenshotMachine.com account.
69
+ ScreenshotMachine::Generator.new("http://example.com").screenshot
70
+ # ScreenshotMachine::Exceptions::NoCredits: ERROR: No Credits for API
71
+ ```
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,18 @@
1
+ require "uri"
2
+ require "open-uri"
3
+ require "screenshot_machine/version"
4
+ require "screenshot_machine/configuration"
5
+
6
+ module ScreenshotMachine
7
+ extend Configuration
8
+
9
+ API_URL = "http://api.screenshotmachine.com/"
10
+
11
+ module Exceptions
12
+ class InvalidUrl < StandardError; end
13
+ class InvalidKey < StandardError; end
14
+ class NoCredits < StandardError; end
15
+ end
16
+ end
17
+
18
+ require "screenshot_machine/generator"
@@ -0,0 +1,41 @@
1
+ module ScreenshotMachine
2
+ module Configuration
3
+ # An array of valid keys in the options hash when configuring TweetStream.
4
+ VALID_PARAMS_KEYS = [
5
+ :size,
6
+ :format,
7
+ :cacheLimit,
8
+ :timeout,
9
+ :url,
10
+ :key].freeze
11
+
12
+ # @private
13
+ attr_accessor *VALID_PARAMS_KEYS
14
+
15
+ # When this module is extended, set all configuration options to their default values
16
+ def self.extended(base)
17
+ base.reset
18
+ end
19
+
20
+ # Convenience method to allow configuration options to be set in a block
21
+ def configure
22
+ yield self
23
+ end
24
+
25
+ # Create a hash of options and their values
26
+ def options
27
+ Hash[*VALID_PARAMS_KEYS.map {|key| [key, send(key)] }.flatten]
28
+ end
29
+
30
+ # Reset all configuration options to defaults
31
+ def reset
32
+ self.size = "L" # T, S, E, N, M, L, X, F
33
+ self.format = "JPG" # JPG, GIF, PNG
34
+ self.cacheLimit = 14 # 0-14 in days
35
+ self.timeout = 200 # 0, 200, 400, 600, 800, 1000 in ms
36
+ self.url = nil
37
+ self.key = nil
38
+ self
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,42 @@
1
+ module ScreenshotMachine
2
+ class Generator
3
+
4
+ attr_accessor *Configuration::VALID_PARAMS_KEYS
5
+
6
+ def initialize(url, options={})
7
+ merged_options = ScreenshotMachine.options.
8
+ merge({ :url => url }).
9
+ merge(options)
10
+ Configuration::VALID_PARAMS_KEYS.each do |key|
11
+ send("#{key}=", merged_options[key])
12
+ end
13
+ end
14
+
15
+ def screenshot
16
+ @screenshot ||= begin
17
+ response = open(screenshot_url)
18
+ case response.meta["x-screenshotmachine-response"]
19
+ when "invalid_url"
20
+ raise Exceptions::InvalidUrl, "WARNING: Invalid URL"
21
+ when "invalid_key"
22
+ raise Exceptions::InvalidKey, "ERROR: Invalid API KEY"
23
+ when "no_credits"
24
+ raise Exceptions::NoCredits, "ERROR: No Credits for API"
25
+ end
26
+ response.read
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def params
33
+ Hash[*Configuration::VALID_PARAMS_KEYS.map {|key| [key, send(key)] }.flatten]
34
+ end
35
+
36
+ def screenshot_url
37
+ uri = URI.parse(API_URL)
38
+ uri.query = URI.escape(params.map{|k,v| "#{k}=#{v}"}.join('&'))
39
+ uri.to_s
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module ScreenshotMachine
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/screenshot_machine/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Constantin Hofstetter"]
6
+ gem.email = ["consti@consti.de"]
7
+ gem.description = %q{Screenshot Machine Gem}
8
+ gem.summary = %q{This gem returns a screenshot of a webpage, using the ScreenshotMachine.com API (free account required).}
9
+ gem.homepage = ""
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 = "screenshot_machine"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ScreenshotMachine::VERSION
17
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: screenshot_machine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Constantin Hofstetter
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Screenshot Machine Gem
15
+ email:
16
+ - consti@consti.de
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - lib/screenshot_machine.rb
27
+ - lib/screenshot_machine/configuration.rb
28
+ - lib/screenshot_machine/generator.rb
29
+ - lib/screenshot_machine/version.rb
30
+ - screenshot_machine.gemspec
31
+ homepage: ''
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.24
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: This gem returns a screenshot of a webpage, using the ScreenshotMachine.com
55
+ API (free account required).
56
+ test_files: []