class Spout::Helpers::SendJson

Generates JSON web requests for POST and PATCH.

Public Class Methods

new(url, args = {}) click to toggle source
# File lib/spout/helpers/send_json.rb, line 21
def initialize(url, args = {})
  @params = args
  @url = URI.parse(url)

  @http = Net::HTTP.new(@url.host, @url.port)
  if @url.scheme == "https"
    @http.use_ssl = true
    @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
rescue
  @error = "Invalid URL: #{url.inspect}"
  puts @error.red
end
patch(url, *args) click to toggle source
# File lib/spout/helpers/send_json.rb, line 16
def patch(url, *args)
  new(url, *args).patch
end
post(*args) click to toggle source
# File lib/spout/helpers/send_json.rb, line 12
def post(*args)
  new(*args).post
end

Public Instance Methods

patch() click to toggle source
# File lib/spout/helpers/send_json.rb, line 49
def patch
  @params["_method"] = "patch"
  post
end
post() click to toggle source
# File lib/spout/helpers/send_json.rb, line 35
def post
  return unless @error.nil?

  header = { "Content-Type" => "application/json", "Accept" => "application/json" }
  response = @http.start do |http|
    http.post(@url.path, @params.to_json, header)
  end
  [JSON.parse(response.body), response]
rescue => e
  puts "POST ERROR".red
  puts e.to_s.white
  nil
end