class Kintone::Client::Path

Constants

BASE_PATH

Public Class Methods

new(conn, auth, path) click to toggle source
# File lib/kintone/client/client.rb, line 50
def initialize(conn, auth, path)
  @conn = conn
  @auth = auth
  @path = path
end

Private Instance Methods

authorize(req) click to toggle source
# File lib/kintone/client/client.rb, line 117
def authorize(req)
  if @auth[:api_token]
    req.headers['X-Cybozu-API-Token'] = Base64.strict_encode64(@auth[:api_token])
  elsif @auth[:login_name] and @auth[:password]
    req.headers['X-Cybozu-Authorization'] = Base64.strict_encode64(
      @auth[:login_name] + ':' + @auth[:password])
  else
    raise 'no auth parameter (:api_token or :login_name,:password)'
  end
end
can_be_expanded?(value) click to toggle source
# File lib/kintone/client/client.rb, line 112
def can_be_expanded?(value)
  value.kind_of?(Array) &&
  value.all? {|v| not v.kind_of?(Array) and not v.kind_of?(Hash) }
end
expand_params_array(params) click to toggle source
# File lib/kintone/client/client.rb, line 83
def expand_params_array(params)
  params.keys.each do |key|
    value = params[key]

    if can_be_expanded?(value)
      params.delete(key)

      value.each_with_index do |v, i|
        params["#{key}[#{i}]"] = v
      end
    else
      params[key] = expand_params_array0(value)
    end
  end

  params
end
expand_params_array0(value) click to toggle source
# File lib/kintone/client/client.rb, line 101
def expand_params_array0(value)
  case value
  when Array
    value.map {|v| expand_params_array0(v) }
  when Hash
    expand_params_array(value)
  else
    value
  end
end
method_missing(method_name, *args, &block) click to toggle source
# File lib/kintone/client/client.rb, line 128
def method_missing(method_name, *args, &block)
  method_name = method_name.to_s

  if %w(get post put delete post_json put_json delete_json).include?(method_name)
    case args.length
    when 0
      args = nil
    when 1
      args = args.first
    end

    options = {}

    if method_name =~ /_json\z/
      method_name.sub!(/_json\z/, '')
      options[:json] = true
    end

    request(method_name, args, options, &block)
  else
    unless args.length.zero?
      raise ArgumentError, "wrong number of arguments (#{args.length} for 0)"
    end

    self.class.new(@conn, @auth, @path + '/' + method_name.to_s)
  end
end
request(method_name, params, options = {}) { |req| ... } click to toggle source
# File lib/kintone/client/client.rb, line 58
def request(method_name, params, options = {})
  response = @conn.send(method_name) do |req|
    req.url BASE_PATH + '/' + @path + '.json'

    if options[:json]
      req.body = JSON.dump(params)
      req.headers['Content-Type'] = 'application/json'
    else
      req.params = expand_params_array(params || {})
    end

    authorize(req)

    yield(req) if block_given?
  end

  body = response.body

  if response.status != 200
    raise body.kind_of?(Hash) ? Kintone::Error.new(body, @path, method_name, params) : body.inspect
  end

  body
end