Class: SterlingApi::RemoteActions::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/sterling_api/remote_actions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Request

Returns a new instance of Request.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sterling_api/remote_actions.rb', line 62

def initialize(options={})
  default_options = {
    :body       => nil,
    :headers    => {'Content-Type' => 'application/xml', 'Accept' => 'application/xml'},
    :proxy      => nil,
    :proxy_port => nil,
  }
  default_options.merge!(options)
  
  self.proxy      = default_options[:proxy]
  self.proxy_port = default_options[:proxy_port]
  self.headers    = default_options[:headers]
  self.body       = default_options[:body]
  self.url        = default_options[:url]
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



58
59
60
# File 'lib/sterling_api/remote_actions.rb', line 58

def body
  @body
end

#headersObject

Returns the value of attribute headers.



55
56
57
# File 'lib/sterling_api/remote_actions.rb', line 55

def headers
  @headers
end

#http_processObject

Returns the value of attribute http_process.



59
60
61
# File 'lib/sterling_api/remote_actions.rb', line 59

def http_process
  @http_process
end

#proxyObject

Returns the value of attribute proxy.



56
57
58
# File 'lib/sterling_api/remote_actions.rb', line 56

def proxy
  @proxy
end

#proxy_portObject

Returns the value of attribute proxy_port.



57
58
59
# File 'lib/sterling_api/remote_actions.rb', line 57

def proxy_port
  @proxy_port
end

#urlObject

Returns the value of attribute url.



60
61
62
# File 'lib/sterling_api/remote_actions.rb', line 60

def url
  @url
end

Instance Method Details

#host_from_urlObject



78
79
80
# File 'lib/sterling_api/remote_actions.rb', line 78

def host_from_url
  self.url.match(%r{(?:https?://)?([^/]+)})[1].gsub(/:\d+$/, "") rescue nil
end

#is_ssl?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/sterling_api/remote_actions.rb', line 82

def is_ssl?
  !!(self.url =~ /^https:/i)
end

#port_from_urlObject



86
87
88
89
90
91
92
93
94
# File 'lib/sterling_api/remote_actions.rb', line 86

def port_from_url
  if m = self.url.match(%r{(?:(http|https)://)[^:]+:(\d+)/?})
    return m[2].to_i
  elsif self.url =~ /^https:/i
    return 443
  else
    80
  end
end

#send_requestObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sterling_api/remote_actions.rb', line 96

def send_request
  self.http_process = Net::HTTP.new( host_from_url, port_from_url, self.proxy, self.proxy_port )
  self.http_process.use_ssl = is_ssl?

  http_process.start
  resp = http_process.post(self.url, self.body, self.headers)
  ret_val = false
  case resp
  when Net::HTTPSuccess, Net::HTTPRedirection
    ret_val = true
  else
    ret_val = false
  end
  return SterlingApi::RemoteActions::Response.new(ret_val, resp)
end