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.8.0'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, *args) ⇒ ImpURI

class << self



269
270
271
272
273
274
275
276
# File 'lib/ImpURI.rb', line 269

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.



27
28
29
# File 'lib/ImpURI.rb', line 27

def strict
  @strict
end

Class Method Details

.all_but_scheme(uri) ⇒ Object



222
223
224
# File 'lib/ImpURI.rb', line 222

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

.all_but_userinfo(uri) ⇒ Object



226
227
228
229
230
231
232
# File 'lib/ImpURI.rb', line 226

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)


206
207
208
# File 'lib/ImpURI.rb', line 206

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

.has_colon_path_separator?(uri) ⇒ Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/ImpURI.rb', line 186

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

.has_parameters?(uri) ⇒ Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/ImpURI.rb', line 196

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

.has_port_number?(uri) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/ImpURI.rb', line 191

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

.has_scheme?(uri) ⇒ Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/ImpURI.rb', line 181

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

.has_semicolon_parameter_separator?(uri) ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/ImpURI.rb', line 201

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

.has_userinfo?(uri) ⇒ Boolean

Returns:

  • (Boolean)


158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ImpURI.rb', line 158

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)


173
174
175
176
177
178
179
# File 'lib/ImpURI.rb', line 173

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



77
78
79
# File 'lib/ImpURI.rb', line 77

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

.hostname_and_path(uri) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ImpURI.rb', line 136

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



149
150
151
152
153
154
155
# File 'lib/ImpURI.rb', line 149

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



254
255
256
257
258
259
260
# File 'lib/ImpURI.rb', line 254

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)


263
264
265
# File 'lib/ImpURI.rb', line 263

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

.parameter_separator(uri) ⇒ Object



211
212
213
214
215
216
217
218
219
# File 'lib/ImpURI.rb', line 211

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

.parameter_string(uri) ⇒ Object



110
111
112
113
# File 'lib/ImpURI.rb', line 110

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ImpURI.rb', line 116

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



29
30
31
# File 'lib/ImpURI.rb', line 29

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

.password(uri) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/ImpURI.rb', line 68

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

.path(uri) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/ImpURI.rb', line 101

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



82
83
84
85
86
87
88
# File 'lib/ImpURI.rb', line 82

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.



92
93
94
95
96
97
98
99
# File 'lib/ImpURI.rb', line 92

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



33
34
35
36
37
38
39
# File 'lib/ImpURI.rb', line 33

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

.scheme_with_separator(uri) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/ImpURI.rb', line 234

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

.userinfo(uri) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ImpURI.rb', line 42

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



243
244
245
246
247
248
249
250
251
# File 'lib/ImpURI.rb', line 243

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



57
58
59
60
61
62
63
64
65
# File 'lib/ImpURI.rb', line 57

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



426
427
428
429
430
431
432
# File 'lib/ImpURI.rb', line 426

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)


507
508
509
# File 'lib/ImpURI.rb', line 507

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

#has_colon_path_separator?Boolean

Returns:

  • (Boolean)


495
496
497
# File 'lib/ImpURI.rb', line 495

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

#has_hostname?Boolean

Returns:

  • (Boolean)


463
464
465
# File 'lib/ImpURI.rb', line 463

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

#has_parameter_string?Boolean

Returns:

  • (Boolean)


478
479
480
# File 'lib/ImpURI.rb', line 478

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

#has_parameters?Boolean

Returns:

  • (Boolean)


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

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

#has_password?Boolean

Returns:

  • (Boolean)


458
459
460
# File 'lib/ImpURI.rb', line 458

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

#has_path?Boolean

Returns:

  • (Boolean)


473
474
475
# File 'lib/ImpURI.rb', line 473

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

#has_port_number?Boolean

Returns:

  • (Boolean)


468
469
470
# File 'lib/ImpURI.rb', line 468

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

#has_scheme?Boolean

attribute boolean methods

Returns:

  • (Boolean)


448
449
450
# File 'lib/ImpURI.rb', line 448

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

#has_semicolon_parameter_separator?Boolean

Returns:

  • (Boolean)


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

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

#has_userinfo?Boolean

derived attribute boolean methods

Returns:

  • (Boolean)


490
491
492
# File 'lib/ImpURI.rb', line 490

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

#has_username?Boolean

Returns:

  • (Boolean)


453
454
455
# File 'lib/ImpURI.rb', line 453

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

#hostnameObject



313
314
315
# File 'lib/ImpURI.rb', line 313

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

#hostname=(new_hostname) ⇒ Object



318
319
320
321
# File 'lib/ImpURI.rb', line 318

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

#hostname_and_pathObject



410
411
412
413
414
415
416
# File 'lib/ImpURI.rb', line 410

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

#hostname_and_port_numberObject



418
419
420
421
422
423
424
# File 'lib/ImpURI.rb', line 418

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



401
402
403
404
405
406
407
# File 'lib/ImpURI.rb', line 401

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

#is_ssh?Boolean

Returns:

  • (Boolean)


512
513
514
# File 'lib/ImpURI.rb', line 512

def is_ssh?
  ImpURI.is_ssh?(to_s)
end

#parameter_stringObject



344
345
346
# File 'lib/ImpURI.rb', line 344

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

#parameter_string=(new_parameter_string) ⇒ Object



348
349
350
351
352
# File 'lib/ImpURI.rb', line 348

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

#parametersObject



354
355
356
# File 'lib/ImpURI.rb', line 354

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

#parameters=(new_parameters) ⇒ Object



358
359
360
361
362
# File 'lib/ImpURI.rb', line 358

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

#passwordObject



302
303
304
# File 'lib/ImpURI.rb', line 302

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

#password=(new_password) ⇒ Object



307
308
309
310
# File 'lib/ImpURI.rb', line 307

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

#pathObject



335
336
337
# File 'lib/ImpURI.rb', line 335

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

#path=(new_path) ⇒ Object



339
340
341
342
# File 'lib/ImpURI.rb', line 339

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

#port_numberObject



324
325
326
# File 'lib/ImpURI.rb', line 324

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

#port_number=(new_port_number) ⇒ Object



329
330
331
332
# File 'lib/ImpURI.rb', line 329

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.



366
367
368
# File 'lib/ImpURI.rb', line 366

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

#schemeObject

attributes



280
281
282
# File 'lib/ImpURI.rb', line 280

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

#scheme=(new_scheme) ⇒ Object



285
286
287
288
# File 'lib/ImpURI.rb', line 285

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

#scheme_with_separatorObject

derived attributes



372
373
374
375
376
377
378
# File 'lib/ImpURI.rb', line 372

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

#to_hObject



442
443
444
# File 'lib/ImpURI.rb', line 442

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

#to_sObject



434
435
436
437
438
439
440
# File 'lib/ImpURI.rb', line 434

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



381
382
383
384
385
386
387
# File 'lib/ImpURI.rb', line 381

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

#userinfo_with_separatorObject



390
391
392
393
394
395
396
397
398
# File 'lib/ImpURI.rb', line 390

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

#usernameObject



291
292
293
# File 'lib/ImpURI.rb', line 291

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

#username=(new_username) ⇒ Object



296
297
298
299
# File 'lib/ImpURI.rb', line 296

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