Class: Cudan::BaseCudan

Inherits:
Object
  • Object
show all
Defined in:
lib/cudan/base_cudan.rb

Direct Known Subclasses

RegexpCudan, XpathCudan

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBaseCudan

Returns a new instance of BaseCudan.



8
9
10
11
# File 'lib/cudan/base_cudan.rb', line 8

def initialize
    @headers = {}
    @showbody = false
end

Instance Attribute Details

#showbodyObject

Returns the value of attribute showbody.



12
13
14
# File 'lib/cudan/base_cudan.rb', line 12

def showbody
  @showbody
end

Instance Method Details

#do_expect(expect) ⇒ Object



58
59
60
# File 'lib/cudan/base_cudan.rb', line 58

def do_expect expect
    @query_result.include? expect
end

#do_messageObject



50
51
52
53
54
# File 'lib/cudan/base_cudan.rb', line 50

def do_message
    Logger.log 'failure:'
    Logger.log "query: #{@query}"
    Logger.log @query_result.gsub(/^(.*)$/){|r| "\t" + r}
end

#do_query(query) ⇒ Object



55
56
57
# File 'lib/cudan/base_cudan.rb', line 55

def do_query query
    @response.include? query ? query : ''
end

#execute(query, expect, url, method = GET, body = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cudan/base_cudan.rb', line 39

def execute query, expect, url, method=GET, body=nil
    @query = query
    fetch(url, method, body)
    @query_result = do_query(query)
    r = do_expect(expect)
    unless r
        do_message
        exit 1
    else
    end
end

#fetch(url, method = GET, body = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cudan/base_cudan.rb', line 16

def fetch url, method=GET, body=nil
    host, port, path = Util::parse_url(url)
    @post_string = body
    if method == GET
        req = Net::HTTP::Get::new(path, @headers)
    elsif method == POST
        req = Net::HTTP::Post::new(path, @headers)
    end
    response = nil
    Net::HTTP.start(host, port) do |http|
        @starttime = Time::now
        response = http.request(req, @post_string)
        @response_time = Time::now - @starttime
    end
    if response
        @response = response.body.to_s
    else
        @response = ''
    end
    if @showbody
        Logger.log(@response, 2)
    end
end

#set_header(key, value) ⇒ Object



13
14
15
# File 'lib/cudan/base_cudan.rb', line 13

def set_header key, value
    @headers[key] = value
end