to_google_spreadsheet 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -28,36 +28,40 @@ Account.all.to_google_spreadsheet("Accounts")
28
28
  You must provide your Google Docs credentials, optionally you can supply a default spreadsheet to use:
29
29
 
30
30
  ```ruby
31
- module ToGoogleSpreadsheet
32
- CREDENTIALS = ["account@company.com", "seekrit"]
33
- DEFAULT_SPREADSHEET = "spreadsheet_key" # see note below
31
+ GoogleSpreadsheet.config do |c|
32
+ c.email = "user@company.com"
33
+ c.password = "seekrit"
34
+
35
+ # optional, see note below on how to obtain a spreadsheet key
36
+ c.default_spreadsheet = "spreadsheet_key"
34
37
  end
35
38
  ```
36
39
 
40
+ ### Obtaining your spreadsheet key
41
+
37
42
  The spreadsheet key is found in the URL when visiting the spreadsheet:
38
43
 
39
44
  ![Image showing how the key is found in the URL in Google Docs](http://f.cl.ly/items/3j2q0S063j3v1I1P3x1p/Screen%20shot%202011-06-27%20at%209.24.56%20.png)
40
45
 
41
46
  ### Usage
42
47
 
43
- `#to_google_spreadsheet` is simply a method defined on Array, thus any subclasses hereof should work with `to_google_spreadsheet` as expected. `Array#to_google_spreadsheet` takes two arguments:
48
+ `#to_google_spreadsheet` is simply a method defined on Enumerable, thus any subclasses hereof should work with `to_google_spreadsheet` as expected (e.g. Arrays). `Enumerable#to_google_spreadsheet` takes two arguments:
44
49
 
45
50
  ```ruby
46
- class Array
47
- include ToGoogleSpreadsheet
48
-
49
- def to_google_spreadsheet(worksheet, spreadsheet = nil)
50
- session = GoogleSpreadsheet.login(*CREDENTIALS)
51
- spreadsheet = session.spreadsheet_by_key(spreadsheet || DEFAULT_SPREADSHEET)
52
- @ws = spreadsheet.find_or_create_worksheet_by_name(worksheet)
53
- @ws.set_header_columns(self.first)
54
- @ws.populate(self)
55
- @ws.save
51
+ module Enumerable
52
+ def to_google_spreadsheet(worksheet, spreadsheet = GoogleSpreadsheet::Config.default_spreadsheet)
53
+ session = GoogleSpreadsheet.login(GoogleSpreadsheet::Config.email,
54
+ GoogleSpreadsheet::Config.password)
55
+ spreadsheet = session.spreadsheet_by_key(spreadsheet)
56
+ ws = spreadsheet.find_or_create_worksheet_by_name(worksheet)
57
+ ws.set_header_columns(self.first)
58
+ ws.populate(self)
59
+ ws.save
56
60
  end
57
61
  end
58
62
  ```
59
63
 
60
- The first argument is the name of the worksheet within the spreadsheet, e.g. "Accounts", if it doesn't already exist, it will be created. The second, optional, argument is the spreadsheet key, if it is not supplied, the default spreadsheet, defined by `ToGoogleSpreadsheet::DEFAULT_SPREADSHEET`, will be used.
64
+ The first argument being the name of the worksheet within the spreadsheet, e.g. "Accounts", if it doesn't already exist, it will be created. The second, optional, argument is the spreadsheet key (see Obtaining your spreadsheet key above), if it is not supplied, the default spreadsheet, defined by in the configuration will be used.
61
65
 
62
66
  ## Supported objects
63
67
 
@@ -1,3 +1,3 @@
1
1
  module ToGoogleSpreadsheet
2
- VERSION = "0.0.6"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -10,6 +10,16 @@ class OpenStruct
10
10
  end
11
11
 
12
12
  module GoogleSpreadsheet
13
+ def self.config
14
+ yield GoogleSpreadsheet::Config
15
+ end
16
+
17
+ module Config
18
+ extend self
19
+
20
+ attr_accessor :email, :password, :default_spreadsheet
21
+ end
22
+
13
23
  class Spreadsheet
14
24
  def find_or_create_worksheet_by_name(name)
15
25
  ws = worksheets.find {|ws| ws.title == name}
@@ -51,11 +61,10 @@ module GoogleSpreadsheet
51
61
  end
52
62
 
53
63
  module Enumerable
54
- include ToGoogleSpreadsheet
55
-
56
- def to_google_spreadsheet(worksheet, spreadsheet = nil)
57
- session = GoogleSpreadsheet.login(*CREDENTIALS)
58
- spreadsheet = session.spreadsheet_by_key(spreadsheet || DEFAULT_SPREADSHEET)
64
+ def to_google_spreadsheet(worksheet, spreadsheet = GoogleSpreadsheet::Config.default_spreadsheet)
65
+ session = GoogleSpreadsheet.login(GoogleSpreadsheet::Config.email,
66
+ GoogleSpreadsheet::Config.password)
67
+ spreadsheet = session.spreadsheet_by_key(spreadsheet)
59
68
  ws = spreadsheet.find_or_create_worksheet_by_name(worksheet)
60
69
  ws.set_header_columns(self.first)
61
70
  ws.populate(self)
data/test/test.rb CHANGED
@@ -18,11 +18,14 @@ require 'to_google_spreadsheet'
18
18
  # }
19
19
  # ]
20
20
 
21
- module ToGoogleSpreadsheet
22
- CREDENTIALS = ["system-docs@firmafon.dk", "pinkpony42"]
23
- DEFAULT_SPREADSHEET = "tA6lV1NLsBsDNuP_y8sISIw"
21
+ GoogleSpreadsheet.config do |c|
22
+ c.email = "dummy"
23
+ c.password = "seekrit"
24
+ c.default_spreadsheet = "0Agovhaw3pUnDdERZT3oyemlQY2JUaGJjQWNhb0Q3b2c"
24
25
  end
25
26
 
27
+ p GoogleSpreadsheet::Config.email
28
+
26
29
  [{:name => "Bob", :age => 26}, {:name => "John", :age => 28}].to_google_spreadsheet("Employees")
27
30
 
28
31
  #data.to_google_spreadsheet("Employees")
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_google_spreadsheet
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 6
10
- version: 0.0.6
5
+ version: 0.1.0
11
6
  platform: ruby
12
7
  authors:
13
8
  - Simon H. Eskildsen
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-06-27 00:00:00 +02:00
13
+ date: 2011-07-03 00:00:00 +02:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
@@ -26,9 +21,6 @@ dependencies:
26
21
  requirements:
27
22
  - - ">="
28
23
  - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
24
  version: "0"
33
25
  type: :runtime
34
26
  version_requirements: *id001
@@ -40,9 +32,6 @@ dependencies:
40
32
  requirements:
41
33
  - - ">="
42
34
  - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
35
  version: "0"
47
36
  type: :development
48
37
  version_requirements: *id002
@@ -79,23 +68,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
79
68
  requirements:
80
69
  - - ">="
81
70
  - !ruby/object:Gem::Version
82
- hash: 3
83
- segments:
84
- - 0
85
71
  version: "0"
86
72
  required_rubygems_version: !ruby/object:Gem::Requirement
87
73
  none: false
88
74
  requirements:
89
75
  - - ">="
90
76
  - !ruby/object:Gem::Version
91
- hash: 3
92
- segments:
93
- - 0
94
77
  version: "0"
95
78
  requirements: []
96
79
 
97
80
  rubyforge_project: to_google_spreadsheet
98
- rubygems_version: 1.5.2
81
+ rubygems_version: 1.6.2
99
82
  signing_key:
100
83
  specification_version: 3
101
84
  summary: Convert an Array where Array.all? {|e| [OpenStruct,Hash,ActiveRecord::Base].any? { |type| e.kind_of?(type) }} to a Google spreadsheet