Class: Watobo::OTTCache

Inherits:
Object
  • Object
show all
Defined in:
lib/watobo/core/ott_cache.rb

Overview

:nodoc: all

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOTTCache

Returns a new instance of OTTCache.



9
10
11
12
# File 'lib/watobo/core/ott_cache.rb', line 9

def initialize()
  @tokens = {}
  @tokens_lock = Mutex.new
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



7
8
9
# File 'lib/watobo/core/ott_cache.rb', line 7

def tokens
  @tokens
end

Class Method Details

.acquire(request) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/watobo/core/ott_cache.rb', line 14

def self.acquire(request)
  urh = request.uniq_hash
  unless @otts.has_key? urh
    @otts[urh] = OTTCache.new()
  end
  @otts[urh]
end

.chats(target, &block) ⇒ Object

returns an array of Watobo::Requests which are necessary to update the token



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/watobo/core/ott_cache.rb', line 91

def self.chats(target, &block)
  ott_chats = []
  return ott_chats if target.nil?
  request = target.respond_to?(:request) ? target.request : target
  urh = request.uniq_hash
  
  @otts_lock.synchronize do
    return ott_chats unless Watobo::Conf::OttCache.request_ids.has_key? urh
    Watobo::Conf::OttCache.request_ids[urh].each do |id|
     #puts "* [OTT] get chat for id #{id}"
      chat = Watobo::Chats.get_by_id(id)
      unless chat.nil?  
      #ottr = chat.copyRequest
      ott_chats << chat 
      yield chat if block_given?
      end
    end
  end
  ott_chats
end

.patterns(&block) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/watobo/core/ott_cache.rb', line 23

def self.patterns(&block)
   
    Watobo::Conf::OttCache.patterns.each do |p|
      yield p if block_given?
    end
    YAML.load(YAML.dump(Watobo::Conf::OttCache.patterns))
   
end

.requests(target, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/watobo/core/ott_cache.rb', line 79

def self.requests(target, &block)
  ott_requests = []
  return ott_requests if target.nil?
  self.chats(target) do |chat|
    ott_request = chat.copyRequest
    ott_requests << ott_request
  end
  ott_requests
end

.set_chat_ids(target, ott_chat_ids) ⇒ Object

target could be a Watobo::Chat or a Watobo::Request object



71
72
73
74
75
76
77
# File 'lib/watobo/core/ott_cache.rb', line 71

def self.set_chat_ids(target, ott_chat_ids)
  r = target
  r = target.request if target.respond_to? :request
  @otts_lock.synchronize do
    Watobo::Conf::OttCache.request_ids[r.uniq_hash] = ott_chat_ids
  end
end

Instance Method Details

#update_request(request) ⇒ Object

update tokens for a specific request



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/watobo/core/ott_cache.rb', line 114

def update_request(request)     
#urh = target_request.uniq_hash 
#return false unless @tokens.has_key? urh       
  @tokens_lock.synchronize do       
    request.map!{ |line|
      res = line
      self.class.patterns do |pat|
        begin
          if line =~ /#{pat}/i then
            key = Regexp.quote($1.upcase)
            old_value = $2
            if @tokens.has_key?(key) then
              res = line.gsub!(/#{Regexp.quote(old_value)}/, @tokens[key])
              if res.nil? then
                res = line
                puts "!!!could not update token (#{key})"
              end
            else
              if $DEBUG
                puts "[OTT] nothing to update?"
                puts @tokens.to_yaml
                puts request
              end 
            end
          end
        rescue => bang
          puts bang
          puts bang.backtrace if $DEBUG
          # puts @session.to_yaml
        end
      end
      res
    }
  end
  # end
end

#update_tokens(response) ⇒ Object



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
68
# File 'lib/watobo/core/ott_cache.rb', line 33

def update_tokens(response)
  
  begin
    #   site = request.site
    @tokens_lock.synchronize do
      response.each do |line|
        # puts line
        self.class.patterns do |pat|
          puts pat if $DEBUG
          if line =~ /#{pat}/i then
            token_key = Regexp.quote($1.upcase)
            token_value = $2
            #print "U"
              puts "GOT NEW TOKEN (#{token_key}): #{token_value}" if $DEBUG
            #   @session[:valid_csrf_tokens][site] = Hash.new if @session[:valid_csrf_tokens][site].nil?
            #   @session[:valid_csrf_tokens][site][token_key] = token_value
            @tokens[token_key] = token_value
          end
        end

      end
    end
  rescue => bang
    puts bang
    if $DEBUG
    puts bang.backtrace 
    puts "= Request"
    puts request 
    puts "= Response"
    puts response
    puts "==="
    end

  end
  # }
end