Class: Parsers

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

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Parsers

Returns a new instance of Parsers.



7
8
9
10
11
12
# File 'lib/dagger/parsers.rb', line 7

def initialize(response)
  if type = response.content_type
    @normalized_type = type.split(';').first.gsub(/[^a-z]/, '_')
    @body = response.body
  end
end

Instance Method Details

#application_json(body) ⇒ Object Also known as: text_javascript, application_x_javascript



18
19
20
21
22
# File 'lib/dagger/parsers.rb', line 18

def application_json(body)
  Oj.load(body)
rescue Oj::ParseError
  nil
end

#processObject



14
15
16
# File 'lib/dagger/parsers.rb', line 14

def process
  send(@normalized_type, @body) if @normalized_type && respond_to?(@normalized_type)
end

#text_xml(body) ⇒ Object Also known as: application_xml



27
28
29
30
31
32
33
# File 'lib/dagger/parsers.rb', line 27

def text_xml(body)
  if res = Ox.parse(body)
    res.to_node
  end
rescue Ox::ParseError
  nil
end