class Base

Attributes

api_token[RW]

Public Class Methods

new(api_token) click to toggle source
# File lib/blocksdk_ruby/base.rb, line 7
def initialize(api_token)
        @api_token = api_token
end

Public Instance Methods

request(method,path,data = {}) click to toggle source
# 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