Module: Misty::HTTP::Request

Included in:
Client
Defined in:
lib/misty/http/request.rb

Instance Method Summary collapse

Instance Method Details

#decode?(response) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
12
# File 'lib/misty/http/request.rb', line 4

def decode?(response)
  return false if response.body.nil? || response.body.empty?
  if @config.content_type != :json && response.code =~ /2??/ && !response.is_a?(Net::HTTPNoContent) \
    && !response.is_a?(Net::HTTPResetContent) && response.header['content-type'] \
    && response.header['content-type'].include?('application/json')
    return true
  end
  false
end

#http(request) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/misty/http/request.rb', line 14

def http(request)
  Misty::HTTP::NetHTTP.http_request(
    @uri, ssl_verify_mode: @options.ssl_verify_mode, log: @config.log
  ) do |connection|
    response = connection.request(request)
    response.body = JSON.parse(response.body) if decode?(response)
    response
  end
end

#http_copy(path, headers) ⇒ Object



30
31
32
33
34
# File 'lib/misty/http/request.rb', line 30

def http_copy(path, headers)
  @config.log.info(http_to_s(path, headers))
  request = Net::HTTP::Copy.new(path, headers)
  http(request)
end

#http_delete(path, headers) ⇒ Object



24
25
26
27
28
# File 'lib/misty/http/request.rb', line 24

def http_delete(path, headers)
  @config.log.info(http_to_s(path, headers))
  request = Net::HTTP::Delete.new(path, headers)
  http(request)
end

#http_get(path, headers) ⇒ Object



36
37
38
39
40
# File 'lib/misty/http/request.rb', line 36

def http_get(path, headers)
  @config.log.info(http_to_s(path, headers))
  request = Net::HTTP::Get.new(path, headers)
  http(request)
end

#http_head(path, headers) ⇒ Object



42
43
44
45
46
# File 'lib/misty/http/request.rb', line 42

def http_head(path, headers)
  @config.log.info(http_to_s(path, headers))
  request = Net::HTTP::Head.new(path, headers)
  http(request)
end

#http_options(path, headers) ⇒ Object



48
49
50
51
52
# File 'lib/misty/http/request.rb', line 48

def http_options(path, headers)
  @config.log.info(http_to_s(path, headers))
  request = Net::HTTP::Options.new(path, headers)
  http(request)
end

#http_patch(path, headers, data) ⇒ Object



54
55
56
57
58
59
# File 'lib/misty/http/request.rb', line 54

def http_patch(path, headers, data)
  @config.log.info(http_to_s(path, headers, data))
  request = Net::HTTP::Patch.new(path, headers)
  request.body = Misty.to_json(data)
  http(request)
end

#http_post(path, headers, data) ⇒ Object



61
62
63
64
65
66
# File 'lib/misty/http/request.rb', line 61

def http_post(path, headers, data)
  @config.log.info(http_to_s(path, headers, data))
  request = Net::HTTP::Post.new(path, headers)
  request.body = Misty.to_json(data)
  http(request)
end

#http_put(path, headers, data) ⇒ Object



68
69
70
71
72
73
# File 'lib/misty/http/request.rb', line 68

def http_put(path, headers, data)
  @config.log.info(http_to_s(path, headers, data))
  request = Net::HTTP::Put.new(path, headers)
  request.body = Misty.to_json(data)
  http(request)
end

#http_to_s(path, headers, data = nil) ⇒ Object



75
76
77
78
# File 'lib/misty/http/request.rb', line 75

def http_to_s(path, headers, data = nil)
  log = "base_url='#{@uri.host}:#{@uri.port}', path='#{path}', header=#{headers}"
  log << ", data='#{data}'" if data
end