class Workxp::Client

Constants

WORKXP_SITE

Attributes

access_token[RW]

OAuth2::AccessToken

Store authorized infos

sub_domain[RW]

WorkXP product's subdomain. subdomain is your product's identify. Your can set different subdomain for fetch different product's data. @example ruby.workxp.info 'ruby' is subdomain

Public Class Methods

new(opts={}) click to toggle source

@param [Hash] opts the options to create WorkXP Client. @option opts [String] :app_key <required> @option opts [String] :app_secret <required> @option opts [String] :token oauth2 access token. <required> @option opts [String] :refresh_token <optional> @option opts [DateTime] :expires_at <optional> @option opts [String] :workxp_site <optional> @option opts [String] :sub_domain <optional> @option opts [String] :user_agent <optional> App name of client

# File lib/workxp/client.rb, line 32
def initialize(opts={})
  @app_key = opts.delete :app_key
  @app_secret = opts.delete :app_secret
  @workxp_site = opts.delete(:workxp_site) || WORKXP_SITE
  @user_agent = opts.delete(:user_agent)
  self.sub_domain = opts.delete :sub_domain
  
  self.access_token = OAuth2::AccessToken.new(self.oauth_client, opts[:token], opts)
end

Public Instance Methods

accounts() click to toggle source
# File lib/workxp/client.rb, line 68
def accounts
  valid_token.get("/api/accounts.json", )
end
activities(opts={}) click to toggle source
# File lib/workxp/client.rb, line 72
def activities(opts={})
  valid_token.get('/api/activities.json', params: opts, headers: domain_hash).parsed
end
activity(id) click to toggle source
# File lib/workxp/client.rb, line 80
def activity(id)
  valid_token.get("/api/activities/#{id}.json", headers: domain_hash).parsed
end
create_attachment(file_path, file_type) click to toggle source

@param [String] file_path 'path/avatar.png' @param [String] file_type 'image/png' @return [Hash] {“token”=>“1578a2cd0682359935ae03826fb892701f7c5120”}

# File lib/workxp/client.rb, line 87
def create_attachment(file_path, file_type)
  payload = {file: Faraday::UploadIO.new(file_path, file_type)}
  valid_token.post("/api/attachments.json", body: payload, headers: {:'Sub-Domain' => sub_domain}).parsed
end
deletions(opts={}) click to toggle source
# File lib/workxp/client.rb, line 100
def deletions(opts={})
  valid_token.get('/api/deletions.json', params: opts, headers: domain_hash).parsed
end
groups() click to toggle source
# File lib/workxp/client.rb, line 96
def groups
  valid_token.get('/api/groups.json', headers: domain_hash).parsed
end
oauth_client() click to toggle source

OAuth2::Client instance with WorkxP OAuth

# File lib/workxp/client.rb, line 43
def oauth_client
  @oauth_client ||= OAuth2::Client.new @app_key, @app_secret, site: @workxp_site do |stack|
                      stack.request :multipart
                      stack.request :url_encoded
                      stack.adapter :net_http
                    end
end
search_activities(opts={}) click to toggle source
# File lib/workxp/client.rb, line 76
def search_activities(opts={})
  valid_token.get('/api/activities/search.json', params: opts, headers: domain_hash).parsed
end
tags() click to toggle source
# File lib/workxp/client.rb, line 92
def tags
  valid_token.get('/api/tags.json', headers: domain_hash).parsed
end
task_categories() click to toggle source
# File lib/workxp/client.rb, line 113
def task_categories
  categories.select {|obj| obj['type'] == 'TaskCategory'}
end
user(id) click to toggle source
# File lib/workxp/client.rb, line 64
def user(id)
  valid_token.get("/api/users/#{id}.json", headers: domain_hash).parsed
end
users(opts={}) click to toggle source

@param [Hash] opts the request parameters @option opts [String] :group_id Filter by group_id

# File lib/workxp/client.rb, line 60
def users(opts={})
  valid_token.get('/api/users.json', params: opts, headers: domain_hash).parsed
end
valid_token() click to toggle source
# File lib/workxp/client.rb, line 51
def valid_token
  if access_token.expired?
    self.access_token = access_token.refresh!
  end
  self.access_token
end

Private Instance Methods

domain_hash() click to toggle source
# File lib/workxp/client.rb, line 118
def domain_hash
  raise(ArgumentError, "WorkXP Subdomain required.") if sub_domain.nil?
  {:'Sub-Domain' => sub_domain, :'Content-Type' => 'application/json', :'User-Agent' => (@user_agent || 'WorkXP Client')}
end