gsmetrics 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +27 -0
- data/LICENSE +20 -0
- data/README.md +32 -0
- data/Rakefile +31 -0
- data/bin/gsmetrics +53 -0
- data/gsmetrics.gemspec +29 -0
- data/lib/gem/gsmetrics.rb +100 -0
- data/lib/gsmetrics.rb +1 -0
- metadata +62 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
gsmetrics (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
specs:
|
8
|
+
addressable (2.2.6)
|
9
|
+
builder (3.0.0)
|
10
|
+
httparty (0.8.1)
|
11
|
+
multi_json
|
12
|
+
multi_xml
|
13
|
+
launchy (2.0.5)
|
14
|
+
addressable (~> 2.2.6)
|
15
|
+
multi_json (1.0.3)
|
16
|
+
multi_xml (0.4.1)
|
17
|
+
thor (0.14.6)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
builder
|
24
|
+
gsmetrics!
|
25
|
+
httparty
|
26
|
+
launchy
|
27
|
+
thor
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Florian Motlik
|
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.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
GSMetrics is a simple Gem to help you with pushing data into Google Spreadsheets.
|
2
|
+
|
3
|
+
Development was started so we could run a cron job in regular intervals and push metrics data about Railsonfire to a Google Spreadsheet.
|
4
|
+
|
5
|
+
To start using it simply run
|
6
|
+
|
7
|
+
gsmetrics
|
8
|
+
|
9
|
+
and follow the instructions given. You will have to create a new google API Project and after
|
10
|
+
finishing all steps get code you can simply paste into your codebase.
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
Create a new GSMetrics Session and a worksheet, then append to it and save the row:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
session = GSMetrics::Session.new(client_id, client_secret, refresh_token)
|
18
|
+
worksheet = session.worksheet document_id, worksheet_id
|
19
|
+
worksheet << 1
|
20
|
+
worksheet.append(5)
|
21
|
+
worksheet.save_row
|
22
|
+
```
|
23
|
+
|
24
|
+
You can find out what your worksheet_id is by going to
|
25
|
+
|
26
|
+
https://spreadsheets.google.com/feeds/worksheets/#{your_document_id}/private/full
|
27
|
+
|
28
|
+
For every worksheet there will be an <entry> element which has an id. The last part of the id (something like od6) is the worksheet_id you have to provide.
|
29
|
+
If you find a better way to get that information or would implement an extension to the executable to go over the api and get that information you are very welcome.
|
30
|
+
|
31
|
+
Copyright (c) 2011 Florian Motlik
|
32
|
+
Based on the gem template by https://github.com/goncalossilva/gem_template
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
begin
|
2
|
+
require "bundler"
|
3
|
+
Bundler.setup
|
4
|
+
rescue LoadError
|
5
|
+
$stderr.puts "You need to have Bundler installed to be able build this gem."
|
6
|
+
end
|
7
|
+
|
8
|
+
gemspec = eval(File.read(Dir["*.gemspec"].first))
|
9
|
+
|
10
|
+
|
11
|
+
desc "Validate the gemspec"
|
12
|
+
task :gemspec do
|
13
|
+
gemspec.validate
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Build gem locally"
|
17
|
+
task :build => :gemspec do
|
18
|
+
system "gem build #{gemspec.name}.gemspec"
|
19
|
+
FileUtils.mkdir_p "pkg"
|
20
|
+
FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", "pkg"
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "Install gem locally"
|
24
|
+
task :install => :build do
|
25
|
+
system "gem install pkg/#{gemspec.name}-#{gemspec.version}"
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Clean automatically generated files"
|
29
|
+
task :clean do
|
30
|
+
FileUtils.rm_rf "pkg"
|
31
|
+
end
|
data/bin/gsmetrics
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "thor"
|
3
|
+
require 'launchy'
|
4
|
+
require 'httparty'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
class GSMetrics < Thor
|
8
|
+
desc "setup", "Helps in Setting up a google Oauth Token"
|
9
|
+
def setup
|
10
|
+
say "Google Code API Console will open shortly."
|
11
|
+
say "Please create a new API Project and then go to API Access and create a new \"Installed Application\" Client ID"
|
12
|
+
sleep 5
|
13
|
+
Launchy.open("https://code.google.com/apis/console")
|
14
|
+
sleep 2
|
15
|
+
client_id = ask "Paste your new Client ID:"
|
16
|
+
client_secret = ask "Paste your new Client Secret:"
|
17
|
+
auth_query = {
|
18
|
+
:response_type => "code",
|
19
|
+
:client_id => client_id,
|
20
|
+
:redirect_uri => "urn:ietf:wg:oauth:2.0:oob",
|
21
|
+
:scope => "https://spreadsheets.google.com/feeds/"
|
22
|
+
}
|
23
|
+
Launchy.open("https://accounts.google.com/o/oauth2/auth?" + auth_query.map{|e| e.join("=")}.join("&"))
|
24
|
+
|
25
|
+
sleep 2
|
26
|
+
|
27
|
+
code = ask "Paste your new Code:"
|
28
|
+
|
29
|
+
token_query = {
|
30
|
+
:code => code,
|
31
|
+
:client_id => client_id,
|
32
|
+
:client_secret => client_secret,
|
33
|
+
:redirect_uri => "urn:ietf:wg:oauth:2.0:oob",
|
34
|
+
:grant_type => "authorization_code"
|
35
|
+
}
|
36
|
+
|
37
|
+
response = HTTParty.post("https://accounts.google.com/o/oauth2/token", :body => token_query)
|
38
|
+
|
39
|
+
tokens = JSON.parse(response.body)
|
40
|
+
|
41
|
+
refresh_token = tokens["refresh_token"]
|
42
|
+
|
43
|
+
say "Following is the code to create your GSMetrics Session. The last parameter is your Refresh Token"
|
44
|
+
|
45
|
+
say "GSMetrics::Session.new("
|
46
|
+
say " \"#{client_id}\","
|
47
|
+
say " \"#{client_secret}\","
|
48
|
+
say " \"#{refresh_token}\""
|
49
|
+
say ")"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
GSMetrics.start [:setup]
|
data/gsmetrics.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "gsmetrics"
|
3
|
+
s.version = "0.0.1"
|
4
|
+
s.platform = Gem::Platform::RUBY
|
5
|
+
s.authors = ["Florian Motlik"]
|
6
|
+
s.email = ["flo@railsonfire.com"]
|
7
|
+
s.homepage = "http://github.com/flomotlik/gsmetrics"
|
8
|
+
s.summary = "Gem for pushing data to Google Docs"
|
9
|
+
s.description = "Simple Way to open a Session with Google Docs and push data into a Google Spreadsheet Worksheet. We use it primarily for metrics, but it can be used for basically anything regarding google spreadsheets."
|
10
|
+
s.rubyforge_project = s.name
|
11
|
+
|
12
|
+
s.required_rubygems_version = ">= 1.3.6"
|
13
|
+
|
14
|
+
# If you have runtime dependencies, add them here
|
15
|
+
# s.add_runtime_dependency "other", "~> 1.2"
|
16
|
+
|
17
|
+
# If you have development dependencies, add them here
|
18
|
+
# s.add_development_dependency "another", "= 0.9"
|
19
|
+
|
20
|
+
# The list of files to be contained in the gem
|
21
|
+
s.files = `git ls-files`.split("\n")
|
22
|
+
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
23
|
+
# s.extensions = `git ls-files ext/extconf.rb`.split("\n")
|
24
|
+
|
25
|
+
s.require_path = 'lib'
|
26
|
+
|
27
|
+
# For C extensions
|
28
|
+
# s.extensions = "ext/extconf.rb"
|
29
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'builder'
|
2
|
+
require 'httparty'
|
3
|
+
require 'json'
|
4
|
+
require 'crack'
|
5
|
+
|
6
|
+
module GSMetrics
|
7
|
+
class Session
|
8
|
+
def initialize client_id, client_secret, refresh_token
|
9
|
+
refresh_request = {
|
10
|
+
:client_id => client_id,
|
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"]
|
18
|
+
end
|
19
|
+
|
20
|
+
def worksheet doc_id, worksheet_id
|
21
|
+
Worksheet.new doc_id, worksheet_id, @access_token
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
class Worksheet
|
27
|
+
def initialize doc_id, worksheet_id, access_token
|
28
|
+
@doc_id = doc_id
|
29
|
+
@worksheet_id = worksheet_id
|
30
|
+
@access_token = access_token
|
31
|
+
@items = []
|
32
|
+
@worksheet_cell_href = "https://spreadsheets.google.com/feeds/cells/#{@doc_id}/#{@worksheet_id}/private/full"
|
33
|
+
@worksheet_href= "https://spreadsheets.google.com/feeds/list/#{@doc_id}/#{@worksheet_id}/private/full"
|
34
|
+
|
35
|
+
@query = {"access_token" => @access_token, :v => "3.0"}
|
36
|
+
@headers = {"Content-Type" => "application/atom+xml", "If-Match" => "*"}
|
37
|
+
end
|
38
|
+
|
39
|
+
def append item
|
40
|
+
@items << item
|
41
|
+
end
|
42
|
+
|
43
|
+
def << item
|
44
|
+
append item
|
45
|
+
end
|
46
|
+
|
47
|
+
def save_row
|
48
|
+
xmlns = {:xmlns => "http://www.w3.org/2005/Atom",
|
49
|
+
"xmlns:gs" => "http://schemas.google.com/spreadsheets/2006"}
|
50
|
+
|
51
|
+
response = HTTParty.get(@worksheet_href, :query => @query)
|
52
|
+
|
53
|
+
row_id = parse(response.body)["feed"]["openSearch:totalResults"].to_i + 2
|
54
|
+
|
55
|
+
create_row row_id
|
56
|
+
|
57
|
+
@items.each_with_index{|item, index|
|
58
|
+
col_index = index+1
|
59
|
+
col_id = "R#{row_id}C#{col_index}"
|
60
|
+
col_href="#{@worksheet_cell_href}/#{col_id}"
|
61
|
+
xml = builder.entry(xmlns
|
62
|
+
) { |b|
|
63
|
+
b.id(col_href)
|
64
|
+
b.link(:rel => "edit", :type => "application/atom+xml", :href => col_href)
|
65
|
+
b.send("gs:cell", :row => row_id, :col => col_index, :inputValue => item)
|
66
|
+
}
|
67
|
+
HTTParty.put(col_href, :headers => @headers, :query => @query, :body => xml)
|
68
|
+
}
|
69
|
+
@items = []
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
def create_row row_id
|
74
|
+
href = "https://spreadsheets.google.com/feeds/worksheets/#{@doc_id}/private/full/#{@worksheet_id}"
|
75
|
+
worksheet = parse(HTTParty.get(href, :query => @query))
|
76
|
+
row_count = worksheet["entry"]["gs:rowCount"].to_i
|
77
|
+
|
78
|
+
update_href = worksheet["entry"]["link"].last["href"]
|
79
|
+
|
80
|
+
add = builder.entry(:xmlns => "http://www.w3.org/2005/Atom", "xmlns:gs" => "http://schemas.google.com/spreadsheets/2006"){|e|
|
81
|
+
e.send("gs:rowCount", row_count + 1)
|
82
|
+
e.send("gs:colCount", worksheet["entry"]["gs:colCount"].to_i)
|
83
|
+
e.title(worksheet["entry"]["title"])
|
84
|
+
}
|
85
|
+
HTTParty.put(update_href, :query => @query, :headers => @headers, :body => add) if row_count < row_id
|
86
|
+
end
|
87
|
+
|
88
|
+
def parse content
|
89
|
+
Crack::XML.parse(content)
|
90
|
+
end
|
91
|
+
|
92
|
+
def alphabet
|
93
|
+
("A".."ZZ").to_a
|
94
|
+
end
|
95
|
+
|
96
|
+
def builder
|
97
|
+
Builder::XmlMarkup.new
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/lib/gsmetrics.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "gem/gsmetrics"
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gsmetrics
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Florian Motlik
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-29 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Simple Way to open a Session with Google Docs and push data into a Google
|
15
|
+
Spreadsheet Worksheet. We use it primarily for metrics, but it can be used for basically
|
16
|
+
anything regarding google spreadsheets.
|
17
|
+
email:
|
18
|
+
- flo@railsonfire.com
|
19
|
+
executables:
|
20
|
+
- gsmetrics
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- .gitignore
|
25
|
+
- CHANGELOG.md
|
26
|
+
- Gemfile
|
27
|
+
- Gemfile.lock
|
28
|
+
- LICENSE
|
29
|
+
- README.md
|
30
|
+
- Rakefile
|
31
|
+
- bin/gsmetrics
|
32
|
+
- gsmetrics.gemspec
|
33
|
+
- lib/gem/gsmetrics.rb
|
34
|
+
- lib/gsmetrics.rb
|
35
|
+
homepage: http://github.com/flomotlik/gsmetrics
|
36
|
+
licenses: []
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
hash: -4567037286824327209
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 1.3.6
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project: gsmetrics
|
58
|
+
rubygems_version: 1.8.10
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: Gem for pushing data to Google Docs
|
62
|
+
test_files: []
|