Module: Airborne::RestClientRequester

Defined in:
lib/spaceborne.rb

Overview

spaceborne enhancements

Instance Method Summary collapse

Instance Method Details

#body?(method) ⇒ Boolean



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

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

#calc_body(options, local) ⇒ Object



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

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



94
95
96
97
98
99
100
101
# File 'lib/spaceborne.rb', line 94

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



103
104
105
106
107
# File 'lib/spaceborne.rb', line 103

def handle_proxy(_options, local)
  return unless local[:proxy]

  RestClient.proxy = local[:proxy]
end

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



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/spaceborne.rb', line 136

def make_request(method, url, options = {})
  hdrs, body = pre_request(options)
  send_restclient(method, get_url(url), body, hdrs)
rescue RestClient::ServerBrokeConnection => e
  raise e
rescue RestClient::Exception => e
  if [301, 302].include?(e.response&.code)
    e.response.&follow_redirection
  else
    e.response
  end
end

#pre_request(options) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/spaceborne.rb', line 127

def pre_request(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)
  [hdrs, @request_body]
end

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



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

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



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

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