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/'
- COMMAND =
'%s.json'
- 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
-
#delete(url, body = nil) ⇒ Object
-
#get(url, params = {}) ⇒ Object
-
#get_url(command) ⇒ Object
-
#guest(space_id) ⇒ Object
-
#initialize(domain, user, password) ⇒ Api
constructor
-
#method_missing(name, *args) ⇒ Object
-
#post(url, body) ⇒ Object
-
#post_file(url, path, content_type, original_filename) ⇒ Object
-
#put(url, body) ⇒ Object
Constructor Details
#initialize(domain, user, password) ⇒ Api
Returns a new instance of Api.
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/kintone/api.rb', line 33
def initialize(domain, user, password)
token = Base64.encode64("#{user}:#{password}")
url = "https://#{domain}"
= { 'X-Cybozu-Authorization' => token }
@connection =
Faraday.new(url: url, headers: ) 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
105
106
107
108
109
110
111
|
# File 'lib/kintone/api.rb', line 105
def method_missing(name, *args)
if ACCESSIBLE_COMMAND.include?(name)
CommandAccessor.send(name, self)
else
super(name, *args)
end
end
|
Instance Method Details
#delete(url, body = nil) ⇒ Object
83
84
85
86
87
88
89
90
91
|
# File 'lib/kintone/api.rb', line 83
def delete(url, body = nil)
response =
@connection.delete do |request|
request.url url
request.['Content-Type'] = 'application/json'
request.body = body.to_json
end
response.body
end
|
#get(url, params = {}) ⇒ Object
54
55
56
57
58
59
60
61
|
# File 'lib/kintone/api.rb', line 54
def get(url, params = {})
response =
@connection.get do |request|
request.url url
request.params = params
end
response.body
end
|
#get_url(command) ⇒ Object
46
47
48
|
# File 'lib/kintone/api.rb', line 46
def get_url(command)
BASE_PATH + (COMMAND % command)
end
|
#guest(space_id) ⇒ Object
50
51
52
|
# File 'lib/kintone/api.rb', line 50
def guest(space_id)
Kintone::Api::Guest.new(space_id, self)
end
|
#post(url, body) ⇒ Object
63
64
65
66
67
68
69
70
71
|
# File 'lib/kintone/api.rb', line 63
def post(url, body)
response =
@connection.post do |request|
request.url url
request.['Content-Type'] = 'application/json'
request.body = body.to_json
end
response.body
end
|
#post_file(url, path, content_type, original_filename) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/kintone/api.rb', line 93
def post_file(url, path, content_type, original_filename)
response =
@connection.post do |request|
request.url url
request.['Content-Type'] = 'multipart/form-data'
request.body = { file: Faraday::UploadIO.new(
path, content_type, original_filename,
'Content-Disposition' => 'form-data') }
end
response.body['fileKey']
end
|
#put(url, body) ⇒ Object
73
74
75
76
77
78
79
80
81
|
# File 'lib/kintone/api.rb', line 73
def put(url, body)
response =
@connection.put do |request|
request.url url
request.['Content-Type'] = 'application/json'
request.body = body.to_json
end
response.body
end
|