Add a simple api method to ApiHelpers, replacing api_prefix

See docs for usage
This commit is contained in:
Robert Speicher 2012-08-25 13:31:50 -04:00
parent fba174e9bc
commit b2a5344a2d
5 changed files with 33 additions and 3 deletions

View file

@ -1,6 +1,8 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::API do describe Gitlab::API do
include ApiHelpers
let(:user) { Factory :user } let(:user) { Factory :user }
let!(:project) { Factory :project, owner: user } let!(:project) { Factory :project, owner: user }
let!(:issue) { Factory :issue, author: user, assignee: user, project: project } let!(:issue) { Factory :issue, author: user, assignee: user, project: project }

View file

@ -1,6 +1,8 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::API do describe Gitlab::API do
include ApiHelpers
let(:user) { Factory :user } let(:user) { Factory :user }
let!(:project) { Factory :project, owner: user } let!(:project) { Factory :project, owner: user }
let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' } let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' }

View file

@ -1,6 +1,8 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::API do describe Gitlab::API do
include ApiHelpers
let(:user) { Factory :user } let(:user) { Factory :user }
describe "GET /users" do describe "GET /users" do

View file

@ -27,7 +27,6 @@ RSpec.configure do |config|
config.mock_with :rspec config.mock_with :rspec
config.include LoginHelpers, type: :request config.include LoginHelpers, type: :request
config.include ApiHelpers, type: :request
# If you're not using ActiveRecord, or you'd prefer not to run each of your # If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false # examples within a transaction, remove the following line or assign false

View file

@ -1,6 +1,31 @@
module ApiHelpers module ApiHelpers
def api_prefix # Public: Prepend a request path with the path to the API
"/api/#{Gitlab::API::VERSION}" #
# 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)
"/api/#{Gitlab::API::VERSION}#{path}" +
# Normalize query string
(path.index('?') ? '' : '?') +
# Append private_token if given a User object
(user.respond_to?(:private_token) ?
"&private_token=#{user.private_token}" : "")
end end
def json_response def json_response