Class: MyData::ResponseParser

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

Instance Method Summary collapse

Constructor Details

#initialize(response, resource: nil, root: nil) ⇒ ResponseParser

Returns a new instance of ResponseParser.



4
5
6
7
8
# File 'lib/my_data/response_parser.rb', line 4

def initialize(response, resource: nil, root: nil)
  @original_response = response
  @resource = resource
  @root = root
end

Instance Method Details

#errorsObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/my_data/response_parser.rb', line 31

def errors
  @errors ||=
    if success?
      []
    elsif response_type?
      response.response.map(&:errors).flatten
    else
      [MyData::Resources::ErrorType.new(JSON.parse(original_response.body))]
    end
end

#responded_atObject



27
28
29
# File 'lib/my_data/response_parser.rb', line 27

def responded_at
  @responded_at ||= Time.parse(original_response.headers["date"])
end

#responseObject



42
43
44
45
46
47
48
49
# File 'lib/my_data/response_parser.rb', line 42

def response
  return @response if defined? @response

  @response =
    if original_response.status == 200
      MyData::XmlParser.xml_to_resource(xml: original_response.body, resource: resource, root: root)
    end
end

#statusObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/my_data/response_parser.rb', line 10

def status
  if success?
    :success
  elsif original_response.status == 401
    :unauthorized
  elsif original_response.status == 400
    :bad_request
  else
    :validation_error
  end
end

#success?Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/my_data/response_parser.rb', line 22

def success?
  original_response.status == 200 &&
    (!response_type? || response.response.all? { |r| r.status_code == "Success" })
end