Class: RestClientW

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

Instance Method Summary collapse

Constructor Details

#initialize(base_url, headers) ⇒ RestClientW

Returns a new instance of RestClientW.



10
11
12
13
# File 'lib/restClientW.rb', line 10

def initialize(base_url, headers)
    @base_headers = headers
    @base_url = base_url    
end

Instance Method Details

#fill_header_params_for_get(params) ⇒ Object



15
16
17
18
19
20
# File 'lib/restClientW.rb', line 15

def fill_header_params_for_get(params)
    headrs = {}
    headrs["params"] = params
    @base_headers.each_pair {|key, value| headrs[key] = value}
    return headrs
end

#fill_postfile_multipart(multipart, payload) ⇒ Object



52
53
54
55
# File 'lib/restClientW.rb', line 52

def fill_postfile_multipart(multipart, payload)
    payload.each_pair {|key, value| multipart[key] = value }
    return multipart
end

#get(path, params = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/restClientW.rb', line 22

def get(path, params={})
    url = @base_url + path
    headrs = fill_header_params_for_get(params)
    
    resp = nil
    begin
        resp = RestClient::Request.execute(:method => :get, :url => url, :headers => headrs)
    rescue RestClient::ExceptionWithResponse => exp
        raise RiminderResponseException.new("Invalid Response: ", exp)
    rescue RestClient::Exception => exp
        raise RiminderTransfertException.new("Error during transfert: ", exp)
    end
    return JSON.parse(resp)
end

#patch(path, payload = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/restClientW.rb', line 73

def patch(path, payload={})
    url = @base_url + path

    json_payload = payload.to_json
    begin
        resp = RestClient.patch url, json_payload, @base_headers
    rescue RestClient::ExceptionWithResponse => exp
        raise RiminderResponseException.new("Invalid Response: ", exp)
    rescue RestClient::Exception => exp
        raise RiminderTransfertException.new("Error during transfert: ", exp)
    end
    return JSON.parse(resp)
end

#post(path, payload = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/restClientW.rb', line 37

def post(path, payload={})
    url = @base_url + path

    json_payload = payload.to_json
    begin
        resp = RestClient.post url, json_payload, @base_headers
    rescue RestClient::ExceptionWithResponse => exp
        raise RiminderResponseException.new("Invalid Response: ", exp)
    rescue RestClient::Exception => exp
        raise RiminderTransfertException.new("Error during transfert: ", exp)
    end

    return JSON.parse(resp)
end

#postfile(path, file_path, payload = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/restClientW.rb', line 57

def postfile(path, file_path, payload={})
    url = @base_url + path
    
    multipart = {:file => File.new(file_path, 'rb'), :multipart => true}
    multipart = fill_postfile_multipart(multipart, payload)
    
    begin
        resp = RestClient.post url, multipart, @base_headers
    rescue RestClient::ExceptionWithResponse => exp
        raise RiminderResponseException.new("Invalid Response: ", exp)
    rescue RestClient::Exception => exp
        raise RiminderTransfertException.new("Error during transfert: ", exp)
    end
    return JSON.parse(resp)
end