class CPApiRequestBase

APIリクエストの基本クラス

Constants

METHOD_GET
METHOD_POST

Attributes

entry_point[R]
method[R]
param[R]

Public Class Methods

new(entry_point, method, param = {}) click to toggle source

コンストラクタ @param [String] entry_point APIリクエストのエントリポイントとなるURLパス(ex. '/page.list') @param [Enumerator] method APIリクエストのタイプ @param [Hash] param APIリクエストのパラメータ

# File lib/crowi/client/apireq/api_request_base.rb, line 45
def initialize(entry_point, method, param = {})
  @entry_point = entry_point
  @method = method
  @param = param.reject { |k, v| !v }
end

Public Instance Methods

execute(entry_point, rest_client_param: {}) click to toggle source

リクエストを実行する @param [String] entry_point APIのエントリーポイントとなるURL(ex. localhost:3000/_api/pages.list) @param [Hash] rest_client_param RestClientのパラメータ @return [String] リクエスト実行結果(CPApiReturnオブジェクト)

# File lib/crowi/client/apireq/api_request_base.rb, line 79
def execute(entry_point, rest_client_param: {})

  if invalid?
    return validation_msg
  end

  case @method
  when 'GET'
    params = { method: :get, url: entry_point, headers: { params: @param } }.merge(rest_client_param)
  when 'POST'
    params = { method: :post, url: entry_point, content_type: :json, accept: :json,
               headers: { params: @param.to_json } }.merge()
  end
  ret_json = RestClient::Request.execute params
  ret = JSON.parse(ret_json)
  return CPApiReturn.new(ok: ret['ok'], data: ret.reject { |k,v| k == 'ok' })
end
invalid?() click to toggle source

パラメータのバリデーションチェックを行う @return [true/false] バリデーションチェック結果

# File lib/crowi/client/apireq/api_request_base.rb, line 65
def invalid?
  return (_invalid)
end
param=(param = {}) click to toggle source

パラメータを代入 @param [Hash] param APIリクエストのパラメータ

# File lib/crowi/client/apireq/api_request_base.rb, line 53
def param=(param = {})
  @param = param
end
valid?() click to toggle source

パラメータのバリデーションチェックを行う @return [true/false] バリデーションチェック結果

# File lib/crowi/client/apireq/api_request_base.rb, line 59
def valid?
  return (!_invalid)
end
validation_msg() click to toggle source

バリデーションエラーの説明 @return [String] バリデーションエラーの説明

# File lib/crowi/client/apireq/api_request_base.rb, line 71
def validation_msg
  return _invalid&.to_s
end

Protected Instance Methods

_invalid() click to toggle source

バリデーションエラーを取得する @return [nil/CPInvalidRequest] バリデーションエラー結果

# File lib/crowi/client/apireq/api_request_base.rb, line 101
def _invalid
  return CPInvalidRequest "Invalid API request.";
end