page_glimpse 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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ 0.0.1 - Current
2
+
3
+ * Initial public release based on Relax 0.1.1 or later.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Nathaniel E. Bibler
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.rdoc ADDED
@@ -0,0 +1,12 @@
1
+ = PageGlimpse
2
+
3
+ {PageGlimpse.com}[http://www.pageglimpse.com] is a SaaS provider which has a
4
+ REST API for downloading website thumbnails by URI. This library wraps that
5
+ interface to provide a Ruby-like means of interacting with it.
6
+
7
+ <b>Note:</b> The author of this gem is entirely unaffiliated with the PageGlimpse.com web service.
8
+
9
+ == Copyright
10
+
11
+ Copyright (c) 2009 Nathaniel E. Bibler. Released under the MIT License.
12
+ See the LICENSE file for more details.
data/Rakefile ADDED
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "page_glimpse"
8
+ gem.summary = %Q{A Ruby library for the PageGlimpse.com service}
9
+ gem.email = "gem@nathanielbibler.com"
10
+ gem.homepage = "http://github.com/nbibler/page_glimpse"
11
+ gem.authors = ["Nathaniel Bibler"]
12
+ gem.rubyforge_project = 'page-glimpse'
13
+
14
+ gem.add_dependency('relax', '~> 0.1.1')
15
+ gem.add_dependency('json', '~> 1.1.6')
16
+
17
+ gem.add_development_dependency('thoughtbot-shoulda', '~> 2.10.1')
18
+ gem.add_development_dependency('mocha', '~> 0.9.5')
19
+ gem.add_development_dependency('fakeweb', '~> 1.2.2')
20
+ end
21
+
22
+ rescue LoadError
23
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
24
+ end
25
+
26
+ require 'rake/testtask'
27
+ Rake::TestTask.new(:test) do |test|
28
+ test.libs << 'lib' << 'test'
29
+ test.pattern = 'test/**/*_test.rb'
30
+ test.verbose = true
31
+ end
32
+
33
+ begin
34
+ require 'rcov/rcovtask'
35
+ Rcov::RcovTask.new do |test|
36
+ test.libs << 'test'
37
+ test.pattern = 'test/**/*_test.rb'
38
+ test.verbose = true
39
+ end
40
+ rescue LoadError
41
+ task :rcov do
42
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
43
+ end
44
+ end
45
+
46
+
47
+ task :default => :test
48
+
49
+ require 'rake/rdoctask'
50
+ Rake::RDocTask.new do |rdoc|
51
+ if File.exist?('VERSION.yml')
52
+ config = YAML.load(File.read('VERSION.yml'))
53
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
54
+ else
55
+ version = ""
56
+ end
57
+
58
+ rdoc.rdoc_dir = 'rdoc'
59
+ rdoc.title = "page_glimpse #{version}"
60
+ rdoc.rdoc_files.include('README*')
61
+ rdoc.rdoc_files.include('lib/**/*.rb')
62
+ end
63
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 1
3
+ :major: 0
4
+ :minor: 0
@@ -0,0 +1,41 @@
1
+ require 'page_glimpse/exceptions'
2
+ require 'page_glimpse/image_parser'
3
+ require 'page_glimpse/json_parser'
4
+
5
+ module PageGlimpse
6
+
7
+ class API < Relax::Service
8
+
9
+ defaults do
10
+ parameter :devkey, :required => true
11
+ parameter :url, :required => true
12
+ end
13
+
14
+ endpoint 'http://images.pageglimpse.com/v1' do
15
+
16
+ action :thumbnail, :url => '/thumbnails' do
17
+ parameter :size
18
+ parameter :root
19
+ parameter :nothumb
20
+
21
+ parser ImageParser do
22
+ end
23
+ end
24
+
25
+ action :queue, :url => '/thumbnails/request' do
26
+ parser JsonParser do
27
+ end
28
+ end
29
+
30
+ action :exists?, :url => '/thumbnails/exists' do
31
+ parameter :size
32
+
33
+ parser JsonParser do
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,7 @@
1
+ module PageGlimpse
2
+
3
+ class Exception < RuntimeError; end
4
+
5
+ class InvalidDeveloperKeyError < Exception; end
6
+
7
+ end
@@ -0,0 +1,12 @@
1
+ module PageGlimpse
2
+
3
+ class Image
4
+ attr_accessor :content_type,
5
+ :content_length,
6
+ :content,
7
+ :filename
8
+
9
+ alias :body :content
10
+ end
11
+
12
+ end
@@ -0,0 +1,31 @@
1
+ require 'page_glimpse/image'
2
+
3
+ module PageGlimpse
4
+
5
+ class ImageParser
6
+
7
+ def initialize(options = {}, &block)
8
+ end
9
+
10
+ def parse(response)
11
+ image = Image.new
12
+ image.content_type = response.headers[:content_type]
13
+ image.filename = parse_filename(response.headers[:content_disposition])
14
+ image.content_length = (response.headers[:content_length] || 0).to_i
15
+ image.content = response.to_s
16
+ image
17
+ end
18
+
19
+
20
+ private
21
+
22
+
23
+ def parse_filename(disposition)
24
+ if disposition =~ /filename="([^"]*)"/i
25
+ $1
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,17 @@
1
+ gem 'json', '~> 1.1.6'
2
+ require 'json'
3
+
4
+ module PageGlimpse
5
+
6
+ class JsonParser
7
+
8
+ def initialize(options = {}, &block)
9
+ end
10
+
11
+ def parse(response)
12
+ JSON.parse response
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,72 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
2
+
3
+ gem 'relax' #, '~>0.1.0'
4
+ require 'relax'
5
+
6
+ require 'page_glimpse/api'
7
+
8
+ module PageGlimpse
9
+
10
+ THUMBNAIL_EXISTS = 'yes'
11
+ QUEUE_SUCCESS = 'success'
12
+
13
+ ##
14
+ # Sets the developer key to use with the requests.
15
+ #
16
+ def self.developer_key=(key)
17
+ @@developer_key = key
18
+ end
19
+
20
+ ##
21
+ # Returns +true+ if the thumbnail exists on PageGlimpse, +false+ otherwise.
22
+ #
23
+ def self.exists?(url, options = {})
24
+ options[:url] = url
25
+ response = api.exists?(options)
26
+ response.kind_of?(Array) && response.size == 2 && response[1] == THUMBNAIL_EXISTS
27
+ rescue RestClient::ResourceNotFound
28
+ return false
29
+ rescue RestClient::RequestFailed
30
+ handle_failure($!)
31
+ end
32
+
33
+ ##
34
+ # Returns the binary data for the thumbnail requested.
35
+ #
36
+ def self.get(url, options = {})
37
+ options[:url] = url
38
+ exists?(url, options) ? api.thumbnail(options) : nil
39
+ rescue RestClient::RequestFailed
40
+ handle_failure($!)
41
+ end
42
+
43
+ def self.queue(url)
44
+ response = api.queue(:url => url)
45
+ response.kind_of?(Array) && response.size == 2 && response[1] == QUEUE_SUCCESS
46
+ rescue RestClient::RequestFailed
47
+ handle_failure($!)
48
+ end
49
+
50
+
51
+ private
52
+
53
+
54
+ def self.developer_key #:nodoc:
55
+ @@developer_key
56
+ end
57
+
58
+ def self.api #:nodoc:
59
+ @@api ||= API.new(:devkey => self.developer_key)
60
+ end
61
+
62
+ def self.handle_failure(exception)
63
+ raise exception unless exception.respond_to?(:http_code)
64
+ case exception.http_code
65
+ when 403
66
+ raise InvalidDeveloperKeyError, "Check your developer key: it was either mistyped or has been invalidated"
67
+ else
68
+ raise exception
69
+ end
70
+ end
71
+
72
+ end
@@ -0,0 +1,73 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{page_glimpse}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Nathaniel Bibler"]
9
+ s.date = %q{2009-06-04}
10
+ s.email = %q{gem@nathanielbibler.com}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "CHANGELOG.rdoc",
19
+ "LICENSE",
20
+ "README.rdoc",
21
+ "Rakefile",
22
+ "VERSION.yml",
23
+ "lib/page_glimpse.rb",
24
+ "lib/page_glimpse/api.rb",
25
+ "lib/page_glimpse/exceptions.rb",
26
+ "lib/page_glimpse/image.rb",
27
+ "lib/page_glimpse/image_parser.rb",
28
+ "lib/page_glimpse/json_parser.rb",
29
+ "page_glimpse.gemspec",
30
+ "test/integrations/page_glimpse_test.rb",
31
+ "test/mocks/page_glimpse.rb",
32
+ "test/test_helper.rb",
33
+ "test/units/page_glimpse_test.rb"
34
+ ]
35
+ s.has_rdoc = true
36
+ s.homepage = %q{http://github.com/nbibler/page_glimpse}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubyforge_project = %q{page-glimpse}
40
+ s.rubygems_version = %q{1.3.2}
41
+ s.summary = %q{A Ruby library for the PageGlimpse.com service}
42
+ s.test_files = [
43
+ "test/integrations/page_glimpse_test.rb",
44
+ "test/mocks/page_glimpse.rb",
45
+ "test/test_helper.rb",
46
+ "test/units/page_glimpse_test.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
54
+ s.add_runtime_dependency(%q<relax>, ["~> 0.1.1"])
55
+ s.add_runtime_dependency(%q<json>, ["~> 1.1.6"])
56
+ s.add_development_dependency(%q<thoughtbot-shoulda>, ["~> 2.10.1"])
57
+ s.add_development_dependency(%q<mocha>, ["~> 0.9.5"])
58
+ s.add_development_dependency(%q<fakeweb>, ["~> 1.2.2"])
59
+ else
60
+ s.add_dependency(%q<relax>, ["~> 0.1.1"])
61
+ s.add_dependency(%q<json>, ["~> 1.1.6"])
62
+ s.add_dependency(%q<thoughtbot-shoulda>, ["~> 2.10.1"])
63
+ s.add_dependency(%q<mocha>, ["~> 0.9.5"])
64
+ s.add_dependency(%q<fakeweb>, ["~> 1.2.2"])
65
+ end
66
+ else
67
+ s.add_dependency(%q<relax>, ["~> 0.1.1"])
68
+ s.add_dependency(%q<json>, ["~> 1.1.6"])
69
+ s.add_dependency(%q<thoughtbot-shoulda>, ["~> 2.10.1"])
70
+ s.add_dependency(%q<mocha>, ["~> 0.9.5"])
71
+ s.add_dependency(%q<fakeweb>, ["~> 1.2.2"])
72
+ end
73
+ end
@@ -0,0 +1,69 @@
1
+ require 'test_helper'
2
+
3
+ class PageGlimpseTest < Test::Unit::TestCase
4
+
5
+ context 'PageGlimpse' do
6
+
7
+ context 'exists?' do
8
+
9
+ should 'be true for thumbnails which exist' do
10
+ assert PageGlimpse.exists?('http://goodurl.local/', :size => 'large')
11
+ end
12
+
13
+ should 'be false for thumbnails which do not exist' do
14
+ assert !PageGlimpse.exists?('http://badurl.local/', :size => 'large')
15
+ end
16
+
17
+ end
18
+
19
+ context 'get' do
20
+
21
+ context 'with a thumbnail available' do
22
+
23
+ setup do
24
+ @thumbnail = PageGlimpse.get('http://goodurl.local/', :size => 'large')
25
+ end
26
+
27
+ should 'return an Image' do
28
+ assert_kind_of PageGlimpse::Image, @thumbnail
29
+ end
30
+
31
+ should 'have a content type' do
32
+ assert_equal 'image/jpeg', @thumbnail.content_type
33
+ end
34
+
35
+ should 'have a content length' do
36
+ assert_equal 3124, @thumbnail.content_length
37
+ end
38
+
39
+ should 'have content' do
40
+ assert_not_nil @thumbnail.content
41
+ end
42
+
43
+ should 'have a filename' do
44
+ assert_equal 'invalid_devkey.150x108.jpg', @thumbnail.filename
45
+ end
46
+
47
+ end
48
+
49
+ context 'with no thumbnail available' do
50
+
51
+ should 'return nil' do
52
+ assert_nil PageGlimpse.get('http://badurl.local/', :size => 'large')
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+
59
+ context 'queue' do
60
+
61
+ should 'return true' do
62
+ assert PageGlimpse.queue('http://goodurl.local/')
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ end
Binary file
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'fakeweb'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+
9
+ require 'mocks/page_glimpse'
10
+ require 'page_glimpse'
11
+
12
+ DEVELOPER_KEY = 'testdeveloperkey123'
13
+ PageGlimpse.developer_key = DEVELOPER_KEY
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class PageGlimpseTest < Test::Unit::TestCase
4
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: page_glimpse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nathaniel Bibler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-04 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: relax
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 0.1.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.6
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: thoughtbot-shoulda
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 2.10.1
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: mocha
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.9.5
54
+ version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: fakeweb
57
+ type: :development
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ~>
62
+ - !ruby/object:Gem::Version
63
+ version: 1.2.2
64
+ version:
65
+ description:
66
+ email: gem@nathanielbibler.com
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - LICENSE
73
+ - README.rdoc
74
+ files:
75
+ - .document
76
+ - .gitignore
77
+ - CHANGELOG.rdoc
78
+ - LICENSE
79
+ - README.rdoc
80
+ - Rakefile
81
+ - VERSION.yml
82
+ - lib/page_glimpse.rb
83
+ - lib/page_glimpse/api.rb
84
+ - lib/page_glimpse/exceptions.rb
85
+ - lib/page_glimpse/image.rb
86
+ - lib/page_glimpse/image_parser.rb
87
+ - lib/page_glimpse/json_parser.rb
88
+ - page_glimpse.gemspec
89
+ - test/integrations/page_glimpse_test.rb
90
+ - test/mocks/page_glimpse.rb
91
+ - test/test_helper.rb
92
+ - test/units/page_glimpse_test.rb
93
+ has_rdoc: true
94
+ homepage: http://github.com/nbibler/page_glimpse
95
+ licenses: []
96
+
97
+ post_install_message:
98
+ rdoc_options:
99
+ - --charset=UTF-8
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: "0"
107
+ version:
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: "0"
113
+ version:
114
+ requirements: []
115
+
116
+ rubyforge_project: page-glimpse
117
+ rubygems_version: 1.3.2
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: A Ruby library for the PageGlimpse.com service
121
+ test_files:
122
+ - test/integrations/page_glimpse_test.rb
123
+ - test/mocks/page_glimpse.rb
124
+ - test/test_helper.rb
125
+ - test/units/page_glimpse_test.rb