Class: Weekdone::Api

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

Constant Summary collapse

API_URL =
'https://api.weekdone.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, loglevel: Logger::DEBUG) ⇒ Api

Returns a new instance of Api.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/weekdone/api.rb', line 14

def initialize(client_id, client_secret, loglevel: Logger::DEBUG)
  @logger = Logger.new(STDERR)
  @logger.level = loglevel

  @client = OAuth2::Client.new(
    client_id, client_secret,
    {
      site: 'https://weekdone.com',
      authorize_url: 'oauth_authorize',
      token_url: 'oauth_token',
      redirect_uri: 'http://localhost:8080/oauth2/callback'
    }
  )
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/weekdone/api.rb', line 11

def client
  @client
end

#loglevelObject

Returns the value of attribute loglevel.



12
13
14
# File 'lib/weekdone/api.rb', line 12

def loglevel
  @loglevel
end

#token_codeObject

Returns the value of attribute token_code.



12
13
14
# File 'lib/weekdone/api.rb', line 12

def token_code
  @token_code
end

Instance Method Details

#addItemCommentObject

Raises:

  • (NotImplementedError)


134
135
136
# File 'lib/weekdone/api.rb', line 134

def addItemComment
  raise NotImplementedError
end

#addItemLikeObject

Raises:

  • (NotImplementedError)


113
114
115
# File 'lib/weekdone/api.rb', line 113

def addItemLike
  raise NotImplementedError
end

#addObjectiveCommentObject

Raises:

  • (NotImplementedError)


246
247
248
# File 'lib/weekdone/api.rb', line 246

def addObjectiveComment
  raise NotImplementedError
end

#addObjectiveResultObject

Raises:

  • (NotImplementedError)


258
259
260
# File 'lib/weekdone/api.rb', line 258

def addObjectiveResult
  raise NotImplementedError
end

#assignItemToAnotherUserObject

Raises:

  • (NotImplementedError)


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

def assignItemToAnotherUser
  raise NotImplementedError
end

#authenticateObject



29
30
31
32
33
34
35
36
# File 'lib/weekdone/api.rb', line 29

def authenticate
  puts "open URL to authorize:"
  puts authorization_request

  print "\ninput authorization code: "
  auth_code = gets.chomp
  authorization_grant(auth_code)
end

#authorization_grant(auth_code) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/weekdone/api.rb', line 42

def authorization_grant(auth_code)
  @token = client.auth_code.get_token(
    auth_code,
    redirect_uri: 'http://localhost:8080/oauth2/authorized'
  )

  @token_code = @token.token
end

#authorization_requestObject



38
39
40
# File 'lib/weekdone/api.rb', line 38

def authorization_request
  @client.auth_code.authorize_url
end

#createItemObject

Raises:

  • (NotImplementedError)


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

def createItem
  raise NotImplementedError
end

#createNewObjectiveObject

Raises:

  • (NotImplementedError)


225
226
227
# File 'lib/weekdone/api.rb', line 225

def createNewObjective
  raise NotImplementedError
end

#deleteItemObject

Raises:

  • (NotImplementedError)


100
101
102
# File 'lib/weekdone/api.rb', line 100

def deleteItem
  raise NotImplementedError
end

#deleteItemCommentObject

Raises:

  • (NotImplementedError)


138
139
140
# File 'lib/weekdone/api.rb', line 138

def deleteItemComment
  raise NotImplementedError
end

#deleteItemLikeObject

Raises:

  • (NotImplementedError)


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

def deleteItemLike
  raise NotImplementedError
end

#deleteObjectiveObject

Raises:

  • (NotImplementedError)


233
234
235
# File 'lib/weekdone/api.rb', line 233

def deleteObjective
  raise NotImplementedError
end

#deleteObjectiveCommentObject

Raises:

  • (NotImplementedError)


254
255
256
# File 'lib/weekdone/api.rb', line 254

def deleteObjectiveComment
  raise NotImplementedError
end

#deleteObjectiveResultObject

Raises:

  • (NotImplementedError)


266
267
268
# File 'lib/weekdone/api.rb', line 266

def deleteObjectiveResult
  raise NotImplementedError
end

#getAllObjectives(type: nil, departmentid: nil, teamid: nil, userid: nil, period: nil) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/weekdone/api.rb', line 210

def getAllObjectives(type: nil, departmentid: nil, teamid: nil, userid: nil, period: nil)
  refresh

  params = { token: @token_code }
  params[:type] = type if not type.nil?
  params[:department_id] = departmentid if not departmentid.nil?
  params[:team_id] = teamid if not teamid.nil?
  params[:user_id] = userid if not userid.nil?
  params[:period] = period if not period.nil?

  @logger.debug("params: #{params}")
  response = Faraday.get(API_URL + '/1/objective', params)
  JSON.parse(response.body)
end

#getAllTagsObject



183
184
185
186
187
188
189
190
# File 'lib/weekdone/api.rb', line 183

def getAllTags
  refresh

  params = { token: @token_code }

  response = Faraday.get(API_URL + '/1/tag', params)
  JSON.parse(response.body)
end

#getAllTeamsObject



153
154
155
156
157
158
159
160
# File 'lib/weekdone/api.rb', line 153

def getAllTeams
  refresh

  params = { token: @token_code }

  response = Faraday.get(API_URL + '/1/teams', params)
  JSON.parse(response.body)
end

#getAllTypesObject



173
174
175
176
177
178
179
180
# File 'lib/weekdone/api.rb', line 173

def getAllTypes
  refresh

  params = { token: @token_code }

  response = Faraday.get(API_URL + '/1/types', params)
  JSON.parse(response.body)
end

#getAllUsersObject



163
164
165
166
167
168
169
170
# File 'lib/weekdone/api.rb', line 163

def getAllUsers
  refresh

  params = { token: @token_code }

  response = Faraday.get(API_URL + '/1/users', params)
  JSON.parse(response.body)
end

#getCompanyInfoObject



271
272
273
274
275
276
277
278
# File 'lib/weekdone/api.rb', line 271

def getCompanyInfo
  refresh

  params = { token: @token_code }

  response = Faraday.get(API_URL + '/1/company', params)
  JSON.parse(response.body)
end

#getItemComments(item_id) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/weekdone/api.rb', line 125

def getItemComments(item_id)
  refresh

  params = { token: @token_code }

  response = Faraday.get(API_URL + "/1/item/#{item_id}/comments", params)
  JSON.parse(response.body)
end

#getItemLikes(item_id) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/weekdone/api.rb', line 104

def getItemLikes(item_id)
  refresh

  params = { token: @token_code }

  response = Faraday.get(API_URL + "/1/item/#{item_id}/likes", params)
  JSON.parse(response.body)
end

#getReportObject



143
144
145
146
147
148
149
150
# File 'lib/weekdone/api.rb', line 143

def getReport
  refresh

  params = { token: @token_code }

  response = Faraday.get(API_URL + '/1/report', params)
  JSON.parse(response.body)
end

#getSingleTag(tag_id) ⇒ Object



192
193
194
195
196
197
198
199
# File 'lib/weekdone/api.rb', line 192

def getSingleTag(tag_id)
  refresh

  params = { token: @token_code }

  response = Faraday.get(API_URL + "/1/tag/#{tag_id}", params)
  JSON.parse(response.body)
end

#listObjectiveComments(objective_id) ⇒ Object



237
238
239
240
241
242
243
244
# File 'lib/weekdone/api.rb', line 237

def listObjectiveComments(objective_id)
  refresh

  params = { token: @token_code }

  response = Faraday.get(API_URL + "/1/objective/#{objective_id}/comments", params)
  JSON.parse(response.body)
end

#refreshObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/weekdone/api.rb', line 51

def refresh
  @logger.debug("tokencode=#{@token_code} clientid=#{@client.id}")

  if @token.expired?
    @token = @token.refresh!
    @logger.info("refreshed token due to expire (new token expires at #{Time.at(@token.expires_at)}).")
  elsif
    @logger.debug("token is still valid (token expires at #{Time.at(@token.expires_at)}).")
  end

  @token_code = @token.token
end

#searchForItems(user_id: nil, team_id: nil, period: nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/weekdone/api.rb', line 76

def searchForItems(user_id: nil, team_id: nil, period: nil)
  refresh

  params = { token: @token_code }
  params[:user_id] = user_id if not user_id.nil?
  params[:team_id] = team_id if not team_id.nil?
  params[:period] = period if not period.nil?

  response = Faraday.get(API_URL + '/1/items', params)
  JSON.parse(response.body)
end

#sortItemsObject

Raises:

  • (NotImplementedError)


121
122
123
# File 'lib/weekdone/api.rb', line 121

def sortItems
  raise NotImplementedError
end

#token_hashObject



64
65
66
# File 'lib/weekdone/api.rb', line 64

def token_hash
  @token.to_hash
end

#token_hash=(hash) ⇒ Object



68
69
70
71
72
73
# File 'lib/weekdone/api.rb', line 68

def token_hash=(hash)
  @logger.debug("update oauth token from hash=#{hash}")

  @token = OAuth2::AccessToken.from_hash(@client, hash)
  @token_code = @token.token
end

#updateItemObject

Raises:

  • (NotImplementedError)


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

def updateItem
  raise NotImplementedError
end

#updateObjectiveObject

Raises:

  • (NotImplementedError)


229
230
231
# File 'lib/weekdone/api.rb', line 229

def updateObjective
  raise NotImplementedError
end

#updateObjectiveCommentObject

Raises:

  • (NotImplementedError)


250
251
252
# File 'lib/weekdone/api.rb', line 250

def updateObjectiveComment
  raise NotImplementedError
end

#updateObjectiveResultObject

Raises:

  • (NotImplementedError)


262
263
264
# File 'lib/weekdone/api.rb', line 262

def updateObjectiveResult
  raise NotImplementedError
end

#updateTagPriorityObject

Raises:

  • (NotImplementedError)


201
202
203
# File 'lib/weekdone/api.rb', line 201

def updateTagPriority
  raise NotImplementedError
end

#updateTagStatusObject

Raises:

  • (NotImplementedError)


205
206
207
# File 'lib/weekdone/api.rb', line 205

def updateTagStatus
  raise NotImplementedError
end