gdata_spreadsheet 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +31 -0
- data/VERSION +1 -0
- data/gdata_spreadsheet.gemspec +55 -0
- data/lib/gdata_spreadsheet.rb +5 -0
- data/lib/google/base.rb +104 -0
- data/lib/google/config.rb +14 -0
- data/lib/google/log.rb +25 -0
- data/lib/google/spreadsheet.rb +65 -0
- data/spec/google/base_spec.rb +43 -0
- data/spec/spec_helper.rb +17 -0
- metadata +82 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Trike Apps
|
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,17 @@
|
|
1
|
+
= GData Spreadsheet
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Trike Apps. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "gdata_spreadsheet"
|
8
|
+
gem.summary = %Q{Simple wrapper to access a google spreadsheet using the GData gem}
|
9
|
+
gem.description = %Q{}
|
10
|
+
gem.email = "tom@trike.com.au"
|
11
|
+
gem.homepage = "http://github.com/tricycle/gdata_spreadsheet"
|
12
|
+
gem.authors = ["Tom"]
|
13
|
+
# gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
task :default => :build
|
22
|
+
|
23
|
+
require 'rake/rdoctask'
|
24
|
+
Rake::RDocTask.new do |rdoc|
|
25
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
26
|
+
|
27
|
+
rdoc.rdoc_dir = 'rdoc'
|
28
|
+
rdoc.title = "gdata_spreadsheet #{version}"
|
29
|
+
rdoc.rdoc_files.include('README*')
|
30
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
31
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{gdata_spreadsheet}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Tom"]
|
12
|
+
s.date = %q{2010-08-30}
|
13
|
+
s.description = %q{}
|
14
|
+
s.email = %q{tom@trike.com.au}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"gdata_spreadsheet.gemspec",
|
27
|
+
"lib/gdata_spreadsheet.rb",
|
28
|
+
"lib/google/base.rb",
|
29
|
+
"lib/google/config.rb",
|
30
|
+
"lib/google/log.rb",
|
31
|
+
"lib/google/spreadsheet.rb",
|
32
|
+
"spec/google/base_spec.rb",
|
33
|
+
"spec/spec_helper.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/tricycle/gdata_spreadsheet}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.7}
|
39
|
+
s.summary = %q{Simple wrapper to access a google spreadsheet using the GData gem}
|
40
|
+
s.test_files = [
|
41
|
+
"spec/google/base_spec.rb",
|
42
|
+
"spec/spec_helper.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
else
|
51
|
+
end
|
52
|
+
else
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
data/lib/google/base.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
module Google
|
2
|
+
class Base
|
3
|
+
attr_reader :doc
|
4
|
+
|
5
|
+
def initialize(doc_id, row_id = nil)
|
6
|
+
raise Google::MissingDocumentError unless doc_id
|
7
|
+
|
8
|
+
@sheet = Spreadsheet.new(doc_id)
|
9
|
+
@worksheet_id = @sheet.worksheet_id_for(worksheet_name)
|
10
|
+
|
11
|
+
initialize_row row_id
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize_row(id)
|
15
|
+
if id_column && id && @doc = @sheet.row_data(id_column, id.downcase, @worksheet_id)
|
16
|
+
initialize_doc
|
17
|
+
else
|
18
|
+
new_row
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def new_row
|
23
|
+
@doc = Nokogiri::XML.parse("<entry xmlns=\"http://www.w3.org/2005/Atom\"></entry>").css("entry").first
|
24
|
+
@doc.add_namespace_definition "gsx", "http://schemas.google.com/spreadsheets/2006/extended"
|
25
|
+
@doc.add_namespace_definition "gd", "http://schemas.google.com/g/2005"
|
26
|
+
end
|
27
|
+
|
28
|
+
def initialize_doc
|
29
|
+
@doc["xmlns"] = "http://www.w3.org/2005/Atom"
|
30
|
+
@doc["xmlns:gsx"] = "http://schemas.google.com/spreadsheets/2006/extended"
|
31
|
+
@doc["xmlns:gd"] = "http://schemas.google.com/g/2005"
|
32
|
+
end
|
33
|
+
|
34
|
+
def save
|
35
|
+
if new_record?
|
36
|
+
@sheet.add_row @doc, @worksheet_id
|
37
|
+
else
|
38
|
+
@sheet.update_row @doc, @worksheet_id
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def new_record?
|
43
|
+
!@doc.css("id").first
|
44
|
+
end
|
45
|
+
|
46
|
+
def worksheet_name
|
47
|
+
raise "Abstract! Overwrite this method in your subclass"
|
48
|
+
end
|
49
|
+
|
50
|
+
def id_column
|
51
|
+
raise "Abstract! Overwrite this method in your subclass"
|
52
|
+
# return nil in your subclass, if you want a push only model
|
53
|
+
end
|
54
|
+
|
55
|
+
def sync_attributes
|
56
|
+
{ }
|
57
|
+
end
|
58
|
+
|
59
|
+
def sync
|
60
|
+
sync_attributes.each do |field, value|
|
61
|
+
set field, value
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def sync!
|
66
|
+
sync
|
67
|
+
save
|
68
|
+
end
|
69
|
+
|
70
|
+
def method_missing(method, *args, &block)
|
71
|
+
method = method.to_s
|
72
|
+
|
73
|
+
if method[-1, 1] == "=" && args.size == 1
|
74
|
+
set method[0..-2], *args
|
75
|
+
elsif args.empty?
|
76
|
+
get method
|
77
|
+
else
|
78
|
+
raise NoMethodError
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def attribute(field)
|
85
|
+
@doc.xpath(".//gsx:#{field}").first
|
86
|
+
end
|
87
|
+
|
88
|
+
def get(field)
|
89
|
+
attribute(field) && attribute(field).text
|
90
|
+
end
|
91
|
+
|
92
|
+
def set(field, value)
|
93
|
+
@doc << build_new_attribute(field) unless attribute(field)
|
94
|
+
attribute(field).content = value
|
95
|
+
end
|
96
|
+
|
97
|
+
def build_new_attribute(field)
|
98
|
+
new_attribute = Nokogiri::XML::Node.new(field.to_s, @doc)
|
99
|
+
new_attribute.add_namespace_definition "gsx", "http://schemas.google.com/spreadsheets/2006/extended"
|
100
|
+
new_attribute.namespace = new_attribute.namespace_definitions.first
|
101
|
+
new_attribute
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
data/lib/google/log.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Google
|
2
|
+
class Log < Base
|
3
|
+
|
4
|
+
def initialize(doc_id, message)
|
5
|
+
super doc_id
|
6
|
+
|
7
|
+
@message = message
|
8
|
+
end
|
9
|
+
|
10
|
+
def worksheet_name
|
11
|
+
"sync log"
|
12
|
+
end
|
13
|
+
|
14
|
+
def id_column
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def sync_attributes
|
19
|
+
{
|
20
|
+
:timestamp => Time.now.to_s(:db),
|
21
|
+
:message => @message
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'google/config'
|
2
|
+
|
3
|
+
module Google
|
4
|
+
WORKSHEET_BASE_URL = "https://spreadsheets.google.com/feeds/worksheets"
|
5
|
+
LIST_BASE_URL = "https://spreadsheets.google.com/feeds/list"
|
6
|
+
|
7
|
+
class MissingDocumentError < ::Exception
|
8
|
+
end
|
9
|
+
|
10
|
+
class FatalError < ::Exception
|
11
|
+
end
|
12
|
+
|
13
|
+
class Spreadsheet
|
14
|
+
|
15
|
+
def initialize(key)
|
16
|
+
@key = key
|
17
|
+
end
|
18
|
+
|
19
|
+
def row_data(field, value, worksheet_id = 1)
|
20
|
+
request_with_error_handling(:get, :list, "#{LIST_BASE_URL}/#{@key}/#{worksheet_id}/private/full?sq=#{CGI.escape(field)}=#{CGI.escape(value)}") do |response|
|
21
|
+
xml = Nokogiri::XML.parse(response.body)
|
22
|
+
xml.css('entry').first
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def update_row(xml, worksheet_id = 1)
|
27
|
+
request_with_error_handling :put, :list, xml.css("link[rel = edit]").first["href"], xml.to_xml
|
28
|
+
end
|
29
|
+
|
30
|
+
def add_row(xml, worksheet_id = 1)
|
31
|
+
request_with_error_handling :post, :list, "#{LIST_BASE_URL}/#{@key}/#{worksheet_id}/private/full", xml.to_xml
|
32
|
+
end
|
33
|
+
|
34
|
+
def worksheet_id_for(name)
|
35
|
+
request_with_error_handling(:get, :worksheet, "#{WORKSHEET_BASE_URL}/#{@key}/private/full") do |response|
|
36
|
+
xml = Nokogiri::XML.parse(response.body)
|
37
|
+
|
38
|
+
# translate converts the attribute's content to lower case,
|
39
|
+
# therefore allowing us to match case-insensitive
|
40
|
+
xml.xpath(".//atom:entry[translate(
|
41
|
+
atom:title,
|
42
|
+
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
43
|
+
'abcdefghijklmnopqrstuvwxyz'
|
44
|
+
) = '#{name.downcase}']/atom:id",
|
45
|
+
"atom" => "http://www.w3.org/2005/Atom"
|
46
|
+
).text.split("/").last
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
protected
|
51
|
+
|
52
|
+
def client(type)
|
53
|
+
client = GData::Client::Spreadsheets.new(:version => '3', :source => 'PlanDeliver')
|
54
|
+
client.authsub_token = Config.setting("#{type}_token")
|
55
|
+
client
|
56
|
+
end
|
57
|
+
|
58
|
+
def request_with_error_handling(method, token_type = "list", *args)
|
59
|
+
response = client(token_type).send(method, *args)
|
60
|
+
yield(response) if block_given?
|
61
|
+
rescue GData::Client::RequestError => e
|
62
|
+
raise Google::FatalError.new(e.message)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TestOrder < Google::Base
|
4
|
+
|
5
|
+
def worksheet_name
|
6
|
+
"sync log"
|
7
|
+
end
|
8
|
+
|
9
|
+
def id_column
|
10
|
+
"id"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe Google::Base do
|
15
|
+
|
16
|
+
before do
|
17
|
+
Google::Spreadsheet.stub! :new => @sheet = mock("Spreadsheet")
|
18
|
+
|
19
|
+
@sheet.stub! :worksheet_id_for => "test",
|
20
|
+
:row_data => Nokogiri::XML.parse("<entry><id>1</id></entry>").css("entry").first
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "initialize" do
|
24
|
+
it "should return a new document, if we didn't specify a id_column" do
|
25
|
+
class TestOrder
|
26
|
+
def id_column; nil end
|
27
|
+
end
|
28
|
+
|
29
|
+
TestOrder.new(1, 2).should be_new_record
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return a new document if we cant find a matching row in the spreadsheet" do
|
33
|
+
@sheet.stub! :row_data => nil
|
34
|
+
TestOrder.new(1, 2).should be_new_record
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should initialize the returned doc if a matching row in the spreadsheet has been found" do
|
38
|
+
order = TestOrder.new(1, 2)
|
39
|
+
order.should_not be_new_record
|
40
|
+
order.doc["xmlns"].should == "http://www.w3.org/2005/Atom"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "gdata_spreadsheet"))
|
3
|
+
|
4
|
+
Spec::Runner.configure do |config|
|
5
|
+
# == Mock Framework
|
6
|
+
#
|
7
|
+
# RSpec uses it's own mocking framework by default. If you prefer to
|
8
|
+
# use mocha, flexmock or RR, uncomment the appropriate line:
|
9
|
+
#
|
10
|
+
# config.mock_with :mocha
|
11
|
+
# config.mock_with :flexmock
|
12
|
+
# config.mock_with :rr
|
13
|
+
#
|
14
|
+
# == Notes
|
15
|
+
#
|
16
|
+
# For more information take a look at Spec::Runner::Configuration and Spec::Runner
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gdata_spreadsheet
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Tom
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-30 00:00:00 +10:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: ""
|
23
|
+
email: tom@trike.com.au
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- LICENSE
|
30
|
+
- README.rdoc
|
31
|
+
files:
|
32
|
+
- .document
|
33
|
+
- .gitignore
|
34
|
+
- LICENSE
|
35
|
+
- README.rdoc
|
36
|
+
- Rakefile
|
37
|
+
- VERSION
|
38
|
+
- gdata_spreadsheet.gemspec
|
39
|
+
- lib/gdata_spreadsheet.rb
|
40
|
+
- lib/google/base.rb
|
41
|
+
- lib/google/config.rb
|
42
|
+
- lib/google/log.rb
|
43
|
+
- lib/google/spreadsheet.rb
|
44
|
+
- spec/google/base_spec.rb
|
45
|
+
- spec/spec_helper.rb
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: http://github.com/tricycle/gdata_spreadsheet
|
48
|
+
licenses: []
|
49
|
+
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options:
|
52
|
+
- --charset=UTF-8
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
hash: 3
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.3.7
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: Simple wrapper to access a google spreadsheet using the GData gem
|
80
|
+
test_files:
|
81
|
+
- spec/google/base_spec.rb
|
82
|
+
- spec/spec_helper.rb
|