Class: HttpEnvironment

Inherits:
BaseEnvironment show all
Defined in:
lib/libisi/environment/http.rb

Overview

Environment accessible over http or https

Instance Attribute Summary

Attributes inherited from BaseEnvironment

#parent

Instance Method Summary collapse

Methods inherited from BaseEnvironment

#instance, #language, #name, #user

Constructor Details

#initialize(uri, options = {}) ⇒ HttpEnvironment

Returns a new instance of HttpEnvironment.



25
26
27
28
# File 'lib/libisi/environment/http.rb', line 25

def initialize(uri, options = {})
  @uri = uri
  super(nil)
end

Instance Method Details

#execute(task, options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
103
# File 'lib/libisi/environment/http.rb', line 64

def execute(task, options = {})
  url = uri_from_task(task, options)

  # TODO: initialize header
  headers = nil
  #headers = {
  #  'Cookie' => @cookie,
  #  'Referer' => 'http://profil.wp.pl/login.html',
  #  'Content-Type' => 'application/x-www-form-urlencoded'
  #}
  request = Net::HTTP::Get.new([url.path,url.query].compact.join("?"), headers)

  # TODO: login
  # request.basic_auth 'account', 'password'
  response = Net::HTTP.start(url.host, url.port) {|http|
    http.request(request)
  }
  
  # TODO: implement more responses
  case response
  when Net::HTTPSuccess     then response
    # only sucess are allowed at the moment
    #$log.info("Request sucessfuly done")
    #  when Net::HTTPRedirection then fetch(response['location'], limit - 1)
  else
    raise response.error!
  end

  # TODO: set cookie environment
  #@cookie = resp.response['set-cookie'] if
  #  resp.response['set-cookie'] if          

  if out = task.parameter.options[:output_stream]
    out.write(response.body)
    out.close_write
    #response.read_body(task.parameter.options[:output_stream])
  else
    response.body
  end
end

#implementationObject



31
# File 'lib/libisi/environment/http.rb', line 31

def implementation; :ruby; end

#typeObject



30
# File 'lib/libisi/environment/http.rb', line 30

def type; :http_access; end

#uriObject



32
# File 'lib/libisi/environment/http.rb', line 32

def uri; @uri; end

#uri_from_task(task, options = {}) ⇒ Object



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
# File 'lib/libisi/environment/http.rb', line 34

def uri_from_task(task, options = {})
  m_uri = uri.to_s
  m_uri = m_uri[0..-2] if m_uri =~ /\/$/
  m_uri += "/" + task.function.context
  m_uri += "/" + task.function.name
  m_uri += "/" + task.parameter.arguments.join("/")

  params = task.parameter.options.reject {|name, vals|
    [:input_stream,:output_stream].include?(name)      
  }.map {|name, vals|
    case vals
    when Array, NilClass
    when String
	vals = [vals]
    else
	raise "Dont know how to parametrize class #{vals.class}"
    end
    
    vals.map {|val|
p [name, val]
	if val
 "#{name}=#{CGI.escape(val)}"
	else
 name
	end
    }
  }.join("&")
  URI.parse(m_uri + "?" + params)
end