grimen-page_glimpse 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jonas Grimfelt
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,51 @@
1
+ h1. PAGE_GLIMPSE
2
+
3
+ A little Ruby wrapper for the *PageGlimpse API* for generating webpage snapshots - glimpses - in a snap.
4
+
5
+ h2. What Is PageGlimpse?
6
+
7
+ "http://pageglimpse.com":http://pageglimpse.com
8
+
9
+ h2. Installation
10
+
11
+ Dependency:
12
+
13
+ <pre>sudo gem install jnunemaker-httparty</pre>
14
+
15
+ Gem:
16
+
17
+ <pre>sudo gem install grimen-page_glimpse</pre>
18
+
19
+ h2. PageGlimpse API Key
20
+
21
+ You'll need a API key, which you can get for *free* here:
22
+
23
+ "http://pageglimpse.com/signup":http://pageglimpse.com/signup
24
+
25
+ h2. Usage/Examples
26
+
27
+ h3. Initialize
28
+
29
+ <pre>snapper = PageGlimse.new('ec0ccd....26df') # => nil</pre>
30
+
31
+ h3. Tell PageGlimpse to generate a webpage thumbnail
32
+
33
+ <pre>snapper.request!('http://mypage.com') # => true/false</pre>
34
+
35
+ h3. Check if thumbnail exists for a webpage
36
+
37
+ <pre>snapper.exists?('http://techcrunch.com', :size => :medium) # => true/false</pre>
38
+
39
+ h3. Get thumbnail (PNG image) for a webpage
40
+
41
+ <pre>snapper.thumbnail('http://techcrunch.com', :size => :small) # => [raw PNG data]</pre>
42
+
43
+ h3. Save thumbnail into a file (PNG image) for a webpage
44
+
45
+ <pre>snapper.save!('http://techcrunch.com', :size => :large) # => true/false</pre>
46
+
47
+ h3. More?
48
+
49
+ Yes, all the PageGlimpse API options are supported but not documented in this README right now. Peep the code and read more about the options: "http://www.pageglimpse.com/features/api":http://www.pageglimpse.com/features/api.
50
+
51
+ Copyright (c) Jonas Grimfelt, released under the MIT-license.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ NAME = "page_glimpse"
6
+ SUMMARY = %Q{A little Ruby wrapper for the PageGlimpse API (http://pageglimpse.com) for generating webpage snapshots - glimpses - in a snap.}
7
+ HOMEPAGE = "http://github.com/grimen/#{NAME}/tree/master"
8
+ AUTHOR = "Jonas Grimfelt"
9
+ EMAIL = "grimen@gmail.com"
10
+ SUPPORT_FILES = %w(README.textile)
11
+
12
+ begin
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gem|
15
+ gem.name = NAME
16
+ gem.summary = SUMMARY
17
+ gem.description = SUMMARY
18
+ gem.homepage = HOMEPAGE
19
+ gem.author = AUTHOR
20
+ gem.email = EMAIL
21
+
22
+ gem.require_paths = %w{lib}
23
+ gem.files = SUPPORT_FILES + %w(MIT-LICENSE Rakefile) + Dir.glob(File.join('{bin,lib,test,rails}', '**', '*'))
24
+ gem.executables = %w()
25
+ gem.extra_rdoc_files = SUPPORT_FILES
26
+ end
27
+ rescue LoadError
28
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
29
+ end
30
+
31
+ desc %Q{Run unit tests for "#{NAME}".}
32
+ task :default => :test
33
+
34
+ desc %Q{Run unit tests for "#{NAME}".}
35
+ Rake::TestTask.new(:test) do |test|
36
+ test.libs << 'lib'
37
+ test.pattern = 'test/**/*_test.rb'
38
+ test.verbose = true
39
+ end
40
+
41
+ desc %Q{Generate documentation for "#{NAME}".}
42
+ Rake::RDocTask.new(:rdoc) do |rdoc|
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = NAME
45
+ rdoc.options << '--line-numbers' << '--inline-source' << '--charset=UTF-8'
46
+ rdoc.rdoc_files.include(SUPPORT_FILES)
47
+ rdoc.rdoc_files.include(File.join('lib', '**', '*.rb'))
48
+ end
@@ -0,0 +1,131 @@
1
+ require 'rubygems'
2
+
3
+ gem 'httparty', '0.4.3'
4
+ require 'httparty'
5
+
6
+ class PageGlimse
7
+
8
+ InvalidAPIKeyError = Class.new(StandardError)
9
+
10
+ include HTTParty
11
+
12
+ PG_API_URL = 'http://images.pageglimpse.com/v1'.freeze
13
+ PG_VALID_SIZE_VALUES = [:small, :medium, :large].freeze
14
+ PG_VALID_ROOT_VALUES = [:yes, :no].freeze
15
+ PG_IMAGE_FORMAT = :png
16
+
17
+ ACTION_PARAMS = {
18
+ :thumbnails => [:devkey, :url, :size, :root, :nothumb],
19
+ :'thumbnails/exists' => [:devkey, :url, :size],
20
+ :'thumbnails/request' => [:devkey, :url]
21
+ }.freeze
22
+
23
+ DEFAULT_OPTIONS = {
24
+ :size => :medium,
25
+ :root => true,
26
+ :nothumb => nil
27
+ }.freeze
28
+
29
+ base_uri(PG_API_URL)
30
+
31
+ attr_accessor :defaults,
32
+ :devkey,
33
+ :url,
34
+ :size,
35
+ :root,
36
+ :nothumb
37
+
38
+ def initialize(devkey, defaults = {})
39
+ @defaults = DEFAULT_OPTIONS.merge(defaults)
40
+ @devkey = devkey
41
+ end
42
+
43
+ # Get single thumbnail.
44
+ def thumbnail(url, options = {})
45
+ options = defaults.merge(options)
46
+ @url = url
47
+ @size = valid_size_value(options[:size])
48
+ @root = valid_root_value(options[:root])
49
+ @nothumb = options[:nothumb]
50
+
51
+ query_url = generate_url('thumbnails')
52
+ response = self.class.get(query_url)
53
+ handle_response(response)
54
+ response.body
55
+ end
56
+ alias :thumbnails :thumbnail
57
+
58
+ # Export single thumbnail into a file.
59
+ def save!(url, path, options = {})
60
+ begin
61
+ data = thumbnail(url, options)
62
+ path = File.join(path, "#{url.gsub(/(http:)|[\/]/, '')}.#{PG_IMAGE_FORMAT}") if File.directory?(path)
63
+ File.new(path, 'w')
64
+ File.open(path, 'w') { |f| f.write(data) }
65
+ rescue
66
+ false
67
+ else
68
+ true
69
+ end
70
+ end
71
+
72
+ # Request URL to be captured.
73
+ def request!(url, options = {})
74
+ options = defaults.merge(options)
75
+ @url = url
76
+
77
+ query_url = generate_url('thumbnails', 'request')
78
+ response = self.class.get(query_url)
79
+ handle_response(response.code)
80
+ end
81
+
82
+ # Check if a thumbnail exists
83
+ def exists?(url, options = {})
84
+ options = defaults.merge(options)
85
+ @url = url
86
+ @size = valid_size_value(options[:size])
87
+
88
+ query_url = generate_url('thumbnails', 'exists')
89
+ response = self.class.get(query_url)
90
+ handle_response(response.code)
91
+ end
92
+ alias :exist? :exists?
93
+
94
+ private
95
+
96
+ # Return value based on response code.
97
+ def handle_response(response_code)
98
+ case response_code.to_i
99
+ when 200 then
100
+ true
101
+ when 404 then
102
+ false
103
+ when 403 then
104
+ raise InvalidAPIKeyError, "Error: Invalid PageGlimse API key."
105
+ end
106
+ end
107
+
108
+ # Validate/Format value for "root"-param.
109
+ def valid_root_value(root)
110
+ root ||= true
111
+ (root == true) ? :yes : :no
112
+ end
113
+
114
+ # Validate/Format value for "size"-param.
115
+ def valid_size_value(size)
116
+ PG_VALID_SIZE_VALUES.include?(size.to_sym) ? size : :medium
117
+ end
118
+
119
+ # Generate API query URL base on specified args.
120
+ def generate_url(*args)
121
+ action = args.join('/').to_sym
122
+ params = []
123
+ params << "url=#{@url}" if ACTION_PARAMS[action].include?(:url)
124
+ params << "size=#{@size}" if ACTION_PARAMS[action].include?(:size)
125
+ params << "root=#{@root}" if ACTION_PARAMS[action].include?(:thumbnail)
126
+ params << "nothumb=#{@nothumb}" if ACTION_PARAMS[action].include?(:nothumb) && @nothumb && !@nothumb.empty?
127
+ params << "devkey=#{@devkey}" if ACTION_PARAMS[action].include?(:devkey)
128
+ "/#{action}?#{params.join('&')}"
129
+ end
130
+
131
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), 'lib', '**', '*.rb'))).uniq.each do |file|
2
+ require file
3
+ end
@@ -0,0 +1,17 @@
1
+ require 'test/unit'
2
+ require 'shoulda'
3
+
4
+ class PageGlimpseTest < Test::Unit::TestCase
5
+
6
+ VALID_API_KEY = 'ec0ccd30be1b39393d19cb5f410d26df'
7
+ INVALID_API_KEY = 'ec0ccd30be1b39393d19cb5f410d26d'
8
+
9
+ def setup
10
+ @glimpser = PageGlimpse.new(TEST_API_KEY)
11
+ end
12
+
13
+ should "be true" do
14
+ assert true
15
+ end
16
+
17
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grimen-page_glimpse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonas Grimfelt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A little Ruby wrapper for the PageGlimpse API (http://pageglimpse.com) for generating webpage snapshots - glimpses - in a snap.
17
+ email: grimen@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.textile
24
+ files:
25
+ - MIT-LICENSE
26
+ - README.textile
27
+ - Rakefile
28
+ - lib/page_glimse.rb
29
+ - rails/init.rb
30
+ - test/page_glimse_test.rb
31
+ has_rdoc: false
32
+ homepage: http://github.com/grimen/page_glimpse/tree/master
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --charset=UTF-8
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.2.0
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: A little Ruby wrapper for the PageGlimpse API (http://pageglimpse.com) for generating webpage snapshots - glimpses - in a snap.
57
+ test_files:
58
+ - test/page_glimse_test.rb