Class: InternetHakai::ClientHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/internethakai/client_handler.rb

Overview

httpクライアントをいじるクラス

Direct Known Subclasses

MobileClientHandler, SocialClientHandler

Constant Summary collapse

UNIQUE_BY_THREAD =
true
@@var_stores =
{}
@@vars =
Hash::new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scenario) ⇒ ClientHandler

Returns a new instance of ClientHandler.



57
58
59
# File 'lib/internethakai/client_handler.rb', line 57

def initialize scenario
    @scenario = scenario
end

Instance Attribute Details

#performance_idObject

Returns the value of attribute performance_id.



56
57
58
# File 'lib/internethakai/client_handler.rb', line 56

def performance_id
  @performance_id
end

#varsObject

Returns the value of attribute vars.



56
57
58
# File 'lib/internethakai/client_handler.rb', line 56

def vars
  @vars
end

Class Method Details

.on_config_load(config) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
# File 'lib/internethakai/client_handler.rb', line 6

def self::on_config_load config
    @@var_stores = {}
    config['actions'] = config['actions'].map do |actconfig|
        actconfig['usr_var'] = false
        if(/%\(.*\)%/ =~ actconfig['path'])
            actconfig['use_var'] = true
        end
        if(actconfig.has_key?('post_string') && /%25\(.*\)%25/ =~ actconfig['post_string'])
            actconfig['use_var'] = true
        end
        actconfig
    end
    # ファイルでセットする変数
    if(config.has_key?('var_file') && config.has_key?('var_name'))
        s = File::open(config['var_file']){|io|io.read}
        var_ids = []
        if s
            lines = s.split("\n")
            var_ids = lines.sort_by{rand}
        end
        unless var_ids.empty?
            var_name = config['var_name']
            @@var_stores[var_name] = var_ids
        end
    end
    # 複数ファイルの場合
    if(config.has_key?('vars') && config['vars'].is_a?(Array))
        config['vars'].each do |c|
            next unless c.has_key?('var_file') && c.has_key?('var_name')
            s = File::open(c['var_file']){|io|io.read}
            next if s.nil? || s==''
            ids = s.split("\n").sort_by{rand}
            @@var_stores[c['var_name']] = ids unless ids.empty?
        end
    end
    unless @@var_stores.empty?
        def set_var_id
            @@var_stores.each do |var_name, ids|
                id = ids[@performance_id % ids.size]
                set_var(var_name, id)
            end
        end
    else
        #変数がないときは何もしない
        def set_var_id
        end
    end
    config
end

Instance Method Details

#extend_vars(str) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/internethakai/client_handler.rb', line 69

def extend_vars str
    str = str.to_s.clone
    for k,v in @scenario.vars
        str.gsub!("%(#{k})%", v)
    end
    str
end

#extend_vars2(str) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/internethakai/client_handler.rb', line 76

def extend_vars2 str
    str = str.to_s.clone
    for k,v in @scenario.vars
        str.gsub!("%25(#{k})%25", v)
    end
    str
end

#handle_body(client, body, opt) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/internethakai/client_handler.rb', line 91

def handle_body client, body, opt
    if opt['use_var']
        set_var_id
        extend_vars2(body)
    else
        body
    end
end

#handle_client(client) ⇒ Object



99
100
101
# File 'lib/internethakai/client_handler.rb', line 99

def handle_client client
    @client = client
end

#handle_url(client, url, opt) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/internethakai/client_handler.rb', line 83

def handle_url client, url, opt
    if opt['use_var']
        set_var_id
        extend_vars(url)
    else
        url
    end
end

#send(method, host, port, path) ⇒ Object



60
61
62
# File 'lib/internethakai/client_handler.rb', line 60

def send method, host, port, path
    @client = get_client unless @client
end

#set_opt(opt) ⇒ Object



63
64
65
# File 'lib/internethakai/client_handler.rb', line 63

def set_opt opt
    @opt = opt
end

#set_var(key, val) ⇒ Object



66
67
68
# File 'lib/internethakai/client_handler.rb', line 66

def set_var key, val
    @scenario.vars[key] = val
end

#set_var_idObject



102
103
# File 'lib/internethakai/client_handler.rb', line 102

def set_var_id
end