Method: Base#request

Defined in:
lib/blocksdk_ruby/base.rb

#request(method, path, data = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/blocksdk_ruby/base.rb', line 35

def request(method,path,data = {})
	url = "https://api.blocksdk.com/v1" + path 
	if method == "GET" and data.size > 0
		url += "?"
		data.each do |key, value|
			if value == true	 
				url += key + "=true&"
			elsif value == false
				url += key + "=false&"
			else
				url += key+ "=" + value.to_s + "&"
			end
		end
	end

	if method == "POST"
		response = HTTParty.post(url, :data => data, :headers => { 'Content-Type': 'application/json','x-api-key': @api_token})	
	else
		response = HTTParty.get(url,:headers => { 'Content-Type': 'application/json','x-api-key': @api_token})	
	end

	header_dict = response.headers
	body = JSON.parse(response.body)

	header_dict["statusCode"] = response.code

	begin
		body["HTTP_HEADER"] = header_dict
	rescue
		body_dict = {}
		for i in 0..(body.length - 1)
			ind = i.to_s
			body_dict[ind] = body[i]
		end
		body = body_dict
		body["HTTP_HEADER".to_s] = header_dict
	end
	
	return body	
end