Class: Lambom::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/lambom/api.rb

Constant Summary collapse

DEFAULT_API_URL =
"https://riyic.com/api/v1"

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ ApiClient

Returns a new instance of ApiClient.



12
13
14
15
16
17
18
# File 'lib/lambom/api.rb', line 12

def initialize(conf)
    @server_id = conf.server or raise "Required parameter \"server\" not found"
    @private_key_file = conf.private_key_file or raise "Required parameter \"private_key_file\" not found"
    @env = conf.environment

    @api_url = conf.api_url || DEFAULT_API_URL
end

Instance Method Details

#get(controller, action = '', params = {}) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/lambom/api.rb', line 31

def get(controller, action='',params={})
    uri = build_uri(controller,action)
    req = Net::HTTP::Get.new(uri.request_uri)
    req.body=generate_body_string(params)
    send(uri,req)

end

#get_berksfileObject



25
26
27
# File 'lib/lambom/api.rb', line 25

def get_berksfile
    get("servers","#{@server_id}/berksfile")
end

#get_server_configObject



20
21
22
23
# File 'lib/lambom/api.rb', line 20

def get_server_config
    get('servers',"#{@server_id}/generate_config");

end

#post(controller, action = '', params = {}) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/lambom/api.rb', line 39

def post(controller, action='',params={})
    uri = build_uri(controller,action)
    req = Net::HTTP::Post.new(uri.request_uri)
    #req.set_form_data(params)
    req.body=generate_body_string(params)
    send(uri,req)
end

#send(uri, req) ⇒ Object



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
75
76
77
78
79
# File 'lib/lambom/api.rb', line 47

def send(uri,req)
    
    ## agregamos os headers da signature
    headers = generate_headers(@server_id, @private_key_file, req)
    puts headers.inspect if $debug
    
    headers.each do |header,value|
        req[header] = value
    end
    
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true if uri.scheme == 'https'

    response = http.request(req)
    puts response.body if $debug

    if response.is_a?(Net::HTTPSuccess)
        return response.body
    else
        puts "code:#{response.code}, message:#{response.message}" if $debug
        raise "api error: #{response.body}"
    end

    #puts resp.inspect if @debug
    #response = Oj.load(response.body)

    #if response["status"] == "OK" 
    #    return response
    #else
    #    raise response.body
    #end
        
end