class Gosquared::Client

Public Instance Methods

delete(url, data={}) click to toggle source
# File lib/gosquared/client.rb, line 46
def delete(url, data={})
  uri = URI.parse(url)
  begin
    https = Net::HTTP.new(uri.host, uri.port)
    https.use_ssl = true
    request = Net::HTTP::Delete.new(uri.request_uri, initheader = { 'Content-Type' => 'application/json' })
    request.body = "[#{data.to_json}]"
    response = https.request(request)
  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
         Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    puts "[error] HTTP error: #{e}"
    begin
        response.message
      rescue StandardError => e
        puts '[error] StandardError: Could not print response message'
        response = false
      end
  end
  response
end
get(url) click to toggle source
# File lib/gosquared/client.rb, line 7
def get(url)
  uri = URI(url)
  begin
    response = Net::HTTP.get(uri)
  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
         Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    puts "[error] HTTP error: #{e}"
    begin
      JSON.parse(response)
    rescue StandardError => e
      puts '[error] StandardError: Could not parse JSON'
      response = false
    end
  end
  @data = JSON.parse(response) if response
end
post(url, data) click to toggle source
# File lib/gosquared/client.rb, line 24
  def post(url, data)
    uri = URI.parse(url)
    begin
      https = Net::HTTP.new(uri.host, uri.port)
      https.use_ssl = true
      request = Net::HTTP::Post.new(uri.request_uri, initheader = { 'Content-Type' => 'application/json', 'User-Agent' => 'ruby-client/' + VERSION })
      request.body = "[#{data.to_json}]"
      response = https.request(request)
    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
           Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
      puts "[error] HTTP error: #{e}"
      begin
          response.message
        rescue StandardError => e
          puts '[error] StandardError: Could not print response message'
          response = false
        end
    end

    response
end