Class: BeyondApi::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/beyond_api/request.rb

Class Method Summary collapse

Class Method Details

.token(url, params) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/beyond_api/request.rb', line 50

def self.token(url, params)
  response = BeyondApi::Connection.token.post do |request|
    request.url(url)
    request.params = params
  end

  [response.body.blank? ? nil : JSON.parse(response.body), response.status]
end

.upload(session, path, file_binary, content_type, params) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/beyond_api/request.rb', line 38

def self.upload(session, path, file_binary, content_type, params)
  response = BeyondApi::Connection.default.post do |request|
    request.url(session.api_url + path)
    request.headers["Authorization"] = "Bearer #{session.access_token}" unless session.access_token.nil?
    request.headers["Content-Type"] = content_type
    request.params = params.to_h.camelize_keys
    request.body = file_binary
  end

  [response.body.blank? ? nil : JSON.parse(response.body), response.status]
end

.upload_by_form(session, path, files, params) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/beyond_api/request.rb', line 59

def self.upload_by_form(session, path, files, params)
  response = BeyondApi::Connection.multipart.post do |request|
    request.url(session.api_url + path)
    request.headers["Authorization"] = "Bearer #{session.access_token}" unless session.access_token.nil?
    request.options[:params_encoder] = Faraday::FlatParamsEncoder
    request.params = params.to_h.camelize_keys
    files = files.split unless files.is_a? Array
    upload_files = files.map{ |file| Faraday::FilePart.new(File.open(file),
                                                           BeyondApi::Utils.file_content_type(file)) }
    request.body = { image: upload_files }
  end

  [response.body.blank? ? nil : JSON.parse(response.body), response.status]
end