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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gosquared/client.rb', line 47

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
	puts "Response Message: #{response.message}" if response
	response
end

#get(url) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gosquared/client.rb', line 8

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gosquared/client.rb', line 25

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'})
		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
	puts "Response Message: #{response.message}" if response
	response
end