Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1 from jimryan/master

General fixes/improvements
  • Loading branch information...
commit 4c2702977c354bd57bdf744dcde70c9d3b8355b7 2 parents c397915 + 7e7e087
@minciue authored
View
57 .gitignore
@@ -1,55 +1,4 @@
-# rcov generated
-coverage
-
-# rdoc generated
-rdoc
-
-# yard generated
-doc
-.yardoc
-
-# bundler
+*.gem
.bundle
-
-# jeweler generated
-pkg
-
-# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
-#
-# * Create a file at ~/.gitignore
-# * Include files you want ignored
-# * Run: git config --global core.excludesfile ~/.gitignore
-#
-# After doing this, these files will be ignored in all your git projects,
-# saving you from having to 'pollute' every project you touch with them
-#
-# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
-#
-# For MacOS:
-#
-.DS_Store
-
-# For TextMate
-#*.tmproj
-#tmtags
-
-# For emacs:
-#*~
-#\#*
-#.\#*
-
-# For vim:
-#*.swp
-tags
-
-# For redcar:
-#.redcar
-
-# For rubinius:
-#*.rbc
-
-# For RubyMine
-.idea
-
-# For rvm
-.rvmrc
+Gemfile.lock
+pkg/*
View
16 Gemfile
@@ -1,16 +1,4 @@
source "http://rubygems.org"
-gem "oauth2", "~> 0.4.0"
-gem "json"
-group :development do
- gem "bundler", "~> 1.0.0"
- gem "jeweler", "~> 1.6.4"
-end
-
-group :test do
- gem "test-unit", ">= 0"
- # gem "net-http-spy"
- gem "rcov", ">= 0"
- gem "mocha", ">= 0"
- gem "ruby-debug"
-end
+# Specify your gem's dependencies in cloudprint.gemspec
+gemspec
View
48 Gemfile.lock
@@ -1,48 +0,0 @@
-GEM
- remote: http://rubygems.org/
- specs:
- addressable (2.2.7)
- columnize (0.3.6)
- faraday (0.6.1)
- addressable (~> 2.2.4)
- multipart-post (~> 1.1.0)
- rack (>= 1.1.0, < 2)
- git (1.2.5)
- jeweler (1.6.4)
- bundler (~> 1.0)
- git (>= 1.2.5)
- rake
- json (1.6.5)
- linecache (0.46)
- rbx-require-relative (> 0.0.4)
- metaclass (0.0.1)
- mocha (0.10.4)
- metaclass (~> 0.0.1)
- multi_json (1.1.0)
- multipart-post (1.1.5)
- oauth2 (0.4.1)
- faraday (~> 0.6.1)
- multi_json (>= 0.0.5)
- rack (1.4.1)
- rake (0.9.2.2)
- rbx-require-relative (0.0.5)
- rcov (1.0.0)
- ruby-debug (0.10.4)
- columnize (>= 0.1)
- ruby-debug-base (~> 0.10.4.0)
- ruby-debug-base (0.10.4)
- linecache (>= 0.3)
- test-unit (2.4.7)
-
-PLATFORMS
- ruby
-
-DEPENDENCIES
- bundler (~> 1.0.0)
- jeweler (~> 1.6.4)
- json
- mocha
- oauth2 (~> 0.4.0)
- rcov
- ruby-debug
- test-unit
View
61 Rakefile
@@ -1,57 +1,8 @@
-# encoding: utf-8
-
-require 'rubygems'
-require 'bundler'
-begin
- Bundler.setup(:default, :development)
-rescue Bundler::BundlerError => e
- $stderr.puts e.message
- $stderr.puts "Run `bundle install` to install missing gems"
- exit e.status_code
-end
-require 'rake'
-
-require 'jeweler'
-Jeweler::Tasks.new do |gem|
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
- gem.name = "cloudprint"
- gem.homepage = "http://github.com/minciue/cloudprint"
- #noinspection RubyResolve
- gem.license = "MIT"
- gem.summary = %Q{This library provides a ruby-esque interface to Google Cloud Print.}
- 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.}
- gem.email = "eugen@lesseverything.com"
- #noinspection RubyResolve
- gem.authors = ["Eugen Minciu"]
- # dependencies defined in Gemfile
-end
-Jeweler::RubygemsDotOrgTasks.new
-
+require 'bundler/gem_tasks'
require 'rake/testtask'
-Rake::TestTask.new(:test) do |test|
- test.libs << 'lib' << 'test'
- test.pattern = 'test/*_test.rb'
- test.verbose = true
-end
-#require 'rcov/rcovtask'
-#Rcov::RcovTask.new do |test|
-# test.libs << 'test'
-# test.pattern = 'test/**/test_*.rb'
-# test.verbose = true
-# test.rcov_opts << '--exclude "gems/*"'
-#end
-
-task :default => :test
-
-require 'rake/rdoctask'
-Rake::RDocTask.new do |rdoc|
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
-
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = "cloudprint #{version}"
- #noinspection RubyResolve
- rdoc.rdoc_files.include('README*')
- #noinspection RubyResolve
- rdoc.rdoc_files.include('lib/**/*.rb')
-end
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/*_test.rb'
+ test.verbose = true
+end
View
1  VERSION
@@ -1 +0,0 @@
-0.0.2
View
81 cloudprint.gemspec
@@ -1,66 +1,29 @@
-# Generated by jeweler
-# DO NOT EDIT THIS FILE DIRECTLY
-# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "cloudprint/version"
Gem::Specification.new do |s|
- s.name = %q{cloudprint}
- s.version = "0.0.2"
+ s.name = "cloudprint"
+ s.version = Cloudprint::VERSION
+ s.authors = ["Eugen Minciu"]
+ s.email = ["eugen@lesseverything.com"]
+ s.homepage = "http://github.com/minciue/cloudprint"
+ s.licenses = ["MIT"]
+ s.summary = "This library provides a ruby-esque interface to Google Cloud Print."
+ s.description = "This library provides a ruby-esque interface to Google Cloud Print.
+cloudprint is a work in progress. I'll be adding documentation once all the basic GCP functionality is supported."
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
- s.authors = ["Eugen Minciu"]
- s.date = %q{2012-03-04}
- s.description = %q{This library provides a ruby-esque interface to Google Cloud Print.
-cloudprint is a work in progress. I'll be adding documentation once all the basic GCP functionality is supported.}
- s.email = %q{eugen@lesseverything.com}
- s.extra_rdoc_files = [
- "LICENSE.txt",
- "README.rdoc"
- ]
- s.files = [
- ".document",
- "Gemfile",
- "Gemfile.lock",
- "LICENSE.txt",
- "README.rdoc",
- "Rakefile",
- "VERSION",
- "cloudprint.gemspec",
- "lib/cloudprint.rb",
- "lib/cloudprint/connection.rb",
- "lib/cloudprint/print_job.rb",
- "lib/cloudprint/printer.rb",
- "test/cloudprint_test.rb",
- "test/connection_test.rb",
- "test/helper.rb",
- "test/print_job_test.rb",
- "test/printer_test.rb"
- ]
- s.homepage = %q{http://github.com/minciue/cloudprint}
- s.licenses = ["MIT"]
- s.require_paths = ["lib"]
- s.rubygems_version = %q{1.4.2}
- s.summary = %q{This library provides a ruby-esque interface to Google Cloud Print.}
+ s.add_dependency 'oauth2', '~> 0.5.2'
+ s.add_dependency 'json'
- if s.respond_to? :specification_version then
- s.specification_version = 3
+ %w{test-unit mocha rake}.each do |dep|
+ s.add_development_dependency dep
+ end
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- s.add_runtime_dependency(%q<oauth2>, ["~> 0.4.0"])
- s.add_runtime_dependency(%q<json>, [">= 0"])
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
- s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
- else
- s.add_dependency(%q<oauth2>, ["~> 0.4.0"])
- s.add_dependency(%q<json>, [">= 0"])
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
- s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
- end
- else
- s.add_dependency(%q<oauth2>, ["~> 0.4.0"])
- s.add_dependency(%q<json>, [">= 0"])
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
- s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
- end
-end
+ s.rubyforge_project = "cloudprint"
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
View
6 lib/cloudprint.rb
@@ -3,6 +3,7 @@
require "oauth2"
require "json"
+require "cloudprint/version"
require "cloudprint/printer"
require "cloudprint/connection"
require "cloudprint/print_job"
@@ -45,6 +46,11 @@ def self.access_token
end
end
+ def self.refresh_token=(new_token)
+ @refresh_token = new_token
+ get_new_access_token
+ end
+
private
def self.access_token_valid?
View
10 lib/cloudprint/connection.rb
@@ -15,6 +15,11 @@ def post(path, params = {})
parse_response(response)
end
+ def multipart_post(path, params = {})
+ response = request(:multipart, path, params)
+ parse_response(response)
+ end
+
private
def parse_response(response)
@@ -45,6 +50,11 @@ def build_request(options)
uri = options[:uri]
request = case method
+ when :multipart
+ req = Net::HTTP::Post.new(uri.request_uri)
+ # Convert hash keys to strings, because that's what Net::HTTPGenericRequest#encode_multipart_form_data assumes they are
+ req.set_form(options[:params].inject({}) {|memo, (k,v)| memo[k.to_s] = v; memo }, 'multipart/form-data')
+ req
when :post
req = Net::HTTP::Post.new(uri.request_uri)
req.set_form_data(options[:params])
View
19 lib/cloudprint/print_job.rb
@@ -8,13 +8,18 @@ def initialize(options = {})
end
def self.find(jobid)
- response = CloudPrint.connection.post('/jobs', :jobid => jobid) || {}
- return nil unless response['jobs'].is_a?(Array)
- job = response['jobs'].first
+ job = find_by_id(jobid)
return nil if job.nil?
self.new(:id => job['id'], :status => job['status'], :error_code => job['errorCode'])
end
+ def refresh!
+ job = self.class.find_by_id(id)
+ @status = job['status']
+ @error_code = job['errorCode']
+ self
+ end
+
def queued?
status == "QUEUED"
end
@@ -30,5 +35,13 @@ def done?
def error?
status == "ERROR"
end
+
+ private
+
+ def self.find_by_id(id)
+ response = CloudPrint.connection.get('/jobs') || {}
+ return nil unless response['jobs'].is_a?(Array)
+ response['jobs'].select{ |job| job['id'] == id }.first
+ end
end
end
View
3  lib/cloudprint/printer.rb
@@ -9,7 +9,8 @@ def initialize(options = {})
end
def print(options)
- response = CloudPrint.connection.post('/submit', :printerid => self.id, :title => options[:title], :content => options[:content], :contentType => options[:content_type]) || {}
+ method = options[:content].is_a?(IO) ? :multipart_post : :post
+ response = CloudPrint.connection.send(method, '/submit', :printerid => self.id, :title => options[:title], :content => options[:content], :contentType => options[:content_type]) || {}
CloudPrint::PrintJob.new(:id => response["job"]["id"], :status => response["job"]["status"], :error_code => response["job"]["errorCode"])
end
View
3  lib/cloudprint/version.rb
@@ -0,0 +1,3 @@
+module Cloudprint
+ VERSION = "0.0.2"
+end
View
5 test/cloudprint_test.rb
@@ -115,4 +115,9 @@ def setup
CloudPrint.expects(:get_new_access_token).returns(token)
CloudPrint.access_token
end
+
+ test "setting a new refresh token should get a new access token" do
+ CloudPrint.expects(:get_new_access_token)
+ CloudPrint.refresh_token = 'new_token'
+ end
end
View
14 test/connection_test.rb
@@ -21,6 +21,12 @@ def setup
@connection.post('/foo')
end
+ test "you can post multipart data using a connection" do
+ stub
+ any_connection.stubs(:request)
+ @connection.multipart_post('/foo')
+ end
+
test "connections make requests to the right url with POST" do
stub
@connection.expects(:make_http_request).with(:method => :post, :url => "https://www.google.com/cloudprint/submit", :params => {})
@@ -84,6 +90,14 @@ def setup
@connection.get('/submit')
end
+ test "connections setup form properly on multipart POSTs" do
+ stub
+
+ file = mock('File')
+ Net::HTTP::Post.any_instance.expects(:set_form).with({'contentType' => 'application/pdf', 'content' => file}, 'multipart/form-data')
+ @connection.multipart_post('/submit', { :contentType => 'application/pdf', :content => file })
+ end
+
private
def stub(options = {})
View
BIN  test/fixtures/ruby.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
6 test/helper.rb
@@ -1,5 +1,5 @@
require "cloudprint"
-Bundler.require(:test)
+require 'mocha'
class Test::Unit::TestCase
def any_connection
@@ -54,4 +54,8 @@ def stub_connection
CloudPrint.stubs(:connection).returns(fake_connection)
@connection.stub_everything
end
+
+ def fixture_file(filename)
+ File.join(File.dirname(__FILE__), 'fixtures', filename)
+ end
end
View
30 test/print_job_test.rb
@@ -2,24 +2,24 @@
class PrintJobTest < Test::Unit::TestCase
def setup
- CloudPrint.setup('refresh_token')
+ # TODO: Is it necessary to pass a fake token to #setup?
+ CloudPrint.setup(:refresh_token => 'refresh_token')
stub_connection
end
test "find a job" do
- fake_connection.stubs(:post).with('/jobs', :jobid => 'job_id').returns(job_response)
+ fake_connection.stubs(:get).with('/jobs').returns(jobs_response)
assert CloudPrint::PrintJob.find('job_id').is_a?(CloudPrint::PrintJob)
end
- # Google doesn't list jobid as a possible value in its public docs, but it seems to work just fine
test 'find a job performs a remote request' do
- fake_connection.expects(:post).with('/jobs', :jobid => 'job_id').returns({})
+ fake_connection.expects(:get).with('/jobs').returns({})
CloudPrint::PrintJob.find('job_id')
end
test 'find a job gets the job details' do
- fake_connection.stubs(:post).with('/jobs', :jobid => 'job_id').returns(job_response)
+ fake_connection.stubs(:get).with('/jobs').returns(jobs_response)
job = CloudPrint::PrintJob.find('job_id')
assert_equal 'job_id', job.id
@@ -66,9 +66,25 @@ def setup
assert job.error?
end
+
+ test "refreshing a job" do
+ job = CloudPrint::PrintJob.new(:status => "IN_PROGRESS")
+ CloudPrint::PrintJob.stubs(:find_by_id).returns({"id" => "job_id", "status" => "DONE", "errorCode" => "42"})
+
+ assert_equal job, job.refresh!
+
+ assert job.done?
+ assert_equal "42", job.error_code
+ end
+
private
- def job_response
- {"jobs" => [{"id" => "job_id", "status" => "status", "errorCode" => "Error"}]}
+ def jobs_response
+ {
+ "jobs" => [
+ {"id" => "other_job", "status" => "status", "errorCode" => "Error"},
+ {"id" => "job_id", "status" => "status", "errorCode" => "Error"}
+ ]
+ }
end
end
View
26 test/printer_test.rb
@@ -1,7 +1,8 @@
require "helper"
class PrinterTest < Test::Unit::TestCase
def setup
- CloudPrint.setup('refresh_token')
+ # TODO: Is it necessary to pass a fake token to #setup?
+ CloudPrint.setup(:refresh_token => 'refresh_token')
end
test "CloudPrint Printers exist" do
@@ -78,6 +79,13 @@ def setup
assert job.is_a?(CloudPrint::PrintJob)
end
+ test "print file" do
+ fake_connection.expects(:multipart_post).with('/submit', connection_print_file_params).returns(empty_job)
+ stub_connection
+
+ print_file
+ end
+
test "print job has an id and a status" do
stub_connection
fake_connection.expects(:post).with('/submit', connection_print_params).returns({"success" => true, "job" => {"id" => "job_id", "status" => 'status'}})
@@ -106,6 +114,19 @@ def connection_print_params
{ :printerid => 'printer', :title => "Hello World", :content => "<h1>ohai!</h1>", :contentType => "text/html" }
end
+ def print_file
+ printer = CloudPrint::Printer.new(:id => 'printer')
+ printer.print(print_file_params)
+ end
+
+ def print_file_params
+ { :title => "Ruby!", :content => ruby_png_fixture, :content_type => "image/png" }
+ end
+
+ def connection_print_file_params
+ { :printerid => 'printer', :title => "Ruby!", :content => ruby_png_fixture, :contentType => "image/png" }
+ end
+
def one_printer_hash
{'printers' =>[{'id' => 'my_printer', 'status' => 'online', 'name' => "My Printer", 'tags' => { 'email' => 'a@b.com'}}]}
end
@@ -117,5 +138,8 @@ def multiple_printer_hash
]}
end
+ def ruby_png_fixture
+ @ruby_png_fixture ||= File.open(fixture_file('ruby.png'))
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.