gsmetrics 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +2 -2
- data/Gemfile +1 -7
- data/Gemfile.lock +21 -20
- data/README.md +14 -34
- data/gsmetrics.gemspec +2 -6
- data/lib/gsmetrics/gsmetrics.rb +25 -76
- metadata +13 -34
- data/bin/gsmetrics +0 -65
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,32 +1,33 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
gsmetrics (0.0
|
5
|
-
|
6
|
-
crack
|
7
|
-
httparty
|
4
|
+
gsmetrics (0.1.0)
|
5
|
+
google-spreadsheet-ruby (~> 0.2.1)
|
8
6
|
|
9
7
|
GEM
|
10
8
|
specs:
|
11
|
-
addressable (2.2.
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
9
|
+
addressable (2.2.7)
|
10
|
+
faraday (0.7.6)
|
11
|
+
addressable (~> 2.2)
|
12
|
+
multipart-post (~> 1.1)
|
13
|
+
rack (~> 1.1)
|
14
|
+
google-spreadsheet-ruby (0.2.1)
|
15
|
+
nokogiri (>= 1.4.4, != 1.5.2, != 1.5.1)
|
16
|
+
oauth (>= 0.3.6)
|
17
|
+
oauth2 (>= 0.5.0)
|
18
|
+
httpauth (0.1)
|
19
|
+
multi_json (1.3.2)
|
20
|
+
multipart-post (1.1.5)
|
21
|
+
nokogiri (1.5.0)
|
22
|
+
oauth (0.4.6)
|
23
|
+
oauth2 (0.6.0)
|
24
|
+
faraday (~> 0.7)
|
25
|
+
httpauth (~> 0.1)
|
26
|
+
multi_json (~> 1.0)
|
27
|
+
rack (1.4.1)
|
22
28
|
|
23
29
|
PLATFORMS
|
24
30
|
ruby
|
25
31
|
|
26
32
|
DEPENDENCIES
|
27
|
-
builder
|
28
|
-
crack
|
29
33
|
gsmetrics!
|
30
|
-
httparty
|
31
|
-
launchy
|
32
|
-
thor
|
data/README.md
CHANGED
@@ -6,58 +6,38 @@ You can install it by calling
|
|
6
6
|
|
7
7
|
Development was started so we could run a cron job in regular intervals and push metrics data about Railsonfire to a Google Spreadsheet.
|
8
8
|
|
9
|
-
To start using it simply run
|
10
|
-
|
11
|
-
``` gsmetrics ```
|
12
|
-
|
13
|
-
and follow the instructions given. You will have to create a new google API Project and after
|
14
|
-
finishing all steps get code you can simply paste into your codebase.
|
15
|
-
|
16
9
|
## Usage
|
17
10
|
|
18
|
-
Create a new GSMetrics Session and a worksheet, then append to it and
|
11
|
+
Create a new GSMetrics Session and a worksheet, then append to it and create as many rows as you like:
|
19
12
|
|
20
13
|
```ruby
|
21
|
-
session = GSMetrics::Session.new(
|
22
|
-
|
14
|
+
session = GSMetrics::Session.new("YOUR_GOOGLE_USERNAME, "YOUR_GOOGLE_PASSWORD")
|
15
|
+
session.worksheet "SPREADSHEET_TITLE, "WORKSHEET_TITLE"
|
23
16
|
worksheet << 1
|
24
17
|
worksheet.append(5)
|
25
|
-
worksheet.
|
18
|
+
worksheet.next_row
|
19
|
+
worksheet.append(10)
|
20
|
+
worksheet.save
|
26
21
|
```
|
27
22
|
|
28
|
-
|
23
|
+
You simply add to the current row with either ***<<*** or ***.append()***. When calling ***next_row*** the current data is stored away for later saving and you can start adding new items again.
|
29
24
|
|
30
|
-
|
25
|
+
The whole worksheet is updated in one batch call, so only one HTTP request is sent.
|
31
26
|
|
32
|
-
|
33
|
-
|
34
|
-
```ruby
|
35
|
-
worksheet.save_row 5
|
36
|
-
```
|
27
|
+
You can also set the exact row in the worksheet the data should be saved into. This is very handy if you have a worksheet that you need to update completely.
|
37
28
|
|
38
|
-
The following example
|
39
|
-
if there is a free row at the end of the worksheet and if not adds one. If you know beforehand the number of rows you need (e.g. daily updated list of users) you can
|
40
|
-
set the worksheet size and disable the check. This makes the upload process much faster, as the check doesn't have to be made before every new row is added.
|
29
|
+
The following example saves all rows you added starting with the row id of 5. So if you added 2 rows to the worksheet the rows 5 and 6 will be added/overwritten in the spreadsheet.
|
41
30
|
|
42
31
|
```ruby
|
43
|
-
|
44
|
-
worksheet.check_worksheet_size = false
|
45
|
-
worksheet << 1
|
46
|
-
worksheet.save_row
|
32
|
+
worksheet.save 5
|
47
33
|
```
|
34
|
+
When you do not specify any row it will be automatically added to the end.
|
48
35
|
|
49
36
|
After saving a row all appended data is removed and you can start with a new row.
|
50
|
-
Please make sure there are only empty lines at the end of the spreadsheet as the first empty line is used to write the data.
|
51
|
-
|
52
|
-
|
53
|
-
### Worksheet ID
|
54
|
-
|
55
|
-
You can find out what your worksheet_id is by going to
|
56
37
|
|
57
|
-
https://spreadsheets.google.com/feeds/worksheets/#{your_document_id}/private/full
|
58
38
|
|
59
|
-
|
60
|
-
|
39
|
+
###Authentication
|
40
|
+
GSMetrics doesn't suppot OAUTH login with Google any more, as it was painful to implement and not worthwile for us. If you are interrested in having this we gladly accept a pull request.
|
61
41
|
|
62
42
|
Copyright (c) 2011-2012 Florian Motlik, licensed under MIT License
|
63
43
|
|
data/gsmetrics.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "gsmetrics"
|
3
|
-
s.version = "0.0
|
3
|
+
s.version = "0.1.0"
|
4
4
|
s.platform = Gem::Platform::RUBY
|
5
5
|
s.authors = ["Florian Motlik"]
|
6
6
|
s.email = ["flo@railsonfire.com"]
|
@@ -11,12 +11,8 @@ Gem::Specification.new do |s|
|
|
11
11
|
|
12
12
|
s.required_rubygems_version = ">= 1.3.6"
|
13
13
|
|
14
|
-
s.requirements << 'Thor and launchy gem need to be installed for the executable. Not adding them as a dependency for the whole library'
|
15
|
-
|
16
14
|
# If you have runtime dependencies, add them here
|
17
|
-
s.
|
18
|
-
s.add_runtime_dependency(%q<builder>, [">= 0"])
|
19
|
-
s.add_runtime_dependency(%q<crack>, [">= 0"])
|
15
|
+
s.add_dependency("google-spreadsheet-ruby", "~> 0.2.1")
|
20
16
|
|
21
17
|
# If you have development dependencies, add them here
|
22
18
|
# s.add_development_dependency "another", "= 0.9"
|
data/lib/gsmetrics/gsmetrics.rb
CHANGED
@@ -1,107 +1,56 @@
|
|
1
|
-
require '
|
2
|
-
require 'httparty'
|
3
|
-
require 'json'
|
4
|
-
require 'crack'
|
1
|
+
require 'google_spreadsheet'
|
5
2
|
|
6
3
|
module GSMetrics
|
7
4
|
class Session
|
8
|
-
def initialize
|
9
|
-
|
10
|
-
|
11
|
-
:client_secret => client_secret,
|
12
|
-
:refresh_token => refresh_token,
|
13
|
-
:grant_type => "refresh_token"
|
14
|
-
}
|
15
|
-
response = HTTParty.post("https://accounts.google.com/o/oauth2/token", :body => refresh_request)
|
16
|
-
parsed = JSON.parse(response.body)
|
17
|
-
@access_token = parsed["access_token"]
|
5
|
+
def initialize email, password
|
6
|
+
@email = email
|
7
|
+
@password = password
|
18
8
|
end
|
19
9
|
|
20
|
-
def worksheet
|
21
|
-
Worksheet.new
|
10
|
+
def worksheet doc_title, worksheet_id
|
11
|
+
Worksheet.new doc_title, worksheet_id, GoogleSpreadsheet.login(@email, @password)
|
22
12
|
end
|
23
13
|
end
|
24
14
|
|
25
15
|
private
|
26
16
|
class Worksheet
|
27
17
|
attr_accessor :check_worksheet_size
|
28
|
-
def initialize
|
29
|
-
@
|
30
|
-
@doc_id = doc_id
|
31
|
-
@worksheet_id = worksheet_id
|
32
|
-
@access_token = access_token
|
18
|
+
def initialize doc_title, worksheet_title, session
|
19
|
+
@worksheet = session.spreadsheet_by_title(doc_title).worksheet_by_title(worksheet_title)
|
33
20
|
@items = []
|
34
|
-
@
|
35
|
-
@worksheet_href= "https://spreadsheets.google.com/feeds/list/#{@doc_id}/#{@worksheet_id}/private/full"
|
36
|
-
|
37
|
-
@query = {"access_token" => @access_token, :v => "3.0"}
|
38
|
-
@headers = {"Content-Type" => "application/atom+xml", "If-Match" => "*"}
|
21
|
+
@rows = []
|
39
22
|
end
|
40
23
|
|
41
24
|
def append item
|
42
|
-
@items << item
|
25
|
+
@items << (item.nil? ? "" : item)
|
43
26
|
end
|
44
27
|
|
45
28
|
def << item
|
46
29
|
append item
|
47
30
|
end
|
48
31
|
|
49
|
-
def
|
50
|
-
|
51
|
-
"xmlns:batch" => "http://schemas.google.com/gdata/batch",
|
52
|
-
"xmlns:gs" => "http://schemas.google.com/spreadsheets/2006"}
|
53
|
-
|
54
|
-
response = HTTParty.get(@worksheet_href, :query => @query)
|
55
|
-
|
56
|
-
row_id ||= parse(response.body)["feed"]["openSearch:totalResults"].to_i + 2
|
57
|
-
|
58
|
-
set_worksheet_size row_id if @check_worksheet_size
|
59
|
-
|
60
|
-
xml = builder.feed(xmlns) do |build|
|
61
|
-
build.id("#{@worksheet_cell_href}")
|
62
|
-
@items.each_with_index{|item, index|
|
63
|
-
col_index = index+1
|
64
|
-
col_id = "R#{row_id}C#{col_index}"
|
65
|
-
col_href="#{@worksheet_cell_href}/#{col_id}"
|
66
|
-
build.entry { |b|
|
67
|
-
b.send("batch:id", "A#{index}")
|
68
|
-
b.send("batch:operation", :type => "update")
|
69
|
-
b.id(col_href)
|
70
|
-
b.link(:rel => "edit", :type => "application/atom+xml", :href => col_href)
|
71
|
-
b.send("gs:cell", :row => row_id, :col => col_index, :inputValue => item)
|
72
|
-
}
|
73
|
-
}
|
74
|
-
end
|
75
|
-
response = HTTParty.post("#{@worksheet_cell_href}/batch", :headers => @headers, :query => @query, :body => xml)
|
76
|
-
raise Exception if response.code != 200
|
32
|
+
def next_row
|
33
|
+
@rows << @items unless @items.empty?
|
77
34
|
@items = []
|
78
35
|
end
|
79
36
|
|
80
|
-
def
|
81
|
-
|
82
|
-
|
83
|
-
row_count = worksheet["entry"]["gs:rowCount"].to_i
|
37
|
+
def save row_id = nil
|
38
|
+
next_row
|
39
|
+
row_id ||= @worksheet.num_rows + 1
|
84
40
|
|
85
|
-
|
41
|
+
@worksheet.max_rows = row_id + @rows.size
|
86
42
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
def parse content
|
96
|
-
Crack::XML.parse(content)
|
97
|
-
end
|
98
|
-
|
99
|
-
def alphabet
|
100
|
-
("A".."ZZ").to_a
|
43
|
+
@rows.each_with_index do |items, row_index|
|
44
|
+
items.each_with_index do |item, item_index|
|
45
|
+
@worksheet[row_id + row_index, item_index + 1] = item
|
46
|
+
end
|
47
|
+
end
|
48
|
+
put "Save failed" unless @worksheet.save
|
49
|
+
@items = []
|
101
50
|
end
|
102
51
|
|
103
|
-
def
|
104
|
-
|
52
|
+
def set_worksheet_size size
|
53
|
+
@worksheet.max_rows = size
|
105
54
|
end
|
106
55
|
end
|
107
56
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gsmetrics
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,48 +9,30 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-04-23 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
16
|
-
requirement:
|
15
|
+
name: google-spreadsheet-ruby
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
21
|
+
version: 0.2.1
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: builder
|
27
|
-
requirement: &21137020 !ruby/object:Gem::Requirement
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
25
|
none: false
|
29
26
|
requirements:
|
30
|
-
- -
|
27
|
+
- - ~>
|
31
28
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
33
|
-
type: :runtime
|
34
|
-
prerelease: false
|
35
|
-
version_requirements: *21137020
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: crack
|
38
|
-
requirement: &21136540 !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
40
|
-
requirements:
|
41
|
-
- - ! '>='
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: '0'
|
44
|
-
type: :runtime
|
45
|
-
prerelease: false
|
46
|
-
version_requirements: *21136540
|
29
|
+
version: 0.2.1
|
47
30
|
description: Simple Way to open a Session with Google Docs and push data into a Google
|
48
31
|
Spreadsheet Worksheet. We use it primarily for metrics, but it can be used for basically
|
49
32
|
anything regarding google spreadsheets.
|
50
33
|
email:
|
51
34
|
- flo@railsonfire.com
|
52
|
-
executables:
|
53
|
-
- gsmetrics
|
35
|
+
executables: []
|
54
36
|
extensions: []
|
55
37
|
extra_rdoc_files: []
|
56
38
|
files:
|
@@ -61,7 +43,6 @@ files:
|
|
61
43
|
- LICENSE
|
62
44
|
- README.md
|
63
45
|
- Rakefile
|
64
|
-
- bin/gsmetrics
|
65
46
|
- gsmetrics.gemspec
|
66
47
|
- lib/gsmetrics.rb
|
67
48
|
- lib/gsmetrics/gsmetrics.rb
|
@@ -79,18 +60,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
79
60
|
version: '0'
|
80
61
|
segments:
|
81
62
|
- 0
|
82
|
-
hash:
|
63
|
+
hash: 2891853343761736090
|
83
64
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
65
|
none: false
|
85
66
|
requirements:
|
86
67
|
- - ! '>='
|
87
68
|
- !ruby/object:Gem::Version
|
88
69
|
version: 1.3.6
|
89
|
-
requirements:
|
90
|
-
- Thor and launchy gem need to be installed for the executable. Not adding them as
|
91
|
-
a dependency for the whole library
|
70
|
+
requirements: []
|
92
71
|
rubyforge_project: gsmetrics
|
93
|
-
rubygems_version: 1.8.
|
72
|
+
rubygems_version: 1.8.19
|
94
73
|
signing_key:
|
95
74
|
specification_version: 3
|
96
75
|
summary: Gem for pushing data to Google Docs
|
data/bin/gsmetrics
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
require "thor"
|
3
|
-
require 'launchy'
|
4
|
-
require 'httparty'
|
5
|
-
require 'json'
|
6
|
-
require 'crack'
|
7
|
-
|
8
|
-
class GSMetrics < Thor
|
9
|
-
desc "setup", "Helps in Setting up a google Oauth Token"
|
10
|
-
def setup
|
11
|
-
say "Google Code API Console will open shortly."
|
12
|
-
say "Please create a new API Project and then go to API Access and create a new \"Installed Application\" Client ID"
|
13
|
-
sleep 5
|
14
|
-
Launchy.open("https://code.google.com/apis/console")
|
15
|
-
sleep 2
|
16
|
-
client_id = ask "Paste your new Client ID:"
|
17
|
-
client_secret = ask "Paste your new Client Secret:"
|
18
|
-
auth_query = {
|
19
|
-
:response_type => "code",
|
20
|
-
:client_id => client_id,
|
21
|
-
:redirect_uri => "urn:ietf:wg:oauth:2.0:oob",
|
22
|
-
:scope => "https://spreadsheets.google.com/feeds/"
|
23
|
-
}
|
24
|
-
Launchy.open("https://accounts.google.com/o/oauth2/auth?" + auth_query.map{|e| e.join("=")}.join("&"))
|
25
|
-
|
26
|
-
sleep 2
|
27
|
-
|
28
|
-
code = ask "Paste your new Code:"
|
29
|
-
|
30
|
-
token_query = {
|
31
|
-
:code => code,
|
32
|
-
:client_id => client_id,
|
33
|
-
:client_secret => client_secret,
|
34
|
-
:redirect_uri => "urn:ietf:wg:oauth:2.0:oob",
|
35
|
-
:grant_type => "authorization_code"
|
36
|
-
}
|
37
|
-
|
38
|
-
response = HTTParty.post("https://accounts.google.com/o/oauth2/token", :body => token_query)
|
39
|
-
|
40
|
-
tokens = JSON.parse(response.body)
|
41
|
-
|
42
|
-
refresh_token = tokens["refresh_token"]
|
43
|
-
|
44
|
-
say "Following is the code to create your GSMetrics Session. The last parameter is your Refresh Token"
|
45
|
-
|
46
|
-
say "GSMetrics::Session.new("
|
47
|
-
say " \"#{client_id}\","
|
48
|
-
say " \"#{client_secret}\","
|
49
|
-
say " \"#{refresh_token}\""
|
50
|
-
say ")"
|
51
|
-
|
52
|
-
say "\n\n"
|
53
|
-
|
54
|
-
document = ask "Id of Document you want to access:"
|
55
|
-
|
56
|
-
response = HTTParty.get("https://spreadsheets.google.com/feeds/worksheets/#{document}/private/full", :query => {"access_token" => tokens["access_token"], :v => "3.0"})
|
57
|
-
worksheets = Crack::XML.parse(response.body)
|
58
|
-
say "Code for creating a Worksheet"
|
59
|
-
worksheets["feed"]["entry"].each_with_index do |entry, index|
|
60
|
-
say "#{index}. #{entry["title"]} - session.worksheet(\"#{document}\", \"#{entry["id"].split("/").last}\")"
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
GSMetrics.start [:setup]
|