Class: WavixApi::Client
Constant Summary
Constants included
from HTTPMethods
HTTPMethods::CONTENT_TYPES, HTTPMethods::DEFAULT_HEADERS, HTTPMethods::FILE_NAMES
Instance Method Summary
collapse
-
#delete(path, params: {}) ⇒ Object
-
#download(path, headers: {}, params: {}, save_path: nil) ⇒ Object
-
#get(path, params: {}, headers: {}) ⇒ Object
-
#initialize(host:, api_key:) ⇒ Client
constructor
A new instance of Client.
-
#patch(path, body: {}, params: {}) ⇒ Object
-
#post(path, body: {}, params: {}, with_file: false, headers: { 'Content-Type' => 'application/json' }) ⇒ Object
-
#put(path, body: {}, params: {}) ⇒ Object
#filename, #request
Constructor Details
#initialize(host:, api_key:) ⇒ Client
Returns a new instance of Client.
99
100
101
102
|
# File 'lib/wavix_api.rb', line 99
def initialize(host:, api_key:)
@host = host.empty? ? DEFAULT_HOST : host
@api_key = api_key
end
|
Instance Method Details
#delete(path, params: {}) ⇒ Object
112
113
114
|
# File 'lib/wavix_api.rb', line 112
def delete(path, params: {})
request('DELETE', path, params: params)
end
|
#download(path, headers: {}, params: {}, save_path: nil) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/wavix_api.rb', line 130
def download(path, headers: {}, params: {}, save_path: nil)
if save_path
dirname = File.dirname(save_path)
dir_parts = dirname.split(%r{[/\\]}).reject(&:empty?)
(1..dir_parts.size).each do |n|
dir = dir_parts[0...n].join('/')
Dir.mkdir("/#{dir}") unless Dir.exist?("/#{dir}")
end
end
response = request('GET', path, params: params, headers: )
return response if save_path.nil? || !response.success? || response.filename.nil?
filename = if CONTENT_TYPES.keys.find { |el| save_path.include?(".#{el}") }
save_path
else
"#{save_path}/#{response.filename}"
end
File.open(filename, 'wb') { |f| f.write response.body }
response
end
|
#get(path, params: {}, headers: {}) ⇒ Object
104
105
106
|
# File 'lib/wavix_api.rb', line 104
def get(path, params: {}, headers: {})
request('GET', path, params: params, headers: HTTPMethods::DEFAULT_HEADERS.merge())
end
|
#patch(path, body: {}, params: {}) ⇒ Object
108
109
110
|
# File 'lib/wavix_api.rb', line 108
def patch(path, body: {}, params: {})
request('PATCH', path, params: params, body: body.to_json)
end
|
#post(path, body: {}, params: {}, with_file: false, headers: { 'Content-Type' => 'application/json' }) ⇒ Object
120
121
122
123
124
125
126
127
128
|
# File 'lib/wavix_api.rb', line 120
def post(path, body: {}, params: {}, with_file: false,
headers: { 'Content-Type' => 'application/json' })
body = body.to_json unless with_file
['Content-Type'] = 'multipart/form-data' if with_file
request('POST', path, params: params, body: body, headers: ) do |builder|
builder.request :multipart if with_file
end
end
|
#put(path, body: {}, params: {}) ⇒ Object
116
117
118
|
# File 'lib/wavix_api.rb', line 116
def put(path, body: {}, params: {})
request('PUT', path, params: params, body: body.to_json)
end
|