Class: Nx::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/nx/http.rb,
lib/nx/version.rb

Constant Summary collapse

VERSION =
"0.1.7"

Class Method Summary collapse

Class Method Details

.request(in_method, in_url, in_data = {}, in_options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nx/http.rb', line 17

def self.request(in_method, in_url, in_data = {}, in_options = {})
  # uri:
  uri = URI(in_url)
  method = in_method.downcase

  # http:
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"

  # request:
  method_class = Net::HTTP.const_get method.capitalize
  request = method_class.new(uri)

  # callback area:
  if method == "get"
    uri.query = URI.encode_www_form(in_data)
  else
    in_options.each do |key, value|
      if key == :content_type
        request[key] = ContentType.const_get value.upcase
      else
        request[key] = value
      end
    end
  end

  if block_given?
    yield(uri, method, request, http)
  end

  begin
    http.request(request)
  rescue => exception
    raise exception
  end
end

.upload(in_url, in_data) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/nx/http.rb', line 3

def self.upload(in_url, in_data)
  url = URI(in_url)
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = url.scheme == "https"

  data = DataTransform.multipart(in_data)
  request = Net::HTTP::Post.new(url.path)
  request.set_form(data, ContentType::MULTIPART)
  if block_given?
    yield(http, request)
  end
  http.request(request)
end