Class: WebClient

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

Class Method Summary collapse

Class Method Details

.get(url) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/web_client.rb', line 23

def self.get(url)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)

  request = Net::HTTP::Get.new(
    uri.request_uri,
    'Content-Type' => 'application/json'
  )

  response = http.request(request)
  unless response.body.nil?
    JSON.parse(response.body)
  else
    {}
  end
end

.post(url, body) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/web_client.rb', line 5

def self.post(url, body)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)

  request = Net::HTTP::Post.new(
    uri.request_uri,
    'Content-Type' => 'application/json'
  )
  request.body = body.to_json

  response = http.request(request)
  unless response.body.nil?
    JSON.parse(response.body)
  else
    {}
  end
end