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 CHANGED
@@ -1,5 +1,5 @@
1
- ## 0.0.1 (July 10, 2010)
1
+ ## 0.1.0 (April 22, 2012)
2
2
 
3
3
  Features:
4
4
 
5
- - initial import of this dummy gem
5
+ - Using google-spreadsheet-ruby gem for uploading user data
data/Gemfile CHANGED
@@ -1,7 +1 @@
1
- gemspec
2
-
3
- gem 'httparty'
4
- gem 'builder'
5
- gem 'thor'
6
- gem 'launchy'
7
- gem 'crack'
1
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,32 +1,33 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gsmetrics (0.0.4)
5
- builder
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.6)
12
- builder (3.0.0)
13
- crack (0.1.8)
14
- httparty (0.8.1)
15
- multi_json
16
- multi_xml
17
- launchy (2.0.5)
18
- addressable (~> 2.2.6)
19
- multi_json (1.0.4)
20
- multi_xml (0.4.1)
21
- thor (0.14.6)
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 save the row:
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(client_id, client_secret, refresh_token)
22
- worksheet = session.worksheet document_id, worksheet_id
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.save_row
18
+ worksheet.next_row
19
+ worksheet.append(10)
20
+ worksheet.save
26
21
  ```
27
22
 
28
- Every row is added in one batch call, so only one HTTP request is sent for every row.
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
- 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 repeatedly.
25
+ The whole worksheet is updated in one batch call, so only one HTTP request is sent.
31
26
 
32
- The following example saves the worksheet to row nr 1. The rows start with 1 in Googledocs.
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 sets the number of rows in the worksheet beforehand and then disables the check if there is still a free row. By default gsmetrics checks
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
- set_worksheet_size 5
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
- 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.
60
- The executable will give you a list of all worksheets for your document at the end of the setup process.
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.4"
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.add_runtime_dependency(%q<httparty>, [">= 0"])
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"
@@ -1,107 +1,56 @@
1
- require 'builder'
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 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"]
5
+ def initialize email, password
6
+ @email = email
7
+ @password = password
18
8
  end
19
9
 
20
- def worksheet doc_id, worksheet_id
21
- Worksheet.new doc_id, worksheet_id, @access_token
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 doc_id, worksheet_id, access_token
29
- @check_worksheet_size = true
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
- @worksheet_cell_href = "https://spreadsheets.google.com/feeds/cells/#{@doc_id}/#{@worksheet_id}/private/full"
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 save_row row_id = nil
50
- xmlns = {:xmlns => "http://www.w3.org/2005/Atom",
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 set_worksheet_size row_id
81
- href = "https://spreadsheets.google.com/feeds/worksheets/#{@doc_id}/private/full/#{@worksheet_id}"
82
- worksheet = parse(HTTParty.get(href, :query => @query))
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
- update_href = worksheet["entry"]["link"].last["href"]
41
+ @worksheet.max_rows = row_id + @rows.size
86
42
 
87
- add = builder.entry(:xmlns => "http://www.w3.org/2005/Atom", "xmlns:gs" => "http://schemas.google.com/spreadsheets/2006"){|e|
88
- e.send("gs:rowCount", row_id)
89
- e.send("gs:colCount", worksheet["entry"]["gs:colCount"].to_i)
90
- e.title(worksheet["entry"]["title"])
91
- }
92
- HTTParty.put(update_href, :query => @query, :headers => @headers, :body => add) if row_count < row_id
93
- end
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 builder
104
- Builder::XmlMarkup.new
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
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-01-24 00:00:00.000000000Z
12
+ date: 2012-04-23 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: httparty
16
- requirement: &21137520 !ruby/object:Gem::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: '0'
21
+ version: 0.2.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *21137520
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: '0'
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: 577015747898279807
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.10
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]