Class: Datapimp::Sources::GoogleSpreadsheet
- Inherits:
-
Base
- Object
- Base
- Datapimp::Sources::GoogleSpreadsheet
show all
- Defined in:
- lib/datapimp/sources/google_spreadsheet.rb
Instance Attribute Summary collapse
Attributes inherited from Base
#format, #options, #path, #processed, #raw, #refreshed_at, #scopes, #slug_column
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#age, #compute_properties, #data, #ensure_valid_options!, #file, #has_scope?, #jsonify, #max_age, #need_to_refresh?, #path_to_file, #persisted?, #process, #processors, #refresh, #refresh!, #refresh_if_stale?, requires, #save_to_disk, #scope, #select, #to_s
Constructor Details
Returns a new instance of GoogleSpreadsheet.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 9
def initialize name, options={}
@options = options
if defined?(::GoogleDrive) && name.is_a?(GoogleDrive::Spreadsheet)
@spreadsheet = name
@name = @spreadsheet.title
@key = @spreadsheet.key
end
@key ||= options[:key]
@session ||= options.fetch(:session) { Datapimp::Sync.google.api }
ensure_valid_options!
end
|
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
5
6
7
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 5
def key
@key
end
|
#name ⇒ Object
Returns the value of attribute name.
5
6
7
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 5
def name
@name
end
|
#session ⇒ Object
Returns the value of attribute session.
5
6
7
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 5
def session
@session
end
|
Class Method Details
.[](key_or_title) ⇒ Object
34
35
36
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 34
def self.[](key_or_title)
find_by_key(key_or_title) || find_by_title(key_or_title)
end
|
.create_from_data(data, options = {}) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 58
def self.create_from_data(data, options={})
require 'csv'
= Array(options[:headers]).map(&:to_s)
tmpfile = "tmp-csv.csv"
CSV.open(tmpfile, "wb") do |csv|
csv <<
data.each do |row|
csv << .map do ||
row = row.stringify_keys
row[.to_s]
end
end
end
spreadsheet = Datapimp::Sync.google.api.upload_from_file(tmpfile, options[:title], :content_type => "text/csv")
new(spreadsheet.title, key: spreadsheet.key)
end
|
.create_from_file(path, title) ⇒ Object
24
25
26
27
28
29
30
31
32
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 24
def self.create_from_file(path, title)
if find_by_title(title)
raise 'Spreadsheet with this title already exists'
end
session.upload_from_file(path, title, :content_type => "text/csv")
find_by_title(title)
end
|
.find_by_key(key) ⇒ Object
38
39
40
41
42
43
44
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 38
def self.find_by_key(key)
sheet = session_spreadsheets.detect do |spreadsheet|
spreadsheet.key == key
end
sheet && new(sheet, session: Datapimp::Sync.google.session)
end
|
.find_by_title(title) ⇒ Object
46
47
48
49
50
51
52
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 46
def self.find_by_title title
sheet = session_spreadsheets.detect do |spreadsheet|
spreadsheet.title.match(title)
end
sheet && new(sheet, session: Datapimp::Sync.google.session)
end
|
.session_spreadsheets ⇒ Object
54
55
56
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 54
def self.session_spreadsheets
@session_spreadsheets ||= Datapimp::Sync.google.api.spreadsheets
end
|
Instance Method Details
#add_to_collection(collection_title) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 112
def add_to_collection collection_title
collection = if collection_title.is_a?(GoogleDrive::Collection)
collection_title
else
session.collections.find do |c|
c.title == collection_title
end
end
if !collection
collection_names = session.collections.map(&:title)
raise 'Could not find collection in Google drive. Maybe you mean: ' + collection_names.join(', ')
end
end
|
#edit_url ⇒ Object
86
87
88
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 86
def edit_url
spreadsheet.human_url
end
|
#fetch ⇒ Object
147
148
149
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 147
def fetch
self.raw = process_worksheets
end
|
#fresh_on_server? ⇒ Boolean
135
136
137
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 135
def fresh_on_server?
refreshed_at.to_i > 0 && (last_updated_at > refreshed_at)
end
|
#last_updated_at ⇒ Object
139
140
141
142
143
144
145
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 139
def last_updated_at
if value = spreadsheet.document_feed_entry_internal.css('updated').try(:text) rescue nil
DateTime.parse(value).to_i
else
Time.now.to_i
end
end
|
#preprocess ⇒ Object
151
152
153
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 151
def preprocess
single? ? raw.values.flatten : raw
end
|
#share_read_access_with(*emails) ⇒ Object
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 101
def share_read_access_with *emails
acl = spreadsheet.acl
Array(emails).flatten.each do |email|
acl.push scope_type: "user",
with_key: false,
role: "reader",
scope: email
end
end
|
#share_write_access_with(*emails) ⇒ Object
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 90
def share_write_access_with *emails
acl = spreadsheet.acl
Array(emails).flatten.each do |email|
acl.push scope_type: "user",
with_key: false,
role: "writer",
scope: email
end
end
|
#spreadsheet_key ⇒ Object
127
128
129
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 127
def spreadsheet_key
key
end
|
#stale? ⇒ Boolean
131
132
133
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 131
def stale?
(!need_to_refresh? && (age > max_age)) || fresh_on_server?
end
|
#title ⇒ Object
82
83
84
|
# File 'lib/datapimp/sources/google_spreadsheet.rb', line 82
def title
@name ||= spreadsheet.try(:title)
end
|