Class: Base

Inherits:
Object
  • Object
show all
Defined in:
lib/blocksdk_ruby/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_token) ⇒ Base

Returns a new instance of Base.



7
8
9
# File 'lib/blocksdk_ruby/base.rb', line 7

def initialize(api_token)
	@api_token = api_token
end

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



6
7
8
# File 'lib/blocksdk_ruby/base.rb', line 6

def api_token
  @api_token
end

Instance Method Details

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/blocksdk_ruby/base.rb', line 11

def request(method,path,data = {})
	url = "https://api.blocksdk.com/v2" + 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