cloudprint 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.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source "http://rubygems.org"
2
+ gem "oauth2"
3
+ #gem "curb"
4
+ gem "faraday"
5
+ gem "json"
6
+
7
+ group :development do
8
+ gem "bundler", "~> 1.0.0"
9
+ gem "jeweler", "~> 1.6.4"
10
+ end
11
+
12
+ group :test do
13
+ gem "test-unit", ">= 0"
14
+ gem "net-http-spy"
15
+ gem "rcov", ">= 0"
16
+ gem "mocha", ">= 0"
17
+ gem "ruby-debug"
18
+ end
@@ -0,0 +1,51 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.2.6)
5
+ columnize (0.3.6)
6
+ faraday (0.7.6)
7
+ addressable (~> 2.2)
8
+ multipart-post (~> 1.1)
9
+ rack (~> 1.1)
10
+ git (1.2.5)
11
+ jeweler (1.6.4)
12
+ bundler (~> 1.0)
13
+ git (>= 1.2.5)
14
+ rake
15
+ json (1.6.5)
16
+ linecache (0.46)
17
+ rbx-require-relative (> 0.0.4)
18
+ metaclass (0.0.1)
19
+ mocha (0.10.4)
20
+ metaclass (~> 0.0.1)
21
+ multi_json (1.0.4)
22
+ multipart-post (1.1.5)
23
+ net-http-spy (0.2.1)
24
+ oauth2 (0.5.2)
25
+ faraday (~> 0.7)
26
+ multi_json (~> 1.0)
27
+ rack (1.4.1)
28
+ rake (0.9.2.2)
29
+ rbx-require-relative (0.0.5)
30
+ rcov (1.0.0)
31
+ ruby-debug (0.10.4)
32
+ columnize (>= 0.1)
33
+ ruby-debug-base (~> 0.10.4.0)
34
+ ruby-debug-base (0.10.4)
35
+ linecache (>= 0.3)
36
+ test-unit (2.4.7)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ bundler (~> 1.0.0)
43
+ faraday
44
+ jeweler (~> 1.6.4)
45
+ json
46
+ mocha
47
+ net-http-spy
48
+ oauth2
49
+ rcov
50
+ ruby-debug
51
+ test-unit
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Eugen Minciu
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.
@@ -0,0 +1,20 @@
1
+ = cloudprint
2
+
3
+ This library provides a ruby-esque interface to Google Cloud Print.
4
+ cloudprint is a work in progress. I'll be adding documentation once all the basic GCP functionality is supported.
5
+
6
+ == Contributing to cloudprint
7
+
8
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
9
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
10
+ * Fork the project
11
+ * Start a feature/bugfix branch
12
+ * Commit and push until you are happy with your contribution
13
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
14
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2012 Eugen Minciu. See LICENSE.txt for
19
+ further details.
20
+
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "cloudprint"
18
+ gem.homepage = "http://github.com/minciue/cloudprint"
19
+ #noinspection RubyResolve
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{This library provides a ruby-esque interface to Google Cloud Print.}
22
+ gem.description = %Q{This library provides a ruby-esque interface to Google Cloud Print.\ncloudprint is a work in progress. I'll be adding documentation once all the basic GCP functionality is supported.}
23
+ gem.email = "eugen@lesseverything.com"
24
+ #noinspection RubyResolve
25
+ gem.authors = ["Eugen Minciu"]
26
+ # dependencies defined in Gemfile
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rake/testtask'
31
+ Rake::TestTask.new(:test) do |test|
32
+ test.libs << 'lib' << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+
37
+ #require 'rcov/rcovtask'
38
+ #Rcov::RcovTask.new do |test|
39
+ # test.libs << 'test'
40
+ # test.pattern = 'test/**/test_*.rb'
41
+ # test.verbose = true
42
+ # test.rcov_opts << '--exclude "gems/*"'
43
+ #end
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "cloudprint #{version}"
53
+ #noinspection RubyResolve
54
+ rdoc.rdoc_files.include('README*')
55
+ #noinspection RubyResolve
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{cloudprint}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Eugen Minciu"]
12
+ s.date = %q{2012-02-14}
13
+ s.description = %q{This library provides a ruby-esque interface to Google Cloud Print.
14
+ cloudprint is a work in progress. I'll be adding documentation once all the basic GCP functionality is supported.}
15
+ s.email = %q{eugen@lesseverything.com}
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "cloudprint.gemspec",
29
+ "lib/cloudprint.rb",
30
+ "lib/cloudprint/connection.rb",
31
+ "lib/cloudprint/printer.rb",
32
+ "test/cloudprint_test.rb",
33
+ "test/connection_test.rb",
34
+ "test/helper.rb",
35
+ "test/printer_test.rb"
36
+ ]
37
+ s.homepage = %q{http://github.com/minciue/cloudprint}
38
+ s.licenses = ["MIT"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.4.2}
41
+ s.summary = %q{This library provides a ruby-esque interface to Google Cloud Print.}
42
+
43
+ if s.respond_to? :specification_version then
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<oauth2>, [">= 0"])
48
+ s.add_runtime_dependency(%q<faraday>, [">= 0"])
49
+ s.add_runtime_dependency(%q<json>, [">= 0"])
50
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
51
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
52
+ else
53
+ s.add_dependency(%q<oauth2>, [">= 0"])
54
+ s.add_dependency(%q<faraday>, [">= 0"])
55
+ s.add_dependency(%q<json>, [">= 0"])
56
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
57
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<oauth2>, [">= 0"])
61
+ s.add_dependency(%q<faraday>, [">= 0"])
62
+ s.add_dependency(%q<json>, [">= 0"])
63
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
64
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
65
+ end
66
+ end
67
+
@@ -0,0 +1,72 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ Bundler.require(:default)
4
+
5
+ require "cloudprint/printer"
6
+ require "cloudprint/connection"
7
+
8
+
9
+ module CloudPrint
10
+ def self.setup(options = {})
11
+ @refresh_token = options[:refresh_token]
12
+ @client_id = options[:client_id]
13
+ @client_secret = options[:client_secret]
14
+ @callback_url = options[:callback_url]
15
+ end
16
+
17
+ def self.client_secret
18
+ @client_secret
19
+ end
20
+
21
+ def self.client_id
22
+ @client_id
23
+ end
24
+
25
+ def self.refresh_token
26
+ @refresh_token
27
+ end
28
+
29
+ def self.callback_url
30
+ @callback_url
31
+ end
32
+
33
+ def self.connection
34
+ @connection ||= Connection.new
35
+ end
36
+
37
+
38
+ def self.access_token
39
+ if access_token_valid?
40
+ get_existing_access_token.token
41
+ else
42
+ get_new_access_token.token
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def self.access_token_valid?
49
+ get_existing_access_token != nil && get_existing_access_token.token && get_existing_access_token.token.strip != "" && !get_existing_access_token.expired?
50
+ end
51
+
52
+ def self.get_existing_access_token
53
+ @oauth2_access_token
54
+ end
55
+
56
+ def self.get_new_access_token
57
+ @oauth2_access_token = new_oauth2_access_token
58
+ @oauth2_access_token
59
+ end
60
+
61
+ def self.oauth_client
62
+ @oauth_client ||= OAuth2::Client.new(CloudPrint.client_id, CloudPrint.client_secret,
63
+ :authorize_url => "/o/oauth2/auth",
64
+ :token_url => "/o/oauth2/token",
65
+ :access_token_url => "/o/oauth2/token",
66
+ :site => 'https://accounts.google.com/')
67
+ end
68
+
69
+ def self.new_oauth2_access_token
70
+ OAuth2::AccessToken.new(oauth_client, "", :refresh_token => CloudPrint.refresh_token).refresh!
71
+ end
72
+ end
@@ -0,0 +1,86 @@
1
+ require 'ostruct'
2
+ require 'faraday/utils'
3
+ require "net/https"
4
+ require "uri"
5
+
6
+ OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
7
+ module CloudPrint
8
+ class Connection
9
+ def get(path, params = {})
10
+ response = request(:get, path, params)
11
+ parse_response(response)
12
+ end
13
+
14
+ def post(path, params = {})
15
+ response = request(:post, path, params)
16
+ parse_response(response)
17
+ end
18
+
19
+ private
20
+
21
+ def parse_response(response)
22
+ begin
23
+ JSON.parse(response.body)
24
+ rescue => e
25
+ puts response.body
26
+ raise e
27
+ end
28
+ end
29
+
30
+ def request(method, path, params)
31
+ url = full_url_for(path)
32
+ response = make_http_request(:method => method, :url => url, :params => params)
33
+ end
34
+
35
+ def make_http_request(options = {})
36
+ method = options[:method]
37
+ uri = URI.parse(options[:url])
38
+ http = build_http_connection(uri)
39
+ request = build_request(:method => method, :uri => uri, :params => options[:params])
40
+
41
+ http.request(request)
42
+ end
43
+
44
+ def build_request(options)
45
+ method = options[:method]
46
+ uri = options[:uri]
47
+
48
+ request = case method
49
+ when :post
50
+ req = Net::HTTP::Post.new(uri.request_uri)
51
+ req.set_form_data(options[:params])
52
+ req
53
+ else
54
+ req = Net::HTTP::Get.new(build_get_uri(uri.request_uri, options[:params]))
55
+ end
56
+
57
+ set_request_headers(request)
58
+ request
59
+ end
60
+
61
+ def set_request_headers(request)
62
+ request['Authorization'] = "OAuth " + CloudPrint.access_token
63
+ request['X-CloudPrint-Proxy'] = 'api-prober'
64
+ end
65
+
66
+ def build_get_uri(uri, params = {})
67
+ unescaped_params = params.map { |key,val| "#{key}=#{val}"}.join("&")
68
+ escaped_params = URI.escape(unescaped_params)
69
+
70
+ escaped_params = "?#{escaped_params}" unless escaped_params.empty?
71
+ uri + escaped_params
72
+ end
73
+
74
+ def build_http_connection(uri)
75
+ http = Net::HTTP.new(uri.host, uri.port)
76
+
77
+ http.use_ssl = true
78
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
79
+ http
80
+ end
81
+
82
+ def full_url_for(path)
83
+ "https://www.google.com/cloudprint" + path
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,33 @@
1
+ module CloudPrint
2
+ class Printer
3
+ attr_reader :id, :status, :name
4
+ def initialize(options = {})
5
+ @id = options[:id]
6
+ @status = options[:status]
7
+ @name = options[:name]
8
+ end
9
+
10
+ def print(options)
11
+ CloudPrint.connection.post('/submit', :printerid => self.id, :title => options[:title], :content => options[:content], :contentType => options[:content_type])
12
+ end
13
+
14
+ class << self
15
+ def find(printer_id)
16
+ response = CloudPrint.connection.get('/printer', :printerid => printer_id)
17
+ first_printer_hash = response['printers'].first
18
+ new_from_hash(first_printer_hash)
19
+ end
20
+
21
+ def all
22
+ response = CloudPrint.connection.get('/search')
23
+ response['printers'].map { |p| new_from_hash(p) }
24
+ end
25
+
26
+ private
27
+
28
+ def new_from_hash(hash)
29
+ Printer.new(:id => hash['id'], :status => hash['status'], :name => hash['name'])
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,118 @@
1
+ require "test/unit"
2
+ require "cloudprint"
3
+
4
+ class CloudPrintTest < Test::Unit::TestCase
5
+ def setup
6
+ stub_access_token
7
+ end
8
+
9
+ test "CloudPrint exists" do
10
+ assert_nothing_raised { CloudPrint }
11
+ end
12
+
13
+ test "CloudPrint can be set up" do
14
+ assert CloudPrint.respond_to?(:setup)
15
+ end
16
+
17
+ test "CloudPrint setup stores a client id, client secret, callback URL and refresh token" do
18
+ CloudPrint.setup(:refresh_token => 'refresh_token', :client_id => 'client_id', :client_secret => 'client_secret', :callback_url => 'callback_url')
19
+ assert_equal 'client_id', CloudPrint.client_id
20
+ assert_equal 'client_secret', CloudPrint.client_secret
21
+ assert_equal 'refresh_token', CloudPrint.refresh_token
22
+ assert_equal 'callback_url', CloudPrint.callback_url
23
+ end
24
+
25
+ test "connections will check if the access token is still valid" do
26
+ CloudPrint.expects(:access_token_valid?).returns(true)
27
+ CloudPrint.stubs(:get_existing_access_token).returns(mock_access_token)
28
+ CloudPrint.access_token
29
+ end
30
+
31
+ test "connections getting the existing access token use it if it's still valid" do
32
+ token = mock_access_token
33
+ token.expects(:token)
34
+
35
+ CloudPrint.stubs(:access_token_valid?).returns(true)
36
+ CloudPrint.expects(:get_existing_access_token).once.returns(token)
37
+ CloudPrint.expects(:get_new_access_token).never
38
+ CloudPrint.access_token
39
+ end
40
+
41
+ test "connections getting the existing access token get a new one if it's no longer valid" do
42
+ CloudPrint.stubs(:access_token_valid?).returns(false)
43
+ CloudPrint.expects(:get_new_access_token).returns(mock_access_token)
44
+ CloudPrint.access_token
45
+ end
46
+
47
+ test "access token may not be nil" do
48
+ CloudPrint.expects(:get_existing_access_token).returns(nil)
49
+ CloudPrint.expects(:get_new_access_token).returns(mock_access_token)
50
+ CloudPrint.access_token
51
+ end
52
+
53
+ test "access token may not have a nil token" do
54
+ CloudPrint.expects(:get_existing_access_token).at_least_once.returns(mock_access_token(nil))
55
+ CloudPrint.expects(:get_new_access_token).returns(mock_access_token)
56
+ CloudPrint.access_token
57
+ end
58
+
59
+ test "access token may not be a blank string" do
60
+ CloudPrint.expects(:get_existing_access_token).at_least_once.returns(mock_access_token(' '))
61
+ CloudPrint.expects(:get_new_access_token).returns(mock_access_token)
62
+ CloudPrint.access_token
63
+ end
64
+
65
+ test "access token can be a string" do
66
+ CloudPrint.stubs(:access_token_valid?).returns(true)
67
+ CloudPrint.expects(:get_new_access_token).never
68
+ CloudPrint.expects(:get_existing_access_token).at_least_once.returns(mock_access_token)
69
+ assert_equal("token", CloudPrint.access_token)
70
+ end
71
+
72
+ test "getting a new access token initializes an OAuth2 client" do
73
+ CloudPrint.stubs(:access_token_valid?).returns(false)
74
+ CloudPrint.expects(:oauth_client)
75
+ CloudPrint.access_token
76
+ end
77
+
78
+ test "setting up an oauth client" do
79
+ CloudPrint.setup(:client_id => 'client_id', :client_secret => 'client_secret', :callback_url => "http://test.com/callback")
80
+ CloudPrint.stubs(:access_token_valid?).returns(false)
81
+ OAuth2::Client.expects(:new).with('client_id', 'client_secret',
82
+ :authorize_url => "/o/oauth2/auth",
83
+ :token_url => "/o/oauth2/token",
84
+ :access_token_url => "/o/oauth2/token",
85
+ :site => 'https://accounts.google.com/')
86
+
87
+ CloudPrint.access_token
88
+ end
89
+
90
+ test "getting a new access token should initialize an access token" do
91
+ CloudPrint.setup(:refresh_token => "refresh_token")
92
+ CloudPrint.stubs(:access_token_valid?).returns(false)
93
+
94
+ CloudPrint.expects(:new_oauth2_access_token).returns(mock_access_token)
95
+ CloudPrint.access_token
96
+ end
97
+
98
+ test "getting an auth token from oauth2" do
99
+ token = mock_access_token
100
+ CloudPrint.setup(:refresh_token => "refresh_token")
101
+ CloudPrint.stubs(:access_token_valid?).returns(false)
102
+ stub_oauth_client
103
+
104
+ OAuth2::AccessToken.expects(:new).with(mock_oauth_client, "", :refresh_token => "refresh_token").returns(token)
105
+ token.expects(:refresh!).returns(mock_access_token)
106
+ CloudPrint.access_token
107
+ end
108
+
109
+ test "the access token is invalid if it has expired" do
110
+ token = mock_access_token
111
+ token.stubs(:expired?).returns(true)
112
+ CloudPrint.stubs(:get_existing_access_token).returns()
113
+ stub_oauth_client
114
+
115
+ CloudPrint.expects(:get_new_access_token).returns(token)
116
+ CloudPrint.access_token
117
+ end
118
+ end
@@ -0,0 +1,95 @@
1
+ require "helper"
2
+
3
+ class ConnectionTest < Test::Unit::TestCase
4
+ def setup
5
+ @connection = CloudPrint::Connection.new
6
+ end
7
+
8
+ test "Connection class exists" do
9
+ assert_nothing_raised { CloudPrint::Connection }
10
+ end
11
+
12
+ test "you can get using a connection" do
13
+ stub
14
+ any_connection.stubs(:request)
15
+ @connection.get('/foo')
16
+ end
17
+
18
+ test "you can post using a connection" do
19
+ stub
20
+ any_connection.stubs(:request)
21
+ @connection.post('/foo')
22
+ end
23
+
24
+ test "connections make requests to the right url with POST" do
25
+ stub
26
+ @connection.expects(:make_http_request).with(:method => :post, :url => "https://www.google.com/cloudprint/submit", :params => {})
27
+ @connection.post('/submit')
28
+ end
29
+
30
+ test "connections build http connections and requests" do
31
+ stub
32
+ @connection.stubs(:build_request)
33
+
34
+ @connection.expects(:build_http_connection).returns(mock_http)
35
+ @connection.post('/submit')
36
+ end
37
+
38
+ test "connections build http request with proper values" do
39
+ stub
40
+ @connection.stubs(:build_http_connection).returns(mock_http)
41
+
42
+ @connection.expects(:build_request)
43
+ @connection.post('/submit', {:text => "ohai!"})
44
+ end
45
+
46
+ test "connections build a URL when parameters are used" do
47
+ stub
48
+ @connection.stubs(:build_http_connection).returns(mock_http)
49
+
50
+ params = { :text => 'ohai' }
51
+ @connection.expects(:build_get_uri).returns("https://www.google.com/cloudprint/submit")
52
+ @connection.get('/submit', params)
53
+ end
54
+
55
+ test "gets from the correct URL when params are used" do
56
+ stub
57
+
58
+ params = { :text => 'ohai world' }
59
+ Net::HTTP::Get.expects(:new).with("/cloudprint/submit?text=ohai%20world").returns(mock_http)
60
+ @connection.get('/submit', params)
61
+ end
62
+
63
+ test "connections use the access token" do
64
+ stub
65
+ @connection.stubs(:build_http_connection).returns(mock_http)
66
+
67
+ CloudPrint.expects(:access_token).returns('token')
68
+ @connection.get('/submit')
69
+ end
70
+
71
+ test "connections parse the response" do
72
+ stub
73
+ @connection.expects(:parse_response)
74
+ @connection.get('/submit')
75
+ end
76
+
77
+ test "connect parse the response as JSON" do
78
+ stub(:parsing_responses => false)
79
+ response = mock('response')
80
+ response.stubs(:body).returns('')
81
+ @connection.stubs(:make_http_request).returns(response)
82
+
83
+ JSON.expects(:parse)
84
+ @connection.get('/submit')
85
+ end
86
+
87
+ private
88
+
89
+ def stub(options = {})
90
+ options = {:http => true, :parsing_responses => true}.merge(options)
91
+
92
+ stub_http if options[:http]
93
+ stub_parsing_responses if options[:parsing_responses]
94
+ end
95
+ end
@@ -0,0 +1,48 @@
1
+ require "cloudprint"
2
+ Bundler.require(:test)
3
+
4
+ class Test::Unit::TestCase
5
+ def any_connection
6
+ CloudPrint::Connection.any_instance
7
+ end
8
+
9
+ def any_access_token
10
+ OAuth2::AccessToken.any_instance
11
+ end
12
+
13
+ def stub_access_token
14
+ mock_access_token.stub_everything
15
+ any_access_token.stubs(:refresh!).returns(mock_access_token)
16
+ end
17
+
18
+ def mock_access_token(token = 'token')
19
+ mock = mock('token')
20
+ mock.stubs(:token).returns(token)
21
+ mock.stubs(:expired?).returns(false)
22
+ mock
23
+ end
24
+
25
+ def stub_http
26
+ mock = mock_http()
27
+
28
+ Net::HTTP.stubs(:new).returns(mock)
29
+ end
30
+
31
+ def stub_parsing_responses
32
+ CloudPrint::Connection.any_instance.stubs(:parse_response)
33
+ end
34
+
35
+ def mock_http
36
+ @mock_http ||= mock('http')
37
+ @mock_http.stub_everything
38
+ @mock_http
39
+ end
40
+
41
+ def mock_oauth_client
42
+ @oauth_client ||= mock('oauth_client')
43
+ end
44
+
45
+ def stub_oauth_client
46
+ CloudPrint.stubs(:oauth_client).returns(mock_oauth_client)
47
+ end
48
+ end
@@ -0,0 +1,93 @@
1
+ require "helper"
2
+ class PrinterTest < Test::Unit::TestCase
3
+ def setup
4
+ CloudPrint.setup('refresh_token')
5
+ end
6
+
7
+ test "CloudPrint Printers exist" do
8
+ assert_nothing_raised { CloudPrint::Printer }
9
+ end
10
+
11
+ test "initializing a printer" do
12
+ printer = CloudPrint::Printer.new(:id => 'printer_id', :status => 'online', :name => "My Printer")
13
+ assert_equal 'printer_id', printer.id
14
+ assert_equal 'online', printer.status
15
+ assert_equal 'My Printer', printer.name
16
+ end
17
+
18
+ test "finding a printer by its id should get a connection" do
19
+ fake_connection.stubs(:get).returns(one_printer_hash)
20
+
21
+ CloudPrint.expects(:connection).returns(fake_connection)
22
+ CloudPrint::Printer.find('printer')
23
+ end
24
+
25
+ test "finding a printer by its id should call a remote request" do
26
+ fake_connection.expects(:get).returns(one_printer_hash)
27
+ stub_connection
28
+ CloudPrint::Printer.find('printer')
29
+ end
30
+
31
+ test "finding a printer should call a remote request with the proper params" do
32
+ fake_connection.expects(:get).with('/printer', :printerid => 'printer').returns(one_printer_hash)
33
+ stub_connection
34
+ CloudPrint::Printer.find('printer')
35
+ end
36
+
37
+ test "finding a printer should initialize a new object" do
38
+ fake_connection.stubs(:get).returns(one_printer_hash)
39
+ stub_connection
40
+ printer = CloudPrint::Printer.find('my_printer')
41
+ assert_equal 'my_printer', printer.id
42
+ assert_equal 'online', printer.status
43
+ assert_equal 'My Printer', printer.name
44
+ end
45
+
46
+ test "finding all printer should initialize an array of printers" do
47
+ fake_connection.stubs(:get).returns(multiple_printer_hash)
48
+ stub_connection
49
+ printers = CloudPrint::Printer.all
50
+ first_printer = printers[0]
51
+ second_printer = printers[1]
52
+ assert_equal 'first_printer', first_printer.id
53
+ assert_equal 'second_printer', second_printer.id
54
+ assert_equal 'First Printer', first_printer.name
55
+ assert_equal 'Second Printer', second_printer.name
56
+ end
57
+
58
+ test "print stuff" do
59
+ title = 'Hello World'
60
+ content = '<h1>ohai!</h1>'
61
+ content_type = 'text/html'
62
+ printer = CloudPrint::Printer.new(:id => 'printer')
63
+ fake_connection.expects(:post).with('/submit', :printerid => 'printer', :title => title, :content => content, :contentType => content_type)
64
+ stub_connection
65
+
66
+ printer.print(:title => title, :content => content, :content_type => content_type)
67
+ end
68
+
69
+ private
70
+
71
+ def one_printer_hash
72
+ {'printers' =>[{'id' => 'my_printer', 'status' => 'online', 'name' => "My Printer"}]}
73
+ end
74
+
75
+ def multiple_printer_hash
76
+ {'printers' =>[
77
+ {'id' => 'first_printer', 'status' => 'online', 'name' => "First Printer"},
78
+ {'id' => 'second_printer', 'status' => 'online', 'name' => "Second Printer"}
79
+ ]}
80
+ end
81
+
82
+ def fake_connection
83
+ if @connection.nil?
84
+ @connection = mock('connection')
85
+ @connection.stub_everything
86
+ end
87
+ @connection
88
+ end
89
+
90
+ def stub_connection
91
+ CloudPrint.stubs(:connection).returns(fake_connection)
92
+ end
93
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloudprint
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Eugen Minciu
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-02-14 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ requirement: *id001
33
+ prerelease: false
34
+ name: oauth2
35
+ - !ruby/object:Gem::Dependency
36
+ type: :runtime
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ requirement: *id002
47
+ prerelease: false
48
+ name: faraday
49
+ - !ruby/object:Gem::Dependency
50
+ type: :runtime
51
+ version_requirements: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirement: *id003
61
+ prerelease: false
62
+ name: json
63
+ - !ruby/object:Gem::Dependency
64
+ type: :development
65
+ version_requirements: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ hash: 23
71
+ segments:
72
+ - 1
73
+ - 0
74
+ - 0
75
+ version: 1.0.0
76
+ requirement: *id004
77
+ prerelease: false
78
+ name: bundler
79
+ - !ruby/object:Gem::Dependency
80
+ type: :development
81
+ version_requirements: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ hash: 7
87
+ segments:
88
+ - 1
89
+ - 6
90
+ - 4
91
+ version: 1.6.4
92
+ requirement: *id005
93
+ prerelease: false
94
+ name: jeweler
95
+ description: |-
96
+ This library provides a ruby-esque interface to Google Cloud Print.
97
+ cloudprint is a work in progress. I'll be adding documentation once all the basic GCP functionality is supported.
98
+ email: eugen@lesseverything.com
99
+ executables: []
100
+
101
+ extensions: []
102
+
103
+ extra_rdoc_files:
104
+ - LICENSE.txt
105
+ - README.rdoc
106
+ files:
107
+ - .document
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.rdoc
112
+ - Rakefile
113
+ - VERSION
114
+ - cloudprint.gemspec
115
+ - lib/cloudprint.rb
116
+ - lib/cloudprint/connection.rb
117
+ - lib/cloudprint/printer.rb
118
+ - test/cloudprint_test.rb
119
+ - test/connection_test.rb
120
+ - test/helper.rb
121
+ - test/printer_test.rb
122
+ has_rdoc: true
123
+ homepage: http://github.com/minciue/cloudprint
124
+ licenses:
125
+ - MIT
126
+ post_install_message:
127
+ rdoc_options: []
128
+
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ hash: 3
137
+ segments:
138
+ - 0
139
+ version: "0"
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ hash: 3
146
+ segments:
147
+ - 0
148
+ version: "0"
149
+ requirements: []
150
+
151
+ rubyforge_project:
152
+ rubygems_version: 1.4.2
153
+ signing_key:
154
+ specification_version: 3
155
+ summary: This library provides a ruby-esque interface to Google Cloud Print.
156
+ test_files: []
157
+