Class: DLClient

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

Instance Method Summary collapse

Constructor Details

#initialize(key, secret, host = nil, port = nil, verify_ssl = true) ⇒ DLClient

Returns a new instance of DLClient.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/datalanche/client.rb', line 13

def initialize(key, secret, host = nil, port = nil, verify_ssl = true)

    @auth_key = key
    @auth_secret = secret
    @url = 'https://api.datalanche.com'
    @verify_ssl = verify_ssl
    @verify_mode = OpenSSL::SSL::VERIFY_PEER

    if host != nil
        @url = 'https://' + host
    end

    if port != nil
        @url = @url + ':' + port.to_s()
    end

    if verify_ssl != true
        @verify_mode = OpenSSL::SSL::VERIFY_NONE            
    end

end

Instance Method Details

#get_debug_info(res, req) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/datalanche/client.rb', line 43

def get_debug_info(res,req)
    info = Hash.new

    info['request'] = Hash.new
    info['request']['method'] = req['method']
    info['request']['url'] = req['url']
    info['request']['headers'] = req['headers']
    info['request']['body'] = req['body']

    info['response'] = Hash.new
    info['response']['http_status'] = res.code # r.status_code
    info['response']['message'] = res.message
    info['response']['http_version'] = res.http_version

    return info
end

#key(key) ⇒ Object



35
36
37
# File 'lib/datalanche/client.rb', line 35

def key(key)
    @auth_key = key
end

#query(q = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/datalanche/client.rb', line 60

def query(q = nil)
    if q == nil
        raise Exception('query == nil')
    end

    url = @url

    if q.params.has_key?('database')
        url = url + '/' + q.params['database']
        q.params.delete('database')
    end

    url = URI.parse(url + '/query')

    header = {
               # 'Accept-Encoding'=>'gzip', # will be resumed after method of decompression of gzip found
                'Content-Type'=>'application/json',
                'User-Agent'=>'Datalanche Ruby Client'
            }
    
    req = Net::HTTP::Post.new(url.path, header)
    req.basic_auth @auth_key, @auth_secret

    https = Net::HTTP.new(url.host,url.port)
    https.use_ssl = true 
    https.verify_mode = @verify_mode
    https.ssl_version = "SSLv3"

    req.body = "#{q.params.to_json}"
    res = https.request(req)

    result = Hash.new
    req_info = Hash.new

    req_info['headers'] = header
    req_info['url'] = url
    req_info['method'] = req.method
    req_info['body'] = JSON.parse(req.body)

    debug_info = self.get_debug_info(res,req_info)

    begin
        result['data'] = JSON.parse(res.body)
    rescue  # in case the server does not return a body
        result['data'] = nil
    end

    result['response'] = debug_info['response']
    result['request'] = debug_info['request']


    status_code = res.code.to_i 
    if not (200 <= status_code and status_code < 300)
        raise DLException.new(res.code, result['data'], debug_info),"Http request error: "
    end
    return result

end

#secret(secret) ⇒ Object



39
40
41
# File 'lib/datalanche/client.rb', line 39

def secret(secret)
    @auth_secret = secret
end