Module: HTTPHelper

Defined in:
lib/gofile_ruby/http_helper.rb

Overview

A module for simplifying HTTP requests

Class Method Summary collapse

Class Method Details

.delete(url, data) ⇒ Object

Makes a DELETE request to the specified URL.

If the given URL is a string, it gets converted to a URI.

Parameters:

  • url (String, URI)

    The URL for the request.

  • data (Array<Array<String, T>>)

    The data for the post request.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gofile_ruby/http_helper.rb', line 84

def self.delete(url, data)
  url = URI(url) unless url.class == URI::Generic

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  req = Net::HTTP::Delete.new(url, { 'Content-Type' => 'application/x-www-form-urlencoded' })
  req.body = URI::encode_www_form(data)

  res = http.request(req)

  ret = res.body
  ret = JSON.parse(ret)

  ret
end

.get(url) ⇒ Object

Makes a GET request to the specified URL.

If the given URL is a string, it gets converted to a URI.

Parameters:

  • url (String, URI)

    The URL for the request.



11
12
13
14
15
16
17
18
19
# File 'lib/gofile_ruby/http_helper.rb', line 11

def self.get(url)
  url = URI(url) unless url.class == URI::Generic
  res = Net::HTTP.get_response(url)
  
  ret = res.body
  ret = JSON.parse(ret)

  ret
end

.post_form(url, data) ⇒ Object

POSTs the data as a form to the specified URL.

If the given URL is a string, it gets converted to a URI.

Parameters:

  • url (String, URI)

    The URL for the request.

  • data (Hash)

    The data for the post request.



26
27
28
29
30
31
32
33
34
35
# File 'lib/gofile_ruby/http_helper.rb', line 26

def self.post_form(url, data)
  url = URI(url) unless url.class == URI::Generic

  res = Net::HTTP.post_form(url, data)
  
  ret = res.body
  ret = JSON.parse(ret)

  ret
end

.post_multipart_data(url, data) ⇒ Object

Takes an array of arrays and POSTs them as multipart form data to the specified URL.

If the given URL is a string, it gets converted to a URI.

Parameters:

  • url (String, URI)

    The URL for the request.

  • data (Array<Array<String, T>>)

    The data for the post request.



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

def self.post_multipart_data(url, data)
  url = URI(url) unless url.class == URI::Generic

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  req = Net::HTTP::Post.new(url)
  req.set_form data, 'multipart/form-data'

  res = http.request(req)

  ret = res.body
  ret = JSON.parse(ret)

  ret
end

.put(url, data) ⇒ Object

Makes a PUT request to the specified URL.

If the given URL is a string, it gets converted to a URI.

Parameters:

  • url (String, URI)

    The URL for the request.

  • data (Array<Array<String, T>>)

    The data for the post request.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gofile_ruby/http_helper.rb', line 63

def self.put(url, data)
  url = URI(url) unless url.class == URI::Generic

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  req = Net::HTTP::Put.new(url, { 'Content-Type' => 'application/x-www-form-urlencoded' })
  req.body = URI::encode_www_form(data)

  res = http.request(req)

  ret = res.body
  ret = JSON.parse(ret)

  ret
end