Sha256: 5723462c7bf4d3a4272973304ee93403c931a2f3e8c7607a7266d6c87d18cd48

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require 'faraday'
require 'faraday_middleware'
require 'base64'
require 'json'
require 'kintone/command/record'

class Kintone::Api
  BASE_PATH = "/k%s/v1/"
  SPACE_PATH = "/guest/%s"

  def initialize(domain, user, password)
    @token = Base64.encode64("#{user}:#{password}")
    @base_path = BASE_PATH % nil
    @connection =
      Faraday.new(:url => "https://#{domain}", :ssl => false) do |builder|
        builder.adapter :net_http
        builder.request :url_encoded
        builder.response :json
        builder.response :logger
      end
  end

  def guest(space)
    space_path = SPACE_PATH % space if space.to_s.match(/^[1-9][0-9]*$/)
    @base_path = BASE_PATH % space_path
    return self
  end

  def get(path, params=nil)
    url = @base_path + path
    response =
      @connection.get do |request|
        request.url url
        request.params = params
        request.headers = {"X-Cybozu-Authorization" => @token}
      end
    return response.body
  end

  def post(path, body)
    url = @base_path + path
    response =
      @connection.post do |request|
        request.url url
        request.headers = {"X-Cybozu-Authorization" => @token, "Content-Type" => "application/json"}
        request.body = body.to_json
      end
    return response.body
  end

  def put(path, body)
    url = @base_path + path
    response =
      @connection.put do |request|
        request.url url
        request.headers = {"X-Cybozu-Authorization" => @token, "Content-Type" => "application/json"}
        request.body = body.to_json
      end
    return response.body
  end

  def delete(path, params=nil)
    url = @base_path + path
    response =
      @connection.delete do |request|
        request.url url
        request.params = params
        request.headers = {"X-Cybozu-Authorization" => @token}
      end
    return response.body
  end

  def record
    Kintone::Command::Record.new(self)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kintone-0.0.1 lib/kintone/api.rb