Class: Http

Inherits:
Object
  • Object
show all
Includes:
Client
Defined in:
lib/http_client.rb

Instance Attribute Summary

Attributes included from Client

#protocol, #url_base

Instance Method Summary collapse

Constructor Details

#initialize(url_base) ⇒ Http

Returns a new instance of Http.



13
14
15
# File 'lib/http_client.rb', line 13

def initialize(url_base)
  @url_base = url_base
end

Instance Method Details

#delete(method, params = nil, body = nil) ⇒ Object



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

def delete(method,params=nil,body=nil)
  url = URI(@url_base+"/"+method)
  Net::HTTP.start(url.host, url.port) do |http|
    request = Net::HTTP::Delete.new(url)
    if params
      params.each do |key, value|
        request[key] = value
      end
    end
    request.body = body.to_json if body
    return http.request(request)
  end
end

#get(method, params = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/http_client.rb', line 17

def get(method,params=nil)
  url = URI(@url_base+"/"+method)
  Net::HTTP.start(url.host, url.port) do |http|
    request = Net::HTTP::Get.new(url)
    if params
      params.each do |key, value|
        request[key] = value
      end
    end
    return http.request(request)
  end
end

#post(method, params = nil, body = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/http_client.rb', line 30

def post(method,params=nil,body=nil)
  url = URI(@url_base+"/"+method)
  Net::HTTP.start(url.host, url.port) do |http|
    request = Net::HTTP::Post.new(url)
    if params
      params.each do |key, value|
        request[key] = value
      end
    end
    request.body = body.to_json if body
    return http.request(request)
  end
end

#put(method, params = nil, body = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/http_client.rb', line 44

def put(method,params=nil,body=nil)
  url = URI(@url_base+"/"+method)
  Net::HTTP.start(url.host, url.port) do |http|
    request = Net::HTTP::Put.new(url)
    if params
      params.each do |key, value|
        request[key] = value
      end
    end
    request.body = body.to_json if body
    return http.request(request)
  end
end