Class: Educandus::Estab

Inherits:
Object
  • Object
show all
Defined in:
lib/educandus_api/estab.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, path, user, pass) ⇒ Estab

Returns a new instance of Estab.



7
8
9
10
11
12
13
# File 'lib/educandus_api/estab.rb', line 7

def initialize(url, path,user,pass)
  @url = url
  @user = user
  @pass = pass
  @path = path
  @response = Hash.new
end

Instance Method Details

#connectObject



14
15
16
17
18
19
20
21
22
# File 'lib/educandus_api/estab.rb', line 14

def connect
  self.setupConnection
  begin
    self.doConnect
    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
   Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    raise Educandus::Error,  "The Host seems to be down (like always)."
  end
end

#sendRequest(options = {}) ⇒ Object

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/educandus_api/estab.rb', line 24

def sendRequest(options = {})
  raise Educandus::Error, "The response must be valid!." unless (!@response.empty? || !@cookie.nil?)
  if options.has_key?("url")
    url = options[:url]
  else
    url = @url
  end
  if options.has_key?("headers") && options[:headers].empty?
    headers = {"Cookies" => @response["set-cookies"]}
  else
    if !@response.empty?
      headers = {"Cookie" => @response["set-cookie"]}
    else
      headers = {}
    end
  end
  if options.has_key?("path")
    path = options[:path]
  else
    path = "/"
  end
  if options.has_key?("post")
    if !options.has_key?("method") || !options[:method] == "post"
      raise Educandus::Error, "If you pass POST parameters, you must set the METHOD to POST"
    else
      post = options[:post]
    end
  end
  @connection = Net::HTTP.new(url,443)
  @connection.use_ssl = true
    if options.has_key?(:method)
      method = options[:method].downcase
    else
      method = "get"
    end
      begin
        method == "post" ? (rsp, data = @connection.post(path,post,headers)) : ''
        method == "get"  ? (rsp, data = @connection.get(path,headers)) : '' 
    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, 
           Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
      raise Educandus::Error, "The Host seems to be down (like always)."
    end
  puts rsp.body
end