Class: Gosquared::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gosquared/client.rb

Instance Method Summary collapse

Instance Method Details

#delete(url, data = {}) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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