Module: Airborne::RestClientRequester

Defined in:
lib/spaceborne.rb

Overview

spaceborne enhancements

Instance Method Summary collapse

Instance Method Details

#body?(method) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
# File 'lib/spaceborne.rb', line 68

def body?(method)
  case method
  when :post, :patch, :put
    true
  else
    false
  end
end

#calc_body(options, local) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/spaceborne.rb', line 99

def calc_body(options, local)
  return '' unless options[:body]
  if local[:nonjson_data] || !local[:is_hash]
    options[:body]
  else
    options[:body].to_json
  end
end

#calc_headers(options, local) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/spaceborne.rb', line 86

def calc_headers(options, local)
  headers = base_headers.merge(options[:headers] || {})
  headers[:no_restclient_headers] = true
  return headers unless local[:is_hash]
  headers.delete('Content-Type') if options[:nonjson_data]
  headers
end

#handle_proxy(_options, local) ⇒ Object



94
95
96
97
# File 'lib/spaceborne.rb', line 94

def handle_proxy(_options, local)
  return unless local[:proxy]
  RestClient.proxy = local[:proxy]
end

#make_request(method, url, options = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/spaceborne.rb', line 116

def make_request(method, url, options = {})
  @json_body = nil
  local_options = split_options(options)
  handle_proxy(options, local_options)
  hdrs = calc_headers(options, local_options)
  @request_body = calc_body(options, local_options)
  send_restclient(method, get_url(url), @request_body, hdrs)
rescue RestClient::Exception => e
  e.response
end

#send_restclient(method, url, body, headers) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/spaceborne.rb', line 108

def send_restclient(method, url, body, headers)
  if body?(method)
    RestClient.send(method, url, body, headers)
  else
    RestClient.send(method, url, headers)
  end
end

#split_options(options) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/spaceborne.rb', line 77

def split_options(options)
  local = {}
  local[:nonjson_data] = options.dig(:headers, :nonjson_data)
  options[:headers].delete(:nonjson_data) if local[:nonjson_data]
  local[:is_hash] = options[:body].is_a?(Hash)
  local[:proxy] = options.dig(:headers, :use_proxy)
  local
end