Class: Workxp::Client

Inherits:
Object
  • Object
show all
Includes:
Restfulable
Defined in:
lib/workxp/client.rb

Constant Summary collapse

WORKXP_SITE =
"https://workxp.info"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Restfulable

included

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create WorkXP Client.

Options Hash (opts):

  • :app_key (String)

    <required>

  • :app_secret (String)

    <required>

  • :token (String)

    oauth2 access token. <required>

  • :refresh_token (String)

    <optional>

  • :expires_at (DateTime)

    <optional>

  • :workxp_site (String)

    <optional>

  • :sub_domain (String)

    <optional>

  • :user_agent (String)

    <optional> App name of client



32
33
34
35
36
37
38
39
40
# 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

Instance Attribute Details

#access_tokenObject

OAuth2::AccessToken

Store authorized infos



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

def access_token
  @access_token
end

#sub_domainObject

WorkXP product’s subdomain. subdomain is your product’s identify. Your can set different subdomain for fetch different product’s data. ruby.workxp.info ‘ruby’ is subdomain



21
22
23
# File 'lib/workxp/client.rb', line 21

def sub_domain
  @sub_domain
end

Instance Method Details

#accountsObject



68
69
70
# File 'lib/workxp/client.rb', line 68

def accounts
  valid_token.get("/api/accounts.json", )
end

#activities(opts = {}) ⇒ Object



72
73
74
# 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) ⇒ Object



80
81
82
# 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) ⇒ Hash

Returns “token”=>“1578a2cd0682359935ae03826fb892701f7c5120”.

Parameters:

  • file_path (String)

    ‘path/avatar.png’

  • file_type (String)

    ‘image/png’

Returns:

  • (Hash)

    “token”=>“1578a2cd0682359935ae03826fb892701f7c5120”



87
88
89
90
# 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 = {}) ⇒ Object



100
101
102
# File 'lib/workxp/client.rb', line 100

def deletions(opts={})
  valid_token.get('/api/deletions.json', params: opts, headers: domain_hash).parsed
end

#groupsObject



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

def groups
  valid_token.get('/api/groups.json', headers: domain_hash).parsed
end

#oauth_clientObject

OAuth2::Client instance with WorkxP OAuth



43
44
45
46
47
48
49
# 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 = {}) ⇒ Object



76
77
78
# 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

#tagsObject



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

def tags
  valid_token.get('/api/tags.json', headers: domain_hash).parsed
end

#task_categoriesObject



113
114
115
# File 'lib/workxp/client.rb', line 113

def task_categories
  categories.select {|obj| obj['type'] == 'TaskCategory'}
end

#user(id) ⇒ Object



64
65
66
# File 'lib/workxp/client.rb', line 64

def user(id)
  valid_token.get("/api/users/#{id}.json", headers: domain_hash).parsed
end

#users(opts = {}) ⇒ Object

Parameters:

  • opts (Hash) (defaults to: {})

    the request parameters

Options Hash (opts):

  • :group_id (String)

    Filter by group_id



60
61
62
# File 'lib/workxp/client.rb', line 60

def users(opts={})
  valid_token.get('/api/users.json', params: opts, headers: domain_hash).parsed
end

#valid_tokenObject



51
52
53
54
55
56
# 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