Class: Conflict::DiffParser

Inherits:
Object
  • Object
show all
Defined in:
lib/conflict/parsers.rb

Constant Summary collapse

@@key =
"Index: "
@@key_cvs =
'RCS file: '

Instance Method Summary collapse

Constructor Details

#initialize(cfg) ⇒ DiffParser

Returns a new instance of DiffParser.

Raises:



94
95
96
97
98
99
# File 'lib/conflict/parsers.rb', line 94

def initialize cfg
  raise ConflictException::new("cfg cannot be nil") if cfg.nil?
  raise ConflictException::new("ttl value cannot be nil") if cfg[:ttl].nil?
  raise ConflictException::new("ttl cannot be negative") if cfg[:ttl] < 0
  @cfg = cfg
end

Instance Method Details

#parse(diff, client, url, path) ⇒ Object

Raises:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/conflict/parsers.rb', line 101

def parse diff, client, url, path 

  # would be nice to just get a 3rd party for this


  raise ConflictException::new("client cannot by empty") if client.nil? || client.empty?
  raise ConflictException::new("diff cannot by empty") if diff.nil? || diff.empty?
  raise ConflictException::new("url value cannot be nil or empty") if url.nil? or url.empty?
  raise ConflictException::new("path value cannot be nil or empty") if path.nil? or path.empty?

  events = []
  resource = ""
  lines = diff.split(%r{\n})
  #Conflict.logger.info("received diff w/ " + lines.size.to_s + " line(s)")

  bit = true
  now = Time.now # use one var, so all events for a request get same creation time

  lines.each do |line|
    if bit
      if line.to_s.index(@@key) == 0
        resource = line.to_s.sub(@@key, "") 
        bit = ! bit
      end
    else
      if line.to_s.index("@@ ") == 0
        action = infer_action line
        resource_full = infer_resource(url.to_s, path, resource)
        events << Event::new(client, action, resource_full, now, @cfg[:ttl])
        bit = ! bit
      end
    end
  end
  
  events
end

#parse_cvs(diff, client) ⇒ Object

Raises:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/conflict/parsers.rb', line 135

def parse_cvs diff, client
  
  raise ConflictException::new("client cannot by empty") if client.nil? || client.empty?
  raise ConflictException::new("diff cannot by empty") if diff.nil? || diff.empty?    

  events = []

  lines = diff.split(%r{\n})
  now = Time.now

  lines.each do |line|

    if line.to_s.index(@@key_cvs) == 0
      resource = line.to_s.sub(@@key_cvs, '')
      events << Event::new(client, 'changed', resource.slice!(0..(resource.index(",") - 1)), now, @cfg[:ttl])
    end

  end

  events 

end