2012-08-23 09:19:40 +00:00
|
|
|
module ApiHelpers
|
2012-08-25 17:31:50 +00:00
|
|
|
# Public: Prepend a request path with the path to the API
|
|
|
|
#
|
|
|
|
# path - Path to append
|
|
|
|
# user - User object - If provided, automatically appends private_token query
|
|
|
|
# string for authenticated requests
|
|
|
|
#
|
|
|
|
# Examples
|
|
|
|
#
|
|
|
|
# >> api('/issues')
|
|
|
|
# => "/api/v2/issues"
|
|
|
|
#
|
|
|
|
# >> api('/issues', User.last)
|
|
|
|
# => "/api/v2/issues?private_token=..."
|
|
|
|
#
|
|
|
|
# >> api('/issues?foo=bar', User.last)
|
|
|
|
# => "/api/v2/issues?foo=bar&private_token=..."
|
|
|
|
#
|
|
|
|
# Returns the relative path to the requested API resource
|
|
|
|
def api(path, user = nil)
|
2013-05-14 12:33:31 +00:00
|
|
|
"/api/#{API::API.version}#{path}" +
|
2012-08-25 17:31:50 +00:00
|
|
|
|
|
|
|
# Normalize query string
|
|
|
|
(path.index('?') ? '' : '?') +
|
|
|
|
|
|
|
|
# Append private_token if given a User object
|
|
|
|
(user.respond_to?(:private_token) ?
|
|
|
|
"&private_token=#{user.private_token}" : "")
|
2012-08-23 09:19:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def json_response
|
2015-06-01 03:03:19 +00:00
|
|
|
@_json_response ||= JSON.parse(response.body)
|
2012-08-23 09:19:40 +00:00
|
|
|
end
|
|
|
|
end
|