Class: ClientApi::Request

Inherits:
Object
  • Object
show all
Includes:
ClientApi
Defined in:
lib/client-api/request.rb

Direct Known Subclasses

Api

Constant Summary

Constants included from ClientApi

VERSION

Instance Method Summary collapse

Methods included from ClientApi

#base_url, #basic_auth, configuration, configure, #datatype, #deep_traverse, #headers, #is_num?, #json_output, #payload, #time_out, #validate, #validate_headers, #validate_json, #validate_schema

Constructor Details

#initialize(scenario) ⇒ Request

Returns a new instance of Request.


10
11
12
13
# File 'lib/client-api/request.rb', line 10

def initialize(scenario)
  @scenario = scenario
  $logger.debug("Requested scenario == '#{@scenario.description}'") if $logger
end

Instance Method Details

#basic_encode(options = {}) ⇒ Object


67
68
69
# File 'lib/client-api/request.rb', line 67

def basic_encode(options = {})
  'Basic ' + ["#{options[:username]}:#{options[:password]}"].pack('m0')
end

#connect(args) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/client-api/request.rb', line 52

def connect(args)
  http = Net::HTTP.new(uri(args).host, uri(args).port)

  if uri(args).scheme == "https"
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    http.read_timeout = time_out.to_i
    @http = http
  elsif uri(args).scheme == "http"
    http.use_ssl = false
    http.read_timeout = time_out.to_i
    @http = http
  end
end

#delete_request(url, options = {}) ⇒ Object


28
29
30
31
32
# File 'lib/client-api/request.rb', line 28

def delete_request(url, options = {})
  connect(url)
  pre_logger(:log_url => uri(url), :log_header => header(options), :log_method => 'DELETE') if $logger
  @http.delete(uri(url).path, initheader = header(options))
end

#get_request(url, options = {}) ⇒ Object


15
16
17
18
19
# File 'lib/client-api/request.rb', line 15

def get_request(url, options = {})
  connect(url)
  pre_logger(:log_url => uri(url), :log_header => header(options), :log_method => 'GET') if $logger
  @http.get(uri(url).request_uri, initheader = header(options))
end

#header(options = {}) ⇒ Object


71
72
73
74
75
76
# File 'lib/client-api/request.rb', line 71

def header(options = {})
  mod_headers = options[:headers] || {}
  authorization = basic_encode(:username => basic_auth['Username'], :password => basic_auth['Password'])
  headers['Authorization'] = basic_encode(:username => basic_auth['Username'], :password => basic_auth['Password']) if authorization != "Basic Og=="
  headers.merge(mod_headers)
end

#patch_request(url, options = {}) ⇒ Object


41
42
43
44
45
46
# File 'lib/client-api/request.rb', line 41

def patch_request(url, options = {})
  body = options[:body] || {}
  connect(url)
  pre_logger(:log_url => uri(url), :log_header => header(options), :log_body => body, :log_method => 'PATCH') if $logger
  @http.patch(uri(url).path, body.to_json, initheader = header(options))
end

#post_request(url, options = {}) ⇒ Object


21
22
23
24
25
26
# File 'lib/client-api/request.rb', line 21

def post_request(url, options = {})
  body = options[:body] || {}
  connect(url)
  pre_logger(:log_url => uri(url), :log_header => header(options), :log_body => body, :log_method => 'POST') if $logger
  @http.post(uri(url).path, body.to_json, initheader = header(options))
end

#pre_logger(options = {}) ⇒ Object


78
79
80
81
82
83
84
# File 'lib/client-api/request.rb', line 78

def pre_logger(options = {})
  options[:log_body] = 'not available' if options[:log_body].nil?
  $logger.debug("Requested method == #{options[:log_method]}")
  $logger.debug("Requested url == #{options[:log_url]}")
  $logger.debug("Requested headers == #{options[:log_header]}")
  $logger.debug("Requested body == #{options[:log_body]}")
end

#put_request(url, options = {}) ⇒ Object


34
35
36
37
38
39
# File 'lib/client-api/request.rb', line 34

def put_request(url, options = {})
  body = options[:body] || {}
  connect(url)
  pre_logger(:log_url => uri(url), :log_header => header(options), :log_body => body, :log_method => 'PUT') if $logger
  @http.put(uri(url).path, body.to_json, initheader = header(options))
end

#uri(args) ⇒ Object


48
49
50
# File 'lib/client-api/request.rb', line 48

def uri(args)
  %w[http://, https://].any? {|protocol| args.include? protocol} ? URI.parse(args) : URI.parse(base_url + args)
end