Class: ImpURI

Inherits:
Object show all
Defined in:
lib/ImpURI/VERSION.rb,
lib/impuri.rb

Overview

ImpURI/VERSION.rb ImpURI::VERSION

Defined Under Namespace

Classes: ColonPathSeparatorsNotAllowedError, SchemeMissingError

Constant Summary collapse

VERSION =
'0.11.0'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, *args) ⇒ ImpURI

class << self



262
263
264
265
266
267
268
269
# File 'lib/impuri.rb', line 262

def initialize(uri, *args)
  options = args.extract_options!
  if options[:strict]
    raise SchemeMissingError if !ImpURI.has_scheme?(uri)
    raise ColonPathSeparatorsNotAllowedError if ImpURI.has_colon_path_separator?(uri)
  end
  @uri = uri
end

Class Attribute Details

.strictObject

Returns the value of attribute strict.



20
21
22
# File 'lib/impuri.rb', line 20

def strict
  @strict
end

Class Method Details

.all_but_scheme(uri) ⇒ Object



215
216
217
# File 'lib/impuri.rb', line 215

def all_but_scheme(uri)
  uri.split('://').last
end

.all_but_userinfo(uri) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/impuri.rb', line 219

def all_but_userinfo(uri)
  if has_userinfo?(uri)
    "#{scheme_with_separator(uri)}#{hostname_and_path(uri)}"
  else
    hostname_and_path(uri)
  end
end

.has_ampersand_parameter_separator?(uri) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/impuri.rb', line 199

def has_ampersand_parameter_separator?(uri)
  uri.match(/&/) ? true : false
end

.has_colon_path_separator?(uri) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/impuri.rb', line 179

def has_colon_path_separator?(uri)
  hostname_and_path(uri).match(/:/) && !has_port_number?(uri) ? true : false
end

.has_parameters?(uri) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/impuri.rb', line 189

def has_parameters?(uri)
  uri.match(/\?/) ? true : false
end

.has_port_number?(uri) ⇒ Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/impuri.rb', line 184

def has_port_number?(uri)
  hostname_and_path(uri).match(/:\d+/) ? true : false
end

.has_scheme?(uri) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/impuri.rb', line 174

def has_scheme?(uri)
  uri.match(/^[a-z]*?:\/\//) ? true : false
end

.has_semicolon_parameter_separator?(uri) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/impuri.rb', line 194

def has_semicolon_parameter_separator?(uri)
  uri.match(/;/) ? true : false
end

.has_userinfo?(uri) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/impuri.rb', line 151

def has_userinfo?(uri)
  if has_scheme?(uri)
    partial_decomposition = uri.split('//').all_but_first.join.split('/')
    if partial_decomposition.size > 1
      next_decomposition = partial_decomposition.all_but_last
    else
      next_decomposition = [partial_decomposition.first]
    end
    next_decomposition.join('/').match(/@/) ? true : false
  else
    uri.match(/.+:.+@/) ? true : false
  end
end

.has_username?(uri) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
169
170
171
172
# File 'lib/impuri.rb', line 166

def has_username?(uri)
  if has_scheme?(uri)
    all_but_scheme(uri).scan('@').first ? true : false
  else
    uri.scan('@').first ? true : false
  end
end

.hostname(uri) ⇒ Object



70
71
72
# File 'lib/impuri.rb', line 70

def hostname(uri)
  hostname_and_port_number(uri).split(':').first
end

.hostname_and_path(uri) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/impuri.rb', line 129

def hostname_and_path(uri)
  if has_userinfo?(uri) || has_username?(uri)
    if all_but_scheme(uri).split('@').size > 1
      all_but_scheme(uri).split('@').all_but_first.join('@')
    else
      all_but_scheme(uri).split('@').first
    end
  else
    all_but_scheme(uri)
  end
end

.hostname_and_port_number(uri) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/impuri.rb', line 142

def hostname_and_port_number(uri)
  if has_colon_path_separator?(uri)
    hostname_and_path(uri).split(':').all_but_last.join(':')
  else
    hostname_and_path(uri).split('/').first
  end
end

.hostname_optionally_with_port_number(uri) ⇒ Object



247
248
249
250
251
252
253
# File 'lib/impuri.rb', line 247

def hostname_optionally_with_port_number(uri)
  if port_number(uri).blank?
    "#{hostname(uri)}"
  else
    "#{hostname(uri)}:#{port_number(uri)}"
  end
end

.is_ssh?(uri) ⇒ Boolean

Returns:

  • (Boolean)


256
257
258
# File 'lib/impuri.rb', line 256

def is_ssh?(uri)
  !has_scheme?(uri) && has_colon_path_separator?(uri) ? true : false
end

.parameter_separator(uri) ⇒ Object



204
205
206
207
208
209
210
211
212
# File 'lib/impuri.rb', line 204

def parameter_separator(uri)
  if has_ampersand_parameter_separator?(uri)
    '&'
  elsif has_semicolon_parameter_separator?(uri)
    ';'
  else
    nil
  end
end

.parameter_string(uri) ⇒ Object



103
104
105
106
# File 'lib/impuri.rb', line 103

def parameter_string(uri)
  parameter_string = has_parameters?(uri) ? request_uri(uri).split('?').last : nil
  parameter_string.blank? ? nil : parameter_string
end

.parameters(uri) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/impuri.rb', line 109

def parameters(uri)
  if non_nil_parameter_string = parameter_string(uri)
    parameter_parts = (
      case parameter_separator(uri)
      when '&'; non_nil_parameter_string.split('&')
      when ';'; non_nil_parameter_string.split(';')
      else [non_nil_parameter_string]
      end
    )
    parameter_parts.inject({}) do |h,pairs|
      a = pairs.split('=')
      h[a[0]] = a[1]
      h
    end
  else
    nil
  end
end

.parse(uri, *args) ⇒ Object



22
23
24
# File 'lib/impuri.rb', line 22

def parse(uri, *args)
  ImpURI.new(uri, *args)
end

.password(uri) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/impuri.rb', line 61

def password(uri)
  if has_userinfo?(uri)
    userinfo(uri).split(':')[1]
  else
    nil
  end
end

.path(uri) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/impuri.rb', line 94

def path(uri)
  if non_nil_request_uri = request_uri(uri)
    request_uri = non_nil_request_uri.split('?').first
    request_uri.blank? ? nil : request_uri
  else
    nil
  end
end

.port_number(uri) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/impuri.rb', line 75

def port_number(uri)
  if has_port_number?(uri)
    hostname_and_port_number(uri).split(':').last
  else
    nil
  end
end

.request_uri(uri) ⇒ Object

For the instance method providing compatibility to Ruby's URI.



85
86
87
88
89
90
91
92
# File 'lib/impuri.rb', line 85

def request_uri(uri)
  if has_colon_path_separator?(uri)
    path = hostname_and_path(uri).split(':').all_but_first.join('/')
  else
    path = hostname_and_path(uri).split('/').all_but_first.join('/')
    path.blank? ? nil : '/' + path
  end
end

.scheme(uri) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/impuri.rb', line 26

def scheme(uri)
  if has_scheme?(uri)
    uri.split('://').first
  else
    nil
  end
end

.scheme_with_separator(uri) ⇒ Object



227
228
229
230
231
232
233
# File 'lib/impuri.rb', line 227

def scheme_with_separator(uri)
  if scheme(uri).blank?
    ''
  else
    "#{scheme(uri)}://"
  end
end

.userinfo(uri) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/impuri.rb', line 35

def userinfo(uri)
  if has_userinfo?(uri)
    if all_but_scheme(uri).split('@').size > 1
      all_but_scheme(uri).split('@').all_but_last.join('@')
    else
      all_but_scheme(uri).split('@').first
    end
  elsif has_username?(uri)
    uri.split('@').first
  else
    nil
  end
end

.userinfo_with_separator(uri) ⇒ Object



236
237
238
239
240
241
242
243
244
# File 'lib/impuri.rb', line 236

def userinfo_with_separator(uri)
  if username(uri).blank?
    ''
  elsif password(uri).blank?
    "#{username(uri)}@"
  else
    "#{username(uri)}:#{password(uri)}@"
  end
end

.username(uri) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/impuri.rb', line 50

def username(uri)
  if has_userinfo?(uri)
    userinfo(uri).split(':')[0]
  elsif has_username?(uri)
    uri.split('@').first
  else
    nil
  end
end

Instance Method Details

#all_but_schemeObject



419
420
421
422
423
424
425
# File 'lib/impuri.rb', line 419

def all_but_scheme
  if has_colon_path_separator?
    "#{userinfo_with_separator}#{hostname_optionally_with_port_number}:#{path}"
  else
    "#{userinfo_with_separator}#{hostname_optionally_with_port_number}#{path}"
  end
end

#has_ampersand_parameter_separator?Boolean

Returns:

  • (Boolean)


500
501
502
# File 'lib/impuri.rb', line 500

def has_ampersand_parameter_separator?
  ImpURI.has_ampersand_parameter_separator?(@uri)
end

#has_colon_path_separator?Boolean

Returns:

  • (Boolean)


488
489
490
# File 'lib/impuri.rb', line 488

def has_colon_path_separator?
  ImpURI.has_colon_path_separator?(@uri)
end

#has_hostname?Boolean

Returns:

  • (Boolean)


456
457
458
# File 'lib/impuri.rb', line 456

def has_hostname?
  hostname.nil? ? false : true
end

#has_parameter_string?Boolean

Returns:

  • (Boolean)


471
472
473
# File 'lib/impuri.rb', line 471

def has_parameter_string?
  parameter_string.nil? ? false : true
end

#has_parameters?Boolean

Returns:

  • (Boolean)


476
477
478
# File 'lib/impuri.rb', line 476

def has_parameters?
  parameters.nil? ? false : true
end

#has_password?Boolean

Returns:

  • (Boolean)


451
452
453
# File 'lib/impuri.rb', line 451

def has_password?
  password.nil? ? false : true
end

#has_path?Boolean

Returns:

  • (Boolean)


466
467
468
# File 'lib/impuri.rb', line 466

def has_path?
  path.nil? ? false : true
end

#has_port_number?Boolean

Returns:

  • (Boolean)


461
462
463
# File 'lib/impuri.rb', line 461

def has_port_number?
  port_number.nil? ? false : true
end

#has_scheme?Boolean

attribute boolean methods

Returns:

  • (Boolean)


441
442
443
# File 'lib/impuri.rb', line 441

def has_scheme?
  scheme.nil? ? false : true
end

#has_semicolon_parameter_separator?Boolean

Returns:

  • (Boolean)


493
494
495
# File 'lib/impuri.rb', line 493

def has_semicolon_parameter_separator?
  ImpURI.has_semicolon_parameter_separator?(@uri)
end

#has_userinfo?Boolean

derived attribute boolean methods

Returns:

  • (Boolean)


483
484
485
# File 'lib/impuri.rb', line 483

def has_userinfo?
  ImpURI.has_userinfo?(@uri)
end

#has_username?Boolean

Returns:

  • (Boolean)


446
447
448
# File 'lib/impuri.rb', line 446

def has_username?
  username.nil? ? false : true
end

#hostnameObject



306
307
308
# File 'lib/impuri.rb', line 306

def hostname
  @hostname ||= ImpURI.hostname(@uri)
end

#hostname=(new_hostname) ⇒ Object



311
312
313
314
# File 'lib/impuri.rb', line 311

def hostname=(new_hostname)
  @hostname = new_hostname
  @uri = to_s
end

#hostname_and_pathObject



403
404
405
406
407
408
409
# File 'lib/impuri.rb', line 403

def hostname_and_path
  if has_colon_path_separator?
    "#{hostname}:#{path}"
  else
    "#{hostname}#{path}"
  end
end

#hostname_and_port_numberObject



411
412
413
414
415
416
417
# File 'lib/impuri.rb', line 411

def hostname_and_port_number
  if has_colon_path_separator?
    hostname_and_path.split(':').all_but_last.join(':')
  else
    hostname_and_path.split('/').first
  end
end

#hostname_optionally_with_port_numberObject



394
395
396
397
398
399
400
# File 'lib/impuri.rb', line 394

def hostname_optionally_with_port_number
  if has_port_number?
    "#{hostname}:#{port_number}"
  else
    "#{hostname}"
  end
end

#is_ssh?Boolean

Returns:

  • (Boolean)


505
506
507
# File 'lib/impuri.rb', line 505

def is_ssh?
  ImpURI.is_ssh?(to_s)
end

#parameter_stringObject



337
338
339
# File 'lib/impuri.rb', line 337

def parameter_string
  @parameter_string ||= ImpURI.parameter_string(@uri)
end

#parameter_string=(new_parameter_string) ⇒ Object



341
342
343
344
345
# File 'lib/impuri.rb', line 341

def parameter_string=(new_parameter_string)
  @parameter_string = new_parameter_string
  @uri = to_s
  @parameters = ImpURI.parameters(@uri)
end

#parametersObject



347
348
349
# File 'lib/impuri.rb', line 347

def parameters
  @parameters ||= ImpURI.parameters(@uri)
end

#parameters=(new_parameters) ⇒ Object



351
352
353
354
355
# File 'lib/impuri.rb', line 351

def parameters=(new_parameters)
  @parameters = new_parameters
  @parameter_string = @parameters.x_www_form_urlencode
  @uri = to_s
end

#passwordObject



295
296
297
# File 'lib/impuri.rb', line 295

def password
  @password ||= ImpURI.password(@uri)
end

#password=(new_password) ⇒ Object



300
301
302
303
# File 'lib/impuri.rb', line 300

def password=(new_password)
  @password = new_password
  @uri = to_s
end

#pathObject



328
329
330
# File 'lib/impuri.rb', line 328

def path
  @path ||= ImpURI.path(@uri)
end

#path=(new_path) ⇒ Object



332
333
334
335
# File 'lib/impuri.rb', line 332

def path=(new_path)
  @path = new_path
  @uri = to_s
end

#port_numberObject



317
318
319
# File 'lib/impuri.rb', line 317

def port_number
  @port_number ||= ImpURI.port_number(@uri)
end

#port_number=(new_port_number) ⇒ Object



322
323
324
325
# File 'lib/impuri.rb', line 322

def port_number=(new_port_number)
  @port_number = new_port_number
  @uri = to_s
end

#request_uriObject

For compatability with Ruby's URI. Not quite a drop-in replacement for most aspects as yet however. Perhaps it should be in a compatability module and included.



359
360
361
# File 'lib/impuri.rb', line 359

def request_uri
  @request_uri ||= ImpURI.request_uri(@uri)
end

#schemeObject

attributes



273
274
275
# File 'lib/impuri.rb', line 273

def scheme
  @scheme ||= ImpURI.scheme(@uri)
end

#scheme=(new_scheme) ⇒ Object



278
279
280
281
# File 'lib/impuri.rb', line 278

def scheme=(new_scheme)
  @scheme = new_scheme
  @uri = to_s
end

#scheme_with_separatorObject

derived attributes



365
366
367
368
369
370
371
# File 'lib/impuri.rb', line 365

def scheme_with_separator
  if scheme.blank?
    ''
  else
    "#{scheme}://"
  end
end

#to_hObject



435
436
437
# File 'lib/impuri.rb', line 435

def to_h
  {scheme: scheme, username: username, password: password, hostname: hostname, port_number: port_number, path: path, parameter_string: parameter_string}
end

#to_sObject



427
428
429
430
431
432
433
# File 'lib/impuri.rb', line 427

def to_s
  if has_parameter_string?
    "#{scheme_with_separator}#{all_but_scheme}?#{parameter_string}"
  else
    "#{scheme_with_separator}#{all_but_scheme}"
  end
end

#userinfoObject



374
375
376
377
378
379
380
# File 'lib/impuri.rb', line 374

def userinfo
  if password
    "#{username}:#{password}"
  else
    "#{username}"
  end
end

#userinfo_with_separatorObject



383
384
385
386
387
388
389
390
391
# File 'lib/impuri.rb', line 383

def userinfo_with_separator
  if username.blank?
    ''
  elsif password.blank?
    "#{username}@"
  else
    "#{username}:#{password}@"
  end
end

#usernameObject



284
285
286
# File 'lib/impuri.rb', line 284

def username
  @username ||= ImpURI.username(@uri)
end

#username=(new_username) ⇒ Object



289
290
291
292
# File 'lib/impuri.rb', line 289

def username=(new_username)
  @username = new_username
  @uri = to_s
end