activeresource-google_spreadsheets 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in google_spreadsheets.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Chihiro Ito, Toru KAWAMURA
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # ActiveResource GoogleSpreadsheets
2
+
3
+ Google Spreadsheets accessor with ActiveResource
4
+
5
+ http://webos-goodies.jp/archives/active_resource_google_spreadsheets_data_api.html
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'activeresource-google_spreadsheets'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install activeresource-google_spreadsheets
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'google_spreadsheets/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activeresource-google_spreadsheets"
8
+ spec.version = GoogleSpreadsheets::VERSION
9
+ spec.authors = ["Chihiro Ito", "Toru KAWAMURA"]
10
+ spec.email = ["tkawa@4bit.net"]
11
+ spec.description = %q{Google Spreadsheets accessor with ActiveResource}
12
+ spec.summary = %q{Google Spreadsheets accessor with ActiveResource}
13
+ spec.homepage = "https://github.com/tkawa/activeresource-google_spreadsheets"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'activeresource'
22
+ spec.add_dependency 'activesupport'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
@@ -0,0 +1 @@
1
+ require 'google_spreadsheets'
@@ -0,0 +1,52 @@
1
+ module GoogleSpreadsheets
2
+ class Base < ActiveResource::Base
3
+ self.site = 'http://spreadsheets.google.com/'
4
+ self.format = GDataFormat.new
5
+
6
+ class << self
7
+ def connection(refresh = false)
8
+ if defined?(@connection) || self == Base
9
+ @connection = Connection.new(site, format) if refresh || @connection.nil?
10
+ @connection.user = user if user
11
+ @connection.password = password if password
12
+ @connection.timeout = timeout if timeout
13
+ @connection
14
+ else
15
+ superclass.connection
16
+ end
17
+ end
18
+ def element_path(id, prefix_options = {}, query_options = nil)
19
+ prefix_options, query_options = split_options(prefix_options) if query_options.nil?
20
+ "/feeds/#{collection_name}#{prefix(prefix_options)}#{id}#{query_string(query_options)}"
21
+ end
22
+ def collection_path(prefix_options = {}, query_options = nil)
23
+ prefix_options, query_options = split_options(prefix_options) if query_options.nil?
24
+ "/feeds/#{collection_name}#{prefix(prefix_options)}#{query_string(query_options)}"
25
+ end
26
+ def custom_method_collection_url(method_name, options = {}) raise NotSupportedError.new end
27
+ def delete(id, options = {}) raise NotSupportedError.new end
28
+ def exists?(id, options = {}) raise NotSupportedError.new end
29
+ end
30
+
31
+ def custom_method_element_url(method_name, options = {}) raise NotSupportedError.new end
32
+ def custom_method_new_element_url(method_name, options = {}) raise NotSupportedError.new end
33
+
34
+ def destroy() connection.delete(edit_path, self.class.headers) end
35
+
36
+ protected
37
+
38
+ def update
39
+ returning connection.put(edit_path, encode, self.class.headers) do |response|
40
+ load_attributes_from_response(response)
41
+ end
42
+ end
43
+ def edit_path()
44
+ s = self.class.site
45
+ (self.attributes['link'] || []).map{|l| l.rel == 'edit' ? l.href : nil }.compact.each do |href|
46
+ e = URI.parse(href)
47
+ return e.request_uri if s.scheme == e.scheme && s.port == e.port && s.host == e.host
48
+ end
49
+ raise EditLinkNotFoundError.new
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,21 @@
1
+ module GoogleSpreadsheets
2
+ class Connection < ActiveResource::Connection
3
+ DEBUG = false
4
+ def authorization_header(http_method, uri)
5
+ if @user && @password && !@token
6
+ email = CGI.escape(@user)
7
+ password = CGI.escape(@password)
8
+ http = Net::HTTP.new('www.google.com', 443)
9
+ http.use_ssl = true
10
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
11
+ resp, data = http.post('/accounts/ClientLogin',
12
+ "accountType=HOSTED_OR_GOOGLE&Email=#{email}&Passwd=#{password}&service=wise",
13
+ { 'Content-Type' => 'application/x-www-form-urlencoded' })
14
+ handle_response(resp)
15
+ @token = (data || resp.body)[/Auth=(.*)/n, 1]
16
+ end
17
+ @token ? { 'Authorization' => "GoogleLogin auth=#{@token}" } : {}
18
+ end
19
+ def http() http = super; http.set_debug_output($stderr) if DEBUG; http end
20
+ end
21
+ end
@@ -0,0 +1,32 @@
1
+ module GoogleSpreadsheets
2
+ class GDataFormat
3
+ def extension() '' end
4
+ def mime_type() 'application/atom+xml' end
5
+ def decode(xml)
6
+ e = Hash.from_xml(xml)
7
+ if e.has_key?('feed')
8
+ e = e['feed']['entry'] || []
9
+ (e.is_a?(Array) ? e : [e]).each{|i| format_entry(i) }
10
+ else
11
+ format_entry(e['entry'])
12
+ end
13
+ end
14
+ def encode(hash, options = {})
15
+ root = REXML::Element.new('entry')
16
+ root.add_namespace('http://www.w3.org/2005/Atom')
17
+ (options[:namespaces] || {}).each{|key, value| root.add_namespace(key, value) }
18
+ hash.each do |key, value|
19
+ next unless value
20
+ e = REXML::Element.new(key, root)
21
+ e.text = value
22
+ end
23
+ root.to_s
24
+ end
25
+ private
26
+ def format_entry(e)
27
+ e['id'] = e['id'][/[^\/]+\z/u] if e.has_key?('id')
28
+ e['updated'] = (Time.xmlschema(e['updated']) rescue Time.parse(e['updated'])) if e.has_key?('updated')
29
+ e
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ module GoogleSpreadsheets
2
+ class List < Base
3
+ class Format < GDataFormat
4
+ def decode(xml)
5
+ xml.force_encoding('UTF-8') # cf. http://d.hatena.ne.jp/kitamomonga/20101218/ruby_19_net_http_encoding
6
+ super(xml.gsub(/<(\/?)gsx:/u, '<\1gsx_'))
7
+ end
8
+ def encode(hash, options = {})
9
+ super(Hash[*hash.map{|p| /^gsx_(.+)/ === p[0] ? ['gsx:'+$1, p[1]] : nil }.compact.flatten],
10
+ { :namespaces => { 'gsx' => 'http://schemas.google.com/spreadsheets/2006/extended' } })
11
+ end
12
+ end
13
+ @connection = nil # avoid using base class's connection.
14
+ self.prefix = '/:document_id/:worksheet_id/:visibility/:projection/'
15
+ self.format = Format.new
16
+ def self.collection_name; 'list' end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ module GoogleSpreadsheets
2
+ class Spreadsheet < Base
3
+ @connection = nil # avoid using base class's connection.
4
+ self.prefix = '/private/full/'
5
+ def self.collection_name; 'spreadsheets' end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module GoogleSpreadsheets
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,20 @@
1
+ module GoogleSpreadsheets
2
+ class Worksheet < Base
3
+ class Format < GDataFormat
4
+ def encode(hash, options = {})
5
+ super({'title'=>hash['title'],'gs:rowCount'=>hash['rowCount'],'gs:colCount'=>hash['colCount']},
6
+ { :namespaces => { 'gs' => 'http://schemas.google.com/spreadsheets/2006' } })
7
+ end
8
+ private
9
+ def format_entry(e)
10
+ e = super
11
+ e['rowCount'] = e['rowCount'].to_i if e.has_key?('rowCount')
12
+ e['colCount'] = e['colCount'].to_i if e.has_key?('colCount')
13
+ end
14
+ end
15
+ @connection = nil # avoid using base class's connection.
16
+ self.prefix = '/:document_id/:visibility/:projection/'
17
+ self.format = Format.new
18
+ def self.collection_name; 'worksheets' end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ require 'google_spreadsheets/version'
2
+ require 'active_support'
3
+ require 'active_resource'
4
+ require 'time'
5
+ require 'erb'
6
+
7
+ module GoogleSpreadsheets
8
+ extend ActiveSupport::Autoload
9
+
10
+ autoload :Base
11
+ autoload :Connection
12
+ autoload :GDataFormat
13
+ autoload :Spreadsheet
14
+ autoload :Worksheet
15
+ autoload :List
16
+
17
+ class BaseError < StandardError
18
+ DefaultMessage = nil
19
+ def initialize(message=nil)
20
+ super(message || self.class::DefaultMessage)
21
+ end
22
+ end
23
+ class NotSupportedError < StandardError
24
+ DefaultMessage = "Google Spreadsheets Data API doesn't support this operation"
25
+ end
26
+ class EditLinkNotFoundError < StandardError
27
+ DefaultMessage = "No edit link"
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activeresource-google_spreadsheets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chihiro Ito
9
+ - Toru KAWAMURA
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-10-09 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activeresource
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: activesupport
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '1.3'
63
+ - !ruby/object:Gem::Dependency
64
+ name: rake
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ description: Google Spreadsheets accessor with ActiveResource
80
+ email:
81
+ - tkawa@4bit.net
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - activeresource-google_spreadsheets.gemspec
92
+ - lib/activeresource-google_spreadsheets.rb
93
+ - lib/google_spreadsheets.rb
94
+ - lib/google_spreadsheets/base.rb
95
+ - lib/google_spreadsheets/connection.rb
96
+ - lib/google_spreadsheets/g_data_format.rb
97
+ - lib/google_spreadsheets/list.rb
98
+ - lib/google_spreadsheets/spreadsheet.rb
99
+ - lib/google_spreadsheets/version.rb
100
+ - lib/google_spreadsheets/worksheet.rb
101
+ homepage: https://github.com/tkawa/activeresource-google_spreadsheets
102
+ licenses:
103
+ - MIT
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ segments:
115
+ - 0
116
+ hash: 3471737805379607528
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ segments:
124
+ - 0
125
+ hash: 3471737805379607528
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 1.8.24
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: Google Spreadsheets accessor with ActiveResource
132
+ test_files: []