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.



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

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)



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

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



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

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)



30
31
32
# File 'lib/gitlab/triage/url_builders/url_builder.rb', line 30

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

#params_stringObject (private)



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

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)



48
49
50
# File 'lib/gitlab/triage/url_builders/url_builder.rb', line 48

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