module Teambition

Top-level namespace

Constants

API_DOMAIN

API domain

AUTH_DOMAIN

Authorization domain

VERSION

Current version number

Attributes

callback_url[RW]

@return [String] callback url

client_key[RW]

@return [String] client key provided by Teambition

client_secret[RW]

@return [String] client secret provided by Teambition

Public Class Methods

authorize_url(state: nil, lang: :zh) click to toggle source

Get the url for authorization @param state [String] @param lang [String/Symbol] @return [String]

# File lib/teambition.rb, line 24
def authorize_url(state: nil, lang: :zh)
  uri = URI.join(AUTH_DOMAIN, '/oauth2/authorize')

  params = {
    client_id: client_key,
    redirect_uri: callback_url,
    lang: %w[zh en].include?(lang.to_s) ? lang : :zh
  }
  params[:state] = state if state
  uri.query = URI.encode_www_form(params)

  uri.to_s
end
get_access_token(code) click to toggle source

Fetch the access token by a code @param code [String] @return [String]

# File lib/teambition.rb, line 41
def get_access_token(code)
  res = Net::HTTP.post_form(
    URI.join(AUTH_DOMAIN, '/oauth2/access_token'),
    client_id: client_key,
    client_secret: client_secret,
    code: code
  )
  JSON.parse(res.body)['access_token']
end
valid_access_token?(token) click to toggle source

Validate the token @param token [String]

# File lib/teambition.rb, line 53
def valid_access_token?(token)
  uri = URI.join(API_DOMAIN, "/api/applications/#{client_key}/tokens/check")

  req = Net::HTTP::Get.new(uri)
  req['Authorization'] = "OAuth2 #{token}"

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |https|
    https.request(req)
  end
  res.code == '200'
end