Class: Kintone::Api

Inherits:
Object show all
Defined in:
lib/kintone/api.rb,
lib/kintone/api/guest.rb

Direct Known Subclasses

OAuthApi

Defined Under Namespace

Classes: CommandAccessor, Guest

Constant Summary collapse

BASE_PATH =
'/k/v1/'.freeze
COMMAND =
'%s.json'.freeze
ACCESSIBLE_COMMAND =
[
  :record, :records, :form, :app_acl, :record_acl,
  :field_acl, :template_space, :space, :space_body, :space_thread,
  :space_members, :guests, :app, :apps, :apis,
  :bulk_request, :bulk, :file, :preview_form
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(domain, user, password = nil) {|@connection| ... } ⇒ Api

Returns a new instance of Api.

Yields:

  • (@connection)


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kintone/api.rb', line 20

def initialize(domain, user, password = nil)
  @connection =
    Faraday.new(url: "https://#{domain}", headers: build_headers(user, password)) do |builder|
      builder.request :url_encoded
      builder.request :multipart
      builder.response :json, content_type: /\bjson$/
      builder.adapter :net_http
    end

  yield(@connection) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/kintone/api.rb', line 96

def method_missing(name, *args)
  if ACCESSIBLE_COMMAND.include?(name)
    CommandAccessor.send(name, self)
  else
    super
  end
end

Instance Method Details

#delete(url, body = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/kintone/api.rb', line 74

def delete(url, body = nil)
  response =
    @connection.delete do |request|
      request.url url
      request.headers['Content-Type'] = 'application/json'
      request.body = body.to_json
    end
  raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
  response.body
end

#get(url, params = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kintone/api.rb', line 40

def get(url, params = {})
  response =
    @connection.get do |request|
      request.url url
      # NOTE: Request URI Too Large 対策
      request.headers['Content-Type'] = 'application/json'
      request.body = params.to_h.to_json
    end
  raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
  response.body
end

#get_url(command) ⇒ Object



32
33
34
# File 'lib/kintone/api.rb', line 32

def get_url(command)
  BASE_PATH + (COMMAND % command)
end

#guest(space_id) ⇒ Object



36
37
38
# File 'lib/kintone/api.rb', line 36

def guest(space_id)
  Kintone::Api::Guest.new(space_id, self)
end

#post(url, body) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/kintone/api.rb', line 52

def post(url, body)
  response =
    @connection.post do |request|
      request.url url
      request.headers['Content-Type'] = 'application/json'
      request.body = body.to_json
    end
  raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
  response.body
end

#post_file(url, path, content_type, original_filename) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/kintone/api.rb', line 85

def post_file(url, path, content_type, original_filename)
  response =
    @connection.post do |request|
      request.url url
      request.headers['Content-Type'] = 'multipart/form-data'
      request.body = { file: Faraday::UploadIO.new(path, content_type, original_filename) }
    end
  raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
  response.body['fileKey']
end

#put(url, body) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/kintone/api.rb', line 63

def put(url, body)
  response =
    @connection.put do |request|
      request.url url
      request.headers['Content-Type'] = 'application/json'
      request.body = body.to_json
    end
  raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
  response.body
end

#respond_to_missing?(name, *args) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/kintone/api.rb', line 104

def respond_to_missing?(name, *args)
  ACCESSIBLE_COMMAND.include?(name) || super
end