google_tasks 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'httparty'
4
+ gem 'sinatra'
5
+ gem 'signet'
6
+ gem 'launchy'
7
+
8
+ gemspec
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # Google Tasks
2
+
3
+ This gem provides access to google tasks apis
4
+
5
+ ## Install
6
+
7
+ ``` sh
8
+ $ gem install google_tasks
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Before start you need **client_id**, **client_secret** and **api_key**, you found it [here](https://code.google.com/apis/console)
14
+
15
+ ``` rb
16
+ google_tasks = GoogleTasks.new('client_id', 'client_secret', 'api_key')
17
+ google_tasks.lists.items.map(&:title) # => ["List 1", "List 2", "List 3"]
18
+ list_id = google_tasks.list.first.id
19
+ google_tasks.lists(:delete, list_id)
20
+ tasks = google_tasks.tasks(:list, list_id)
21
+ ```
22
+
23
+ You can find more information on [Api Explorer](http://code.google.com/apis/explorer/#_s=tasks&_v=v1)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "google_tasks/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "google_tasks"
7
+ s.version = GoogleTasks::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["DAddYE"]
10
+ s.email = ["d.dagostino@lipsiasoft.com"]
11
+ s.homepage = "https://github.com/daddye/google_tasks"
12
+ s.summary = %q{This gem provides access to google tasks apis}
13
+ s.description = %q{This gem provides access to google tasks apis}
14
+
15
+ s.rubyforge_project = "google_tasks"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,10 @@
1
+ module GoogleTasks
2
+ autoload :Client, 'google_tasks/client'
3
+ autoload :Login, 'google_tasks/login'
4
+ autoload :Error, 'google_tasks/error'
5
+ autoload :Response, 'google_tasks/response'
6
+
7
+ def self.new(client_id, client_secret, api_key)
8
+ Client.new(client_id, client_secret, api_key)
9
+ end
10
+ end # GoogleTasks
@@ -0,0 +1,76 @@
1
+ require 'httparty'
2
+
3
+ module GoogleTasks
4
+ class Client
5
+ include HTTParty
6
+
7
+ API_URL = 'https://www.googleapis.com/tasks/'
8
+ API_VERSION = 'v1'
9
+ CONFIG = File.expand_path("~/.google-tasks")
10
+
11
+ base_uri "#{API_URL}#{API_VERSION}"
12
+ format :json
13
+
14
+ def initialize(client_id, client_secret, api_key)
15
+ @client_id, @client_secret, @api_key = client_id, client_secret, api_key
16
+ @token = File.read(CONFIG) rescue Login.authenticate(@client_id, @client_secret)
17
+ File.open(CONFIG, "w") { |f| f.write(@token) }
18
+ self.class.headers.merge!("Authorization" => "OAuth #{@token}")
19
+ end
20
+
21
+ %w(get update post delete patch).each do |m|
22
+ define_method(m) do |options|
23
+ options.merge!(:api_key => @api_key)
24
+ path = options.delete(:path)
25
+ resp = parse_respose(self.class.send(m, path, :query => options))
26
+ if resp[:error]
27
+ if resp[:error][:code] == 401
28
+ @token = Login.authenticate(@client_id, @client_secret)
29
+ File.open(CONFIG, "w") { |f| f.write(@token) }
30
+ self.class.headers.merge!("Authorization" => "OAuth #{@token}")
31
+ resp = parse_respose self.class.send(m, path, :query => options)
32
+ elsif resp[:error][:message]
33
+ raise resp[:error][:message]
34
+ end
35
+ end
36
+ resp
37
+ end
38
+ end
39
+
40
+ def lists(method=:list, list_id=nil, options={})
41
+ path = "/users/@me/lists"
42
+ path << "/#{list_id}" if list_id
43
+ options.merge!(:path => path)
44
+ send(operations[method], options)
45
+ end
46
+
47
+ def tasks(method, list_id, task_id=nil, options={})
48
+ path = "/lists/#{list_id}"
49
+ path << case method
50
+ when :clear then "/clear"
51
+ when :move then "/tasks/move"
52
+ else "/tasks"
53
+ end
54
+ path << "/#{taks_id}" if task_id
55
+ options.merge!(:path => path)
56
+ send(operations[method], options)
57
+ end
58
+
59
+ private
60
+ def parse_respose(response)
61
+ GoogleTasks::Response.new(response.parsed_response)
62
+ end
63
+
64
+ def operations
65
+ @_operations ||= {
66
+ :delete => :delete,
67
+ :get => :get,
68
+ :insert => :post,
69
+ :list => :get,
70
+ :update => :update,
71
+ :clear => :post,
72
+ :move => :update
73
+ }
74
+ end
75
+ end # Client
76
+ end # GoogleTasks
@@ -0,0 +1,37 @@
1
+ require 'signet/oauth_2/client'
2
+ require 'sinatra/base'
3
+ require 'launchy'
4
+
5
+ module GoogleTasks
6
+
7
+ module Login
8
+ extend self
9
+
10
+ def authenticate(client_id, client_secret)
11
+ server = Sinatra.new do
12
+ set :code, ""
13
+ get "/" do
14
+ settings.code = params[:code]
15
+ Process.kill(:INT, Process.pid)
16
+ "Thanks! You may close this!"
17
+ end
18
+ end
19
+ client = Signet::OAuth2::Client.new(
20
+ :authorization_uri => 'https://www.google.com/accounts/o8/oauth2/authorization',
21
+ :token_credential_uri => 'https://www.google.com/accounts/o8/oauth2/token',
22
+ :client_id => client_id,
23
+ :client_secret => client_secret,
24
+ :redirect_uri => 'http://localhost:12736/',
25
+ :scope => 'https://www.googleapis.com/auth/tasks'
26
+ )
27
+ Launchy::Browser.run(client.authorization_uri.to_s)
28
+ puts "... waiting browser"
29
+ $stdout = $stderr = log_buffer = StringIO.new
30
+ server.run!(:port => 12736, :host => '0.0.0.0')
31
+ $stdout, $stderr = STDOUT, STDERR
32
+ client.code = server.code
33
+ client.fetch_access_token!
34
+ client.access_token
35
+ end
36
+ end # ClientLogin
37
+ end # GoogleTasks
@@ -0,0 +1,23 @@
1
+ module GoogleTasks
2
+
3
+ class Response < Hash
4
+ def initialize(hash)
5
+ self.update(hash || {})
6
+ self.keys.each do |k|
7
+ new_key = (k.to_sym rescue k) || k
8
+ self[new_key] = format_value(delete(k))
9
+ self.class.send(:define_method, new_key) { self[new_key] }
10
+ end
11
+ super
12
+ end
13
+
14
+ private
15
+ def format_value(value)
16
+ case value
17
+ when Hash then Response.new(value)
18
+ when Array then value.map { |v| format_value(v) }
19
+ else value
20
+ end
21
+ end
22
+ end # Response
23
+ end # GoogleTasks
@@ -0,0 +1,3 @@
1
+ module GoogleTasks
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_tasks
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - DAddYE
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-29 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: This gem provides access to google tasks apis
23
+ email:
24
+ - d.dagostino@lipsiasoft.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README.md
35
+ - Rakefile
36
+ - google_tasks.gemspec
37
+ - lib/google_tasks.rb
38
+ - lib/google_tasks/client.rb
39
+ - lib/google_tasks/login.rb
40
+ - lib/google_tasks/response.rb
41
+ - lib/google_tasks/version.rb
42
+ has_rdoc: true
43
+ homepage: https://github.com/daddye/google_tasks
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project: google_tasks
72
+ rubygems_version: 1.6.2
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: This gem provides access to google tasks apis
76
+ test_files: []
77
+