Class: Gitlab::Triage::UrlBuilders::UrlBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/triage/url_builders/url_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ UrlBuilder

Returns a new instance of UrlBuilder.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/gitlab/triage/url_builders/url_builder.rb', line 7

def initialize(options)
  @network_options = options.fetch(:network_options)
  @host_url = @network_options.host_url
  @api_version = @network_options.api_version
  @all = options.fetch(:all, false)
  @source = options.fetch(:source, 'projects')
  @source_id = options.fetch(:source_id)
  @resource_type = options.fetch(:resource_type, nil)
  @sub_resource_type = options.fetch(:sub_resource_type, nil)
  @resource_id = options.fetch(:resource_id, nil)
  @params = options.fetch(:params, [])

  @params = @params.merge(scope: :all) if @all
end

Instance Method Details

#base_urlObject (private)



36
37
38
39
40
41
42
# File 'lib/gitlab/triage/url_builders/url_builder.rb', line 36

def base_url
  url = host_with_api_url
  url << "/#{@source}/#{percent_encode(@source_id.to_s)}" unless @all
  url << "/repository" if @resource_type == 'branches'
  url << "/#{@resource_type}" if @resource_type
  url
end

#buildObject



22
23
24
25
26
27
28
# File 'lib/gitlab/triage/url_builders/url_builder.rb', line 22

def build
  url = base_url
  url << "/#{percent_encode(@resource_id.to_s)}" if @resource_id
  url << "/#{@sub_resource_type}" if @sub_resource_type
  url << params_string if @params
  url
end

#host_with_api_urlObject (private)



32
33
34
# File 'lib/gitlab/triage/url_builders/url_builder.rb', line 32

def host_with_api_url
  "#{@host_url}/api/#{@api_version}"
end

#params_stringObject (private)



44
45
46
47
48
# File 'lib/gitlab/triage/url_builders/url_builder.rb', line 44

def params_string
  "?" << @params.map do |k, v|
    "#{percent_encode(k.to_s)}=#{percent_encode(v.to_s)}"
  end.join("&")
end

#percent_encode(str) ⇒ Object (private)



50
51
52
# File 'lib/gitlab/triage/url_builders/url_builder.rb', line 50

def percent_encode(str)
  CGI.escape(str).gsub('+', '%20')
end