Class: XRBP::WebClient::Connection

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/xrbp/webclient/connection.rb

Overview

HTTP interface, use Connection to perform web requests.

Examples:

retrieve data from the web

connection = WebClient::Connection.new
connection.url = "https://devnull.network"
connection.perform

Constant Summary collapse

DELEGATED_METHODS =
[:url=,
:timeout=,
:ssl_verify_peer=,
:ssl_verify_host=]

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) {|_self| ... } ⇒ Connection

Returns a new instance of Connection.

Yields:

  • (_self)

Yield Parameters:



45
46
47
48
49
50
# File 'lib/xrbp/webclient/connection.rb', line 45

def initialize(url=nil)
  self.url = url
  @force_quit = false

  yield self if block_given?
end

Instance Method Details

#force_quit!Object

Immediate terminate outstanding requests



57
58
59
60
61
# File 'lib/xrbp/webclient/connection.rb', line 57

def force_quit!
  @force_quit = true
  wake_all
  # TODO immediate terminate outstanding requests
end

#force_quit?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/xrbp/webclient/connection.rb', line 52

def force_quit?
  @force_quit
end

#performObject

Execute web request, retrieving results and returning



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/xrbp/webclient/connection.rb', line 78

def perform
  # TODO fault tolerance plugins:
  #        configurable timeout,
  #        round-robin urls,
  #        redirect handling, etc
  begin
    c.perform
  rescue => e
    emit :error, e
    return handle_error
  end

  if c.response_code != 200
    emit :http_error, c.response_code
    return handle_error
  end

  emit :success, c.body_str
  begin
    parse_result(c.body_str, c)
  rescue Exception => e
    emit :error, e
    return nil
  end
end

#urlObject

Return current url



34
35
36
# File 'lib/xrbp/webclient/connection.rb', line 34

def url
  c.url
end