Class: Kintone::Api

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

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
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(domain, user, password = nil) ⇒ Api

Returns a new instance of Api.



20
21
22
23
24
25
26
27
28
# 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
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/kintone/api.rb', line 92

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



70
71
72
73
74
75
76
77
78
79
# File 'lib/kintone/api.rb', line 70

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



38
39
40
41
42
43
44
45
46
# File 'lib/kintone/api.rb', line 38

def get(url, params = {})
  response =
    @connection.get do |request|
      request.url url
      request.params = params
    end
  raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
  response.body
end

#get_url(command) ⇒ Object



30
31
32
# File 'lib/kintone/api.rb', line 30

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

#guest(space_id) ⇒ Object



34
35
36
# File 'lib/kintone/api.rb', line 34

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

#post(url, body) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/kintone/api.rb', line 48

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



81
82
83
84
85
86
87
88
89
90
# File 'lib/kintone/api.rb', line 81

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



59
60
61
62
63
64
65
66
67
68
# File 'lib/kintone/api.rb', line 59

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)


100
101
102
# File 'lib/kintone/api.rb', line 100

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