Class: Devtunnel::ApiResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/devtunnel/api_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ ApiResponse

Returns a new instance of ApiResponse.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/devtunnel/api_response.rb', line 8

def initialize(response)
	if response.nil? or response.body.nil? or response.code.nil? or response.headers.nil?
		# this response is broken, raise an error.
		raise NilApiResponse.new(500, {}, {"errors" => [{"code" => 990, "message" => "API Server sent back an empty response."}]})
	end

	self._body = ((response.body.is_a? String) ? JSON.parse(response.body) : response.body)
	self._status = response.code
	self._headers = response.headers

	case _status
	when 200, 201
		self._entity = Devtunnel::Entity.new(_body)
	when 400,401,403,404,500
		if _body["errors"]
			self.errors = _body["errors"]
			# quickly look to see if we have authentication errors. We want to raise
			# exceptions on api key errors.
			case errors[0]["code"]
			when "002"
				raise InvalidApiKey.new(_status, _hearders, _body)
			when "007"
				raise ExpiredApiKey.new(_status, _hearders, _body)
			end
		end
	else
		# TODO: test unknown resonse and make sure the client can deal with it. 
		raise UnknownResponse.new(_status, _headers, _body)
	end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

if we’re trying to access a method directly on the ApiResponse, the user is probably trying to get an attribute directly from the single entity that was returned. In this case, we’ll simply look to see if the



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/devtunnel/api_response.rb', line 72

def method_missing(meth, *args, &block)
	return nil unless _entity
	if _entity.object
		if _entity.object.respond_to? meth
			_entity.object.send(meth, *args, &block)
		else
			raise NoMethodError.new("Unknown attribute #{meth} on ApiResponse or #{_entity.object.class} entity.")
		end
	elsif _entity.list
		if _entity.list.respond_to? meth
			_entity.list.send(meth, *args, &block)
		else
			raise NoMethodError.new("Unknown attribute #{meth} on ApiResponse or #{_entity.list.class} list.")
		end
	end
end

Instance Attribute Details

#_bodyObject

Returns the value of attribute _body.



6
7
8
# File 'lib/devtunnel/api_response.rb', line 6

def _body
  @_body
end

#_entityObject

Returns the value of attribute _entity.



6
7
8
# File 'lib/devtunnel/api_response.rb', line 6

def _entity
  @_entity
end

#_headersObject

Returns the value of attribute _headers.



6
7
8
# File 'lib/devtunnel/api_response.rb', line 6

def _headers
  @_headers
end

#_statusObject

Returns the value of attribute _status.



6
7
8
# File 'lib/devtunnel/api_response.rb', line 6

def _status
  @_status
end

#errorsObject

Returns the value of attribute errors.



6
7
8
# File 'lib/devtunnel/api_response.rb', line 6

def errors
  @errors
end

Instance Method Details

#firstObject



43
44
45
46
47
48
49
# File 'lib/devtunnel/api_response.rb', line 43

def first
	if _entity.list
		_entity.list.first
	else
		# TODO: some sort of exception
	end
end

#lengthObject

enumeratable methods and such



40
41
42
# File 'lib/devtunnel/api_response.rb', line 40

def length
	_entity.list.length if _entity.list
end

#limitObject



63
64
65
66
# File 'lib/devtunnel/api_response.rb', line 63

def limit
	return nil unless _entity.meta
	_entity.meta["limit"]
end

#offsetObject



59
60
61
62
# File 'lib/devtunnel/api_response.rb', line 59

def offset
	return nil unless _entity.meta
	_entity.meta["offset"]
end

#success?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/devtunnel/api_response.rb', line 51

def success?
	errors.nil? or errors.empty?
end

#total_resultsObject



55
56
57
58
# File 'lib/devtunnel/api_response.rb', line 55

def total_results 
	return nil unless _entity.meta
	_entity.meta["total_results"]
end