class ABBYY::Cloud::Operations::Base

Public Class Methods

http_method(value = nil) click to toggle source
# File lib/abbyy/cloud/operations/base.rb, line 14
def http_method(value = nil)
  @http_method = value.to_s.capitalize if value
  @http_method || "Post"
end
path(&block) click to toggle source
# File lib/abbyy/cloud/operations/base.rb, line 19
def path(&block)
  block ? @path = block : @path
end
request_body(model = nil, &block) click to toggle source
# File lib/abbyy/cloud/operations/base.rb, line 31
def request_body(model = nil, &block)
  provide_model :@request_body, model, &block
end
request_query(model = nil, &block) click to toggle source
# File lib/abbyy/cloud/operations/base.rb, line 35
def request_query(model = nil, &block)
  provide_model :@request_query, model, &block
end
request_type(value = nil) click to toggle source
# File lib/abbyy/cloud/operations/base.rb, line 27
def request_type(value = nil)
  value ? (@request_type = value) : (@request_type || :json)
end
response_body(model = nil, &block) click to toggle source
# File lib/abbyy/cloud/operations/base.rb, line 43
def response_body(model = nil, &block)
  provide_model :@response_body, model, &block
end
response_type(value = nil) click to toggle source
# File lib/abbyy/cloud/operations/base.rb, line 39
def response_type(value = nil)
  value ? (@response_type = value) : (@response_type || :json)
end

Private Class Methods

provide_model(variable, model, &block) click to toggle source
# File lib/abbyy/cloud/operations/base.rb, line 49
def provide_model(variable, model, &block)
  value = instance_variable_get(variable)
  return value if value && model.nil? && block.nil?

  model ||= Class.new(Model)
                 .tap { |obj| obj.instance_eval(&block) if block }

  instance_variable_set(variable, model)
end

Public Instance Methods

call(file = nil, **data) click to toggle source
# File lib/abbyy/cloud/operations/base.rb, line 69
def call(file = nil, **data)
  mash      = Hashie::Mash.new(data)
  url       = mash.instance_eval(&path)
  multipart = prepare_multipart(file, **data) if file
  body      = prepare_request_body(multipart, data)
  query     = prepare_request_query(data)
  headers   = prepare_request_headers(multipart)
  res       = connection.call http_method, url, body: body,
                                                query: query,
                                                headers: headers
  handle_response_body res
end

Private Instance Methods

handle_response_body(data) click to toggle source
# File lib/abbyy/cloud/operations/base.rb, line 120
def handle_response_body(data)
  case response_type
  when :json then response_body[JSON.parse(data)]
  when :file then StringIO.new(data)
  else data
  end
rescue => error
  raise TypeError.new(link, data, error.message)
end
prepare_multipart(data, content_type: "text/plain", **) click to toggle source
# File lib/abbyy/cloud/operations/base.rb, line 84
def prepare_multipart(data, content_type: "text/plain", **)
  name = SecureRandom.hex(10)
  file = File.new(data, content_type: content_type)
  part = Part.new(name, file.read, file.path)
  part.content_type = content_type
  MultipartBody.new [part]
end
prepare_request_body(multipart, data) click to toggle source
# File lib/abbyy/cloud/operations/base.rb, line 92
def prepare_request_body(multipart, data)
  case request_type
  when :json then JSON(request_body[data].to_h)
  when :file then [nil, nil, multipart, nil].join("\r\n")
  else data
  end
rescue => error
  raise ArgumentError.new(link, data, error.message)
end
prepare_request_headers(multipart) click to toggle source
# File lib/abbyy/cloud/operations/base.rb, line 108
def prepare_request_headers(multipart)
  headers = {}
  headers["accept"] = "application/json" if response_type == :json
  headers["content-type"] = \
    case request_type
    when :json then "application/json"
    when :file then "multipart/form-data; boundary=#{multipart.boundary}"
    end

  headers
end
prepare_request_query(data) click to toggle source
# File lib/abbyy/cloud/operations/base.rb, line 102
def prepare_request_query(data)
  request_query[data]
rescue => error
  raise ArgumentError.new(link, data, error.message)
end