summaryrefslogtreecommitdiff
path: root/qa/qa/support/api_helpers.rb
blob: ccabf7fe8098decab58486a9053093c806102c84 (plain)
1
2
3
4
5
6
7
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
38
39
40
41
42
43
44
require 'airborne'

module QA
  module Support
    module ApiHelpers
      API_VERSION = 'v4'.freeze

      # 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, version: API_VERSION, personal_access_token: nil, oauth_access_token: nil)
        full_path = "/api/#{version}#{path}"

        if oauth_access_token
          query_string = "access_token=#{oauth_access_token}"
        elsif personal_access_token
          query_string = "private_token=#{personal_access_token}"
        end

        if query_string
          full_path << (path.index('?') ? '&' : '?')
          full_path << query_string
        end

        full_path
      end
    end
  end
end