Class: CrowiClient

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

Overview

Crowi のクライアントクラス

Instance Method Summary collapse

Constructor Details

#initializeCrowiClient

コンストラクタ(シングルトン)

Raises:

  • (ArgumentError)


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

def initialize
  raise ArgumentError, 'Config url is required.'   unless EasySettings['url']
  raise ArgumentError, 'Config token is required.' unless EasySettings['token']
  @cp_entry_point = URI.join(EasySettings['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



65
66
67
68
# File 'lib/crowi/client/client.rb', line 65

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)

    添付ファイルの存在



49
50
51
52
# File 'lib/crowi/client/client.rb', line 49

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



57
58
59
60
# File 'lib/crowi/client/client.rb', line 57

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)

    ページの存在



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

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



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

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

#request(req) ⇒ String

APIリクエストを送信する

Parameters:

  • req (ApiRequestBase)

    APIリクエスト

Returns:

  • (String)

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



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

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