Class: GoogleTaskAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/gtasks.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GoogleTaskAPI

Returns a new instance of GoogleTaskAPI.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gtasks.rb', line 31

def initialize(options={})
  @client_id = options[:client_id] or raise ArgumentError.new
  @client_secret = options[:client_secret] or raise ArgumentError.new
  @logger = options[:logger] ||= Logger.new(nil)
  @callback_uri = options[:callback_uri] ||= GoogleTaskAPI::default_callback_uri
  @no_refresh = options[:no_refresh] ||= false

  @client = OAuth2::Client.new(
    @client_id, 
    @client_secret, 
    :site => 'https://www.googleapis.com/',
    :authorize_url => 'https://accounts.google.com/o/oauth2/auth',
    :token_url => 'https://accounts.google.com/o/oauth2/token')

  refresh_or_auth GoogleTaskAPI.default_dat_path

  if block_given?
    yield self
  end
end

Class Method Details

.default_callback_uriObject



20
21
22
23
24
25
26
27
28
# File 'lib/gtasks.rb', line 20

def default_callback_uri
  scheme = "http"
  hostname = `hostname`.chomp
  port = 9999
  callback = '/oauth_callback'

  base_url = "#{scheme}://#{hostname}:#{port}"
  URI.join(base_url, callback).to_s
end

.default_dat_pathObject



16
17
18
# File 'lib/gtasks.rb', line 16

def default_dat_path
  File.join(ENV['HOME'], ".google_tasks.yml")
end

Instance Method Details

#refresh_or_auth(dat_path) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/gtasks.rb', line 121

def refresh_or_auth(dat_path)
  @dat_path = dat_path
  @logger.info "dat_path is #{@dat_path.inspect}"
  if File.exists? @dat_path
    @logger.info "dat_path already exist"
    load_from_file @dat_path # load access token
    @logger.info "loaded"

    if @token.refresh_token.nil?
      @logger.info "retry auth"
      auth @callback_uri
      @logger.info "successful auth"

      @logger.info "save to #{@dat_path}"
      save_to_file @dat_path
      @logger.info "saved"
    else
      @logger.info "try to refresh"
      refresh @dat_path # refresh and save access token
      @logger.info "successful refresh"
    end
  else
    begin
      @logger.info "try to oauth2 authentication"
      auth @callback_uri # generate access token
      @logger.info "successful oauth2 authentication"

      @logger.info "save to #{@dat_path}"
      save_to_file @dat_path # save token
      @logger.info "saved"
    rescue => ex
      FileUtils.rm @dat_path if File.exists? @dat_path
      @logger.info "error detect. remove #{@dat_path.inspect}"
      @logger.info "error information: #{ex.inspect}"
    end
  end
end

#task_clear(tasklist_ident) ⇒ Object

完了済みタスクの全削除



88
89
90
# File 'lib/gtasks.rb', line 88

def task_clear(tasklist_ident)
  @token.post("/tasks/v1/lists/#{tasklist_ident}/clear").parsed
end

#task_delete(tasklist_ident, task_ident) ⇒ Object



78
79
80
# File 'lib/gtasks.rb', line 78

def task_delete(tasklist_ident, task_ident)
  @token.delete("/tasks/v1/lists/#{tasklist_ident}/tasks/#{task_ident}")
end

#task_get(tasklist_ident, task_ident) ⇒ Object



57
58
59
# File 'lib/gtasks.rb', line 57

def task_get(tasklist_ident, task_ident)
  @token.get("/tasks/v1/lists/#{tasklist_ident}/tasks/#{task_ident}").parsed
end

#task_insert(tasklist_ident, params = {}) ⇒ Object



61
62
63
64
65
66
# File 'lib/gtasks.rb', line 61

def task_insert(tasklist_ident, params={})
  @token.post("/tasks/v1/lists/#{tasklist_ident}/tasks"){ |req|
    req.headers["Content-Type"] = "application/json"
    req.body = params.to_json
  }
end

#task_list(tasklist_ident = '@default') ⇒ Object



53
54
55
# File 'lib/gtasks.rb', line 53

def task_list(tasklist_ident='@default')
  @token.get("/tasks/v1/lists/#{tasklist_ident}/tasks").parsed
end

#task_move(tasklist_ident, task_ident) ⇒ Object

ORDER系



83
84
85
# File 'lib/gtasks.rb', line 83

def task_move(tasklist_ident, task_ident)
  # TODO
end

#task_update(tasklist_ident, task_ident, opts = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/gtasks.rb', line 68

def task_update(tasklist_ident, task_ident, opts={})
  task_info = task_get(tasklist_ident, task_ident)
  params = task_info.merge(opts)

  @token.put("/tasks/v1/lists/#{tasklist_ident}/tasks/#{task_ident}"){ |req|
    req.headers["Content-Type"] = "application/json"
    req.body = params.to_json
  }
end

#tasklist_delete(username, tasklist_ident) ⇒ Object



117
118
119
# File 'lib/gtasks.rb', line 117

def tasklist_delete(username, tasklist_ident)
  @token.delete("/tasks/v1/users/#{username}/lists/#{tasklist_ident}")
end

#tasklist_get(username, ident) ⇒ Object



96
97
98
# File 'lib/gtasks.rb', line 96

def tasklist_get(username, ident)
  @token.get("/tasks/v1/users/#{username}/lists/#{ident}").parsed
end

#tasklist_insert(username, listname) ⇒ Object



100
101
102
103
104
105
# File 'lib/gtasks.rb', line 100

def tasklist_insert(username, listname)
  @token.post("/tasks/v1/users/#{username}/lists"){ |req|
    req.headers["Content-Type"] = "application/json"
    req.body = {:title => listname}.to_json
  }
end

#tasklist_list(username = '@me') ⇒ Object



92
93
94
# File 'lib/gtasks.rb', line 92

def tasklist_list(username = '@me')
  @token.get("/tasks/v1/users/#{username}/lists").parsed
end

#tasklist_update(username, tasklist_ident, params = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/gtasks.rb', line 107

def tasklist_update(username, tasklist_ident, params={})
  info = tasklist_get(username, tasklist_ident)
  params = info.merge(params)

  @token.put("/tasks/v1/users/#{username}/lists/#{tasklist_ident}"){ |req|
    req.headers["Content-Type"] = "application/json"
    req.body = params.to_json
  }
end