Class: SimpleHelper::ResponseParser

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_helper/response_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, format, path) ⇒ ResponseParser

Returns a new instance of ResponseParser.



11
12
13
14
15
# File 'lib/simple_helper/response_parser.rb', line 11

def initialize(body, format, path)
  @body = body
  @format = format
  @path = path
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



9
10
11
# File 'lib/simple_helper/response_parser.rb', line 9

def body
  @body
end

#formatObject (readonly)

Returns the value of attribute format.



9
10
11
# File 'lib/simple_helper/response_parser.rb', line 9

def format
  @format
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/simple_helper/response_parser.rb', line 9

def path
  @path
end

Class Method Details

.perform(body, format, path) ⇒ Object



17
18
19
# File 'lib/simple_helper/response_parser.rb', line 17

def self.perform(body, format, path)
  new(body, format, path).parse
end

Instance Method Details

#parseObject



21
22
23
24
25
26
27
28
# File 'lib/simple_helper/response_parser.rb', line 21

def parse
  if body.nil? || body == 'null' || (body.valid_encoding? && body.strip.empty?)
    return log(message: 'Invalid response')
  end

  @body = body.gsub(/\A#{UTF8_BOM}/, '') if body.valid_encoding? && body.encoding == Encoding::UTF_8
  send(format)
end