Class: Teambition2::Client

Inherits:
Object
  • Object
show all
Includes:
API::Project, API::StageTemplate, API::Tag, API::Task, API::TaskGroup
Defined in:
lib/teambition2/client.rb

Overview

Teambiton Client

Constant Summary collapse

OAUTH2_ENDPOINT =
'https://account.teambition.com'.freeze
API_ENDPOINT =
'https://api.teambition.com'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API::Tag

#create_tag, #tag, #tag_search, #tags

Methods included from API::StageTemplate

#create_stage_template, #stage_template, #stage_templates

Methods included from API::Task

#create_task, #edit_task_tags, #project_tasks, #stage_tasks

Methods included from API::TaskGroup

#create_task_group, #task_group, #task_group_search

Methods included from API::Project

#project, #project_search, #project_tags, #project_task_groups, #projects

Constructor Details

#initialize(key, secret, callback_url) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
# File 'lib/teambition2/client.rb', line 17

def initialize(key, secret, callback_url)
  @key = key
  @secret = secret
  @callback_url = callback_url
end

Instance Attribute Details

#callback_urlObject

Returns the value of attribute callback_url.



15
16
17
# File 'lib/teambition2/client.rb', line 15

def callback_url
  @callback_url
end

#keyObject

Returns the value of attribute key.



15
16
17
# File 'lib/teambition2/client.rb', line 15

def key
  @key
end

#secretObject

Returns the value of attribute secret.



15
16
17
# File 'lib/teambition2/client.rb', line 15

def secret
  @secret
end

#tokenObject

Returns the value of attribute token.



15
16
17
# File 'lib/teambition2/client.rb', line 15

def token
  @token
end

Instance Method Details

#access_token(code) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/teambition2/client.rb', line 36

def access_token(code)
  r = HTTP.post URI.join(OAUTH2_ENDPOINT, '/oauth2/access_token'), form: {
    client_id: @key,
    client_secret: @secret,
    code: code
  }

  @token = r.parse(:json)['access_token']
end

#authorize_url(state = nil, response_type = 'code') ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/teambition2/client.rb', line 23

def authorize_url(state = nil, response_type = 'code')
  uri = URI.join(OAUTH2_ENDPOINT, '/oauth2/authorize'.freeze)
  params = {
    client_id: @key,
    redirect_uri: @callback_url,
    response_type: response_type,
    state: state
  }

  uri.query = URI.encode_www_form(params)
  uri.to_s
end

#delete(path, form = {}) ⇒ Object



80
81
82
83
84
# File 'lib/teambition2/client.rb', line 80

def delete(path, form = {})
  uri = URI.join(API_ENDPOINT, path)
  r = request.delete uri, form: form
  r.parse(:json)
end

#get(path, params = {}) ⇒ Object



62
63
64
65
66
# File 'lib/teambition2/client.rb', line 62

def get(path, params = {})
  uri = URI.join(API_ENDPOINT, path)
  r = request.get uri, params: params
  r.parse(:json)
end

#post(path, form = {}) ⇒ Object



68
69
70
71
72
# File 'lib/teambition2/client.rb', line 68

def post(path, form = {})
  uri = URI.join(API_ENDPOINT, path)
  r = request.post uri, form: form
  r.parse(:json)
end

#put(path, form = {}) ⇒ Object



74
75
76
77
78
# File 'lib/teambition2/client.rb', line 74

def put(path, form = {})
  uri = URI.join(API_ENDPOINT, path)
  r = request.put uri, form: form
  r.parse(:json)
end

#valid_token?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/teambition2/client.rb', line 46

def valid_token?
  uri = URI.join(OAUTH2_ENDPOINT, "/api/applications/#{@client_key}/tokens/check")
  r = request.get uri, params: params

  case r.code
  when 200
    true
  when 400
    raise Teambition2::ParamError
  when 403
    raise Teambition2::NoPermissionError
  end

  false
end