Class: Nzbmatrix::ApiResponseParser

Inherits:
Object
  • Object
show all
Defined in:
lib/nzbmatrix/api_response_parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(response) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/nzbmatrix/api_response_parser.rb', line 4

def parse(response)
  parsed = response.split(/^\|$/).map do |block|
    parse_block(block)
  end

  parsed.reject do |hash|
    hash.empty?
  end
end

#parse_block(block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/nzbmatrix/api_response_parser.rb', line 14

def parse_block(block)
  block.split("\n").inject({}) do |h, line|
    if (parsed = parse_line(line))
      key, value = parsed
      h[key] = value
    end
    
    h
  end
end

#parse_line(line) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/nzbmatrix/api_response_parser.rb', line 25

def parse_line(line)
  unless line.empty?
    key, value = line.split(":", 2)
    value.gsub!(/;$/, "") unless value.nil?
    [key, value]
  end
end