Class: Stretchr::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/stretchr/response.rb

Overview

Response parser for stretchr

Parameters

response - The raw response from stretchr options[:api_version] - The version of the api we’re talking to, tells us how to parse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, options = {}) ⇒ Response

Returns a new instance of Response.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/stretchr/response.rb', line 9

def initialize(response, options = {})
	@api_version = options[:api_version] || Stretchr.config["api_version"] #api version is used to determine how to parse response
	@raw = response
	@parsed = JSON.parse(response)
	begin
		@data = @parsed[Stretchr.config[@api_version]["data"]] #stretchr data object
		@errors = @parsed[Stretchr.config[@api_version]["errors"]] #errors
		@changes = @parsed[Stretchr.config[@api_version]["changes"]] #changes (POST/PUT/PATCH/DELETE)
		@status = @parsed[Stretchr.config[@api_version]["status"]] #request status
		if @data
			@items = @data[Stretchr.config[@api_version]["items"]]
		end
	rescue
	end
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



8
9
10
# File 'lib/stretchr/response.rb', line 8

def api_version
  @api_version
end

#changesObject (readonly)

Returns the value of attribute changes.



8
9
10
# File 'lib/stretchr/response.rb', line 8

def changes
  @changes
end

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/stretchr/response.rb', line 8

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/stretchr/response.rb', line 8

def errors
  @errors
end

#itemsObject (readonly)

Returns the value of attribute items.



8
9
10
# File 'lib/stretchr/response.rb', line 8

def items
  @items
end

#parsedObject (readonly)

Returns the value of attribute parsed.



8
9
10
# File 'lib/stretchr/response.rb', line 8

def parsed
  @parsed
end

#rawObject (readonly)

Returns the value of attribute raw.



8
9
10
# File 'lib/stretchr/response.rb', line 8

def raw
  @raw
end

#statusObject (readonly)

Returns the value of attribute status.



8
9
10
# File 'lib/stretchr/response.rb', line 8

def status
  @status
end

Instance Method Details

#success?Boolean

Returns whether or not the request was successful

Returns:

  • (Boolean)


26
27
28
# File 'lib/stretchr/response.rb', line 26

def success?
	@status >= 200 && @status <= 299
end