Class: CrowiClient

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

Overview

Crowi のクライアントクラス

Instance Method Summary collapse

Constructor Details

#initialize(crowi_url: '', access_token: '') ⇒ CrowiClient

コンストラクタ

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
# File 'lib/crowi/client/client.rb', line 13

def initialize(crowi_url: '', access_token: '')
  raise ArgumentError, 'Config `crowi_url` is required.'    if crowi_url.empty?
  raise ArgumentError, 'Config `access_token` is required.' if access_token.empty?

  @crowi_url = crowi_url
  @access_toke = access_token
  @cp_entry_point = URI.join(crowi_url, '/_api/').to_s
end

Instance Method Details

#attachment(path_exp: nil, attachment_name: nil) ⇒ String

指定した添付ファイル情報を取得する

Parameters:

  • path_exp (String) (defaults to: nil)

    ページパス(正規表現)

Returns:

  • (String)

    attachment’s file name



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

def attachment(path_exp: nil, attachment_name: nil)
    ret = request(CPApiRequestAttachmentsList.new page_id: page_id(path_exp: path_exp))
    return ret&.data&.find { |a| a.originalName == attachment_name }
end

#attachment_exist?(path_exp: nil, attachment_name: nil) ⇒ true/false

ページに添付ファイルが存在するか調べる

Parameters:

  • path_exp (String) (defaults to: nil)

    ページパス(正規表現)

  • attachment_name (String) (defaults to: nil)

    添付ファイル名

Returns:

  • (true/false)

    添付ファイルの存在



51
52
53
54
# File 'lib/crowi/client/client.rb', line 51

def attachment_exist?(path_exp: nil, attachment_name: nil)
  ret = request(CPApiRequestAttachmentsList.new page_id: page_id(path_exp: path_exp))
  return ret&.ok && ret&.data&.find { |a| a.originalName == attachment_name } != nil
end

#attachment_id(path_exp: nil, attachment_name: nil) ⇒ String

指定した添付ファイルのIDを取得する

Parameters:

  • path_exp (String) (defaults to: nil)

    ページパス(正規表現)

Returns:

  • (String)

    attachment’s file name



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

def attachment_id(path_exp: nil, attachment_name: nil)
    ret = request(CPApiRequestAttachmentsList.new page_id: page_id(path_exp: path_exp))
    return ret&.data&.find { |a| a.originalName == attachment_name }&._id
end

#page_exist?(path_exp: nil) ⇒ true/false

ページが存在するか調べる

Parameters:

  • path (String)

    ページパス

Returns:

  • (true/false)

    ページの存在



42
43
44
45
# File 'lib/crowi/client/client.rb', line 42

def page_exist?(path_exp: nil)
  ret = request(CPApiRequestPagesList.new path_exp: path_exp)
  return ret&.ok && ret&.data&.find { |p| p.path.match(path_exp) } != nil
end

#page_id(path_exp: nil) ⇒ String

ページIDを取得する

Parameters:

  • path_exp (String) (defaults to: nil)

    ページパス

Returns:

  • (String)

    ページID



33
34
35
36
37
# File 'lib/crowi/client/client.rb', line 33

def page_id(path_exp: nil)
  ret = request(CPApiRequestPagesList.new path_exp: path_exp)
  return nil if (ret.kind_of? CPInvalidRequest || ret.data.nil?)
  return ret.data.find { |page| URI.unescape(page.path) == path_exp }&.id
end

#request(req) ⇒ String

APIリクエストを送信する

Parameters:

  • req (ApiRequestBase)

    APIリクエスト

Returns:

  • (String)

    APIリクエストの応答(JSON形式)



25
26
27
28
# File 'lib/crowi/client/client.rb', line 25

def request(req)
  req.param[:access_token] = @access_toke
  return req.execute URI.join(@cp_entry_point, req.entry_point).to_s
end