Class: LedgerSync::Util::URLHelpers

Inherits:
Object
  • Object
show all
Defined in:
lib/ledger_sync/util/url_helpers.rb

Class Method Summary collapse

Class Method Details

.id_from_url(args = {}) ⇒ Object



31
32
33
34
35
# File 'lib/ledger_sync/util/url_helpers.rb', line 31

def self.id_from_url(args = {})
  uri = args.fetch(:url)

  URI.parse(uri).path.split('/').last
end

.merge_params_in_path(args = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ledger_sync/util/url_helpers.rb', line 18

def self.merge_params_in_path(args = {})
  params = args.fetch(:params)
  path   = args.fetch(:path)

  root, str_params = path.split('?')
  str_params = Rack::Utils.parse_nested_query(str_params)

  [
    root,
    Rack::Utils.build_query(str_params.merge(HashHelpers.deep_stringify_keys(params)))
  ].compact.join('?')
end

.merge_params_in_url(args = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ledger_sync/util/url_helpers.rb', line 6

def self.merge_params_in_url(args = {})
  params = args.fetch(:params)
  uri    = args.fetch(:url)

  uri = URI.parse(uri) unless uri.is_a?(URI::HTTPS) || uri.is_a?(URI::HTTP)
  uri.query = URI.encode_www_form(
    params.inject(URI.decode_www_form(String(uri.query))) { |prev, param| prev << param }
  )

  uri.to_s
end