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: '', rest_client_param: {}) ⇒ CrowiClient

コンストラクタ

Raises:

  • (ArgumentError)


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

def initialize(crowi_url: '', access_token: '', rest_client_param: {})
  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_token = access_token
  @rest_client_param = rest_client_param
  @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



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

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)

    添付ファイルの存在



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

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



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

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)

    ページの存在



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

def page_exist?(path_exp: nil)
  return !page_id(path_exp: path_exp).nil?
end

#page_id(path_exp: nil) ⇒ String

ページIDを取得する

Parameters:

  • path_exp (String) (defaults to: nil)

    ページパス

Returns:

  • (String)

    ページID



35
36
37
38
39
# File 'lib/crowi/client/client.rb', line 35

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形式)



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

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