Class: DocBase::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/docbase/client.rb

Defined Under Namespace

Classes: NotSetPostIdError, NotSetTeamError, TooManyRequestError

Constant Summary collapse

DEFAULT_URL =
'https://api.docbase.io'
USER_AGENT =
"DocBase Ruby Gem #{DocBase::VERSION}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token: nil, url: nil, team: nil, retry_on_rate_limit_exceeded: false) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
# File 'lib/docbase/client.rb', line 12

def initialize(access_token: nil, url: nil, team: nil, retry_on_rate_limit_exceeded: false)
  self.access_token = access_token
  self.team = team
  @url = url || DEFAULT_URL
  self.retry_on_rate_limit_exceeded = retry_on_rate_limit_exceeded
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



10
11
12
# File 'lib/docbase/client.rb', line 10

def access_token
  @access_token
end

#retry_on_rate_limit_exceededObject

Returns the value of attribute retry_on_rate_limit_exceeded.



10
11
12
# File 'lib/docbase/client.rb', line 10

def retry_on_rate_limit_exceeded
  @retry_on_rate_limit_exceeded
end

#teamObject

Returns the value of attribute team.



10
11
12
# File 'lib/docbase/client.rb', line 10

def team
  @team
end

Instance Method Details

#add_users_to_group(params) ⇒ Object

Raises:



44
45
46
47
48
49
50
# File 'lib/docbase/client.rb', line 44

def add_users_to_group(params)
  group_id = params[:group_id].to_i
  raise NotSetTeamError if group_id <= 0

  users_params = except(params, :group_id)
  request(method: :post, path: "/teams/#{team!}/groups/#{group_id}/users", params: users_params)
end

#archive_post(id) ⇒ Object



84
85
86
# File 'lib/docbase/client.rb', line 84

def archive_post(id)
  request(method: :put, path: "/teams/#{team!}/posts/#{id}/archive")
end

#attachment(id) ⇒ Object



118
119
120
# File 'lib/docbase/client.rb', line 118

def attachment(id)
  request(method: :get, path: "/teams/#{team!}/attachments/#{id}", for_binary: true)
end

#create_comment(params) ⇒ Object

Raises:



92
93
94
95
96
97
98
# File 'lib/docbase/client.rb', line 92

def create_comment(params)
  post_id = params[:post_id].to_i
  raise NotSetTeamError if post_id <= 0

  comment_params = except(params, :post_id)
  request(method: :post, path: "/teams/#{team!}/posts/#{post_id}/comments", params: params)
end

#create_group(params) ⇒ Object



40
41
42
# File 'lib/docbase/client.rb', line 40

def create_group(params)
  request(method: :post, path: "/teams/#{team!}/groups", params: params)
end

#create_post(params) ⇒ Object



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

def create_post(params)
  request(method: :post, path: "/teams/#{team!}/posts", params: params)
end

#delete_comment(id) ⇒ Object



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

def delete_comment(id)
  request(method: :delete, path: "/teams/#{team!}/comments/#{id}")
end

#delete_post(id) ⇒ Object



80
81
82
# File 'lib/docbase/client.rb', line 80

def delete_post(id)
  request(method: :delete, path: "/teams/#{team!}/posts/#{id}")
end

#group(id) ⇒ Object



36
37
38
# File 'lib/docbase/client.rb', line 36

def group(id)
  request(method: :get, path: "/teams/#{team!}/groups/#{id}")
end

#groups(name: nil, page: 1, per_page: 100) ⇒ Object



32
33
34
# File 'lib/docbase/client.rb', line 32

def groups(name: nil, page: 1, per_page: 100)
  request(method: :get, path: "/teams/#{team!}/groups", params: { name: name, page: page, per_page: per_page })
end

#post(id) ⇒ Object



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

def post(id)
  request(method: :get, path: "/teams/#{team!}/posts/#{id}")
end

#posts(q: '*', page: 1, per_page: 20) ⇒ Object



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

def posts(q: '*', page: 1, per_page: 20)
  request(method: :get, path: "/teams/#{team!}/posts", params: { q: q, page: page, per_page: per_page })
end

#remove_users_from_group(params) ⇒ Object

Raises:



52
53
54
55
56
57
58
# File 'lib/docbase/client.rb', line 52

def remove_users_from_group(params)
  group_id = params[:group_id].to_i
  raise NotSetTeamError if group_id <= 0

  users_params = except(params, :group_id)
  request(method: :delete, path: "/teams/#{team!}/groups/#{group_id}/users", params: users_params)
end

#tagsObject



28
29
30
# File 'lib/docbase/client.rb', line 28

def tags
  request(method: :get, path: "/teams/#{team!}/tags")
end

#team!Object

Raises:



19
20
21
22
# File 'lib/docbase/client.rb', line 19

def team!
  raise NotSetTeamError unless @team
  @team
end

#unarchive_post(id) ⇒ Object



88
89
90
# File 'lib/docbase/client.rb', line 88

def unarchive_post(id)
  request(method: :put, path: "/teams/#{team!}/posts/#{id}/unarchive")
end

#update_post(params) ⇒ Object

Raises:



72
73
74
75
76
77
78
# File 'lib/docbase/client.rb', line 72

def update_post(params)
  post_id = params[:id].to_i
  raise NotSetTeamError if post_id <= 0

  post_params = except(params, :id)
  request(method: :patch, path: "/teams/#{team!}/posts/#{post_id}", params: post_params)
end

#upload(paths) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/docbase/client.rb', line 104

def upload(paths)
  paths = [paths] unless paths.instance_of?(Array)

  params = paths.map do |path|
    file = File.new(path, 'r+b')
    {
      name: file.path.split('/').last,
      content: Base64.strict_encode64(file.read),
    }
  end

  request(method: :post, path: "/teams/#{team!}/attachments", params: params)
end

#users(q: nil, page: 1, per_page: 100, include_user_groups: false) ⇒ Object



24
25
26
# File 'lib/docbase/client.rb', line 24

def users(q: nil, page: 1, per_page: 100, include_user_groups: false)
  request(method: :get, path: "/teams/#{team!}/users", params: { q: q, page: page, per_page: per_page, include_user_groups: include_user_groups })
end