module HTTMultiParty

Constants

VERSION

Public Class Methods

delete(*args) click to toggle source
# File lib/httmultiparty.rb, line 75
def self.delete(*args)
  Basement.delete(*args)
end
file_to_upload_io(file, detect_mime_type = false) click to toggle source
# File lib/httmultiparty.rb, line 12
def self.file_to_upload_io(file, detect_mime_type = false)
  if file.respond_to? :original_filename
    filename = file.original_filename
  else
    filename =  File.split(file.path).last
  end

  UploadIO.new(file, content_type_of(file, detect_mime_type), filename)
end
flatten_params(params = {}, prefix = '') click to toggle source
# File lib/httmultiparty.rb, line 36
def self.flatten_params(params = {}, prefix = '')
  flattened = []
  params.each do |(k, v)|
    if params.is_a?(Array)
      v = k
      k = ''
    end

    flattened_key = prefix == '' ? "#{k}" : "#{prefix}[#{k}]"
    if v.is_a?(Hash) || v.is_a?(Array)
      flattened += flatten_params(v, flattened_key)
    else
      flattened << [flattened_key, v]
    end
  end
  flattened
end
get(*args) click to toggle source
# File lib/httmultiparty.rb, line 59
def self.get(*args)
  Basement.get(*args)
end
head(*args) click to toggle source
# File lib/httmultiparty.rb, line 79
def self.head(*args)
  Basement.head(*args)
end
included(base) click to toggle source
# File lib/httmultiparty.rb, line 7
def self.included(base)
  base.send :include, HTTParty
  base.extend ClassMethods
end
options(*args) click to toggle source
# File lib/httmultiparty.rb, line 83
def self.options(*args)
  Basement.options(*args)
end
patch(*args) click to toggle source
# File lib/httmultiparty.rb, line 71
def self.patch(*args)
  Basement.patch(*args)
end
post(*args) click to toggle source
# File lib/httmultiparty.rb, line 63
def self.post(*args)
  Basement.post(*args)
end
prepare_value!(value, detect_mime_type) click to toggle source
# File lib/httmultiparty.rb, line 54
def self.prepare_value!(value, detect_mime_type)
  return value if does_not_need_conversion?(value)
  HTTMultiParty.file_to_upload_io(value, detect_mime_type)
end
put(*args) click to toggle source
# File lib/httmultiparty.rb, line 67
def self.put(*args)
  Basement.put(*args)
end
query_string_normalizer(options = {}) click to toggle source
# File lib/httmultiparty.rb, line 22
def self.query_string_normalizer(options = {})
  detect_mime_type = options.fetch(:detect_mime_type, false)
  proc do |params|
    HTTMultiParty.flatten_params(params).map do |(k, v)|
      if file_present?(params)
        v = prepare_value!(v, detect_mime_type)
        [k, v]
      else
        "#{k}=#{v}"
      end
    end
  end
end

Private Class Methods

content_type_of(file, detect_mime_type) click to toggle source
# File lib/httmultiparty.rb, line 120
def self.content_type_of(file, detect_mime_type)
  if detect_mime_type
    type = MimeMagic.by_magic file
    file.rewind
    type
  else
    'application/octet-stream'
  end
end
does_not_need_conversion?(value) click to toggle source
# File lib/httmultiparty.rb, line 115
def self.does_not_need_conversion?(value)
  not_a_file?(value) ||
    upload_io?(value)
end
file?(value) click to toggle source
# File lib/httmultiparty.rb, line 103
def self.file?(value)
  value.respond_to?(:read)
end
file_present?(params) click to toggle source
# File lib/httmultiparty.rb, line 89
def self.file_present?(params)
  if params.is_a? Array
    file_present_in_array?(params)
  elsif params.is_a? Hash
    file_present_in_array?(params.values)
  else
    file?(params)
  end
end
file_present_in_array?(ary) click to toggle source
# File lib/httmultiparty.rb, line 99
def self.file_present_in_array?(ary)
  ary.any? { |a| file_present?(a) }
end
not_a_file?(value) click to toggle source
# File lib/httmultiparty.rb, line 107
def self.not_a_file?(value)
  !file?(value)
end
upload_io?(value) click to toggle source
# File lib/httmultiparty.rb, line 111
def self.upload_io?(value)
  value.is_a?(UploadIO)
end