Module: CodewarsApi::RequestHelper

Defined in:
lib/codewars_api/helpers/request_helper.rb

Class Method Summary collapse

Class Method Details

.add_api_key(request_options, api_key) ⇒ Object



8
9
10
11
12
# File 'lib/codewars_api/helpers/request_helper.rb', line 8

def self.add_api_key(request_options, api_key)
  request_options = request_options.dup
  request_options[:headers] = { 'Authorization' => api_key }
  request_options
end

.add_body_options(request_options, body_options) ⇒ Object



14
15
16
17
18
# File 'lib/codewars_api/helpers/request_helper.rb', line 14

def self.add_body_options(request_options, body_options)
  request_options = request_options.dup
  request_options[:body] = body_options
  request_options
end

.get(url, options = nil) ⇒ Object



29
30
31
32
33
# File 'lib/codewars_api/helpers/request_helper.rb', line 29

def self.get(url, options = nil)
  uri = URI(CodewarsApi::BASE_URL + url)
  headers = options[:headers] if options
  https(uri).request_get uri, headers
end

.post(url, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/codewars_api/helpers/request_helper.rb', line 20

def self.post(url, options = {})
  uri = URI(CodewarsApi::BASE_URL + url)
  headers = { 'Content-Type' => 'application/json' }
  headers.merge! options[:headers] if options
  request = Net::HTTP::Post.new(uri.path, headers)
  request.body = options[:body].to_json if options
  https(uri).request(request)
end