furi 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d7f6ef143c746b142b2ff9e1a87660e81d050667
4
- data.tar.gz: 365aa57947465c284cdfa26059d1ed8df526c14b
3
+ metadata.gz: 208063d16947ce85a4d80a12bd3834cb26c0f6ab
4
+ data.tar.gz: ab3eff6b787f0df3e04781908477bb69d3924333
5
5
  SHA512:
6
- metadata.gz: f2f8320051bfe74de4a4e396d20bd1aa464aa6e14fd7a02595b8145e0294e19759d7e829003d08c7c5213c685cd289cb508dacbb74101f45515bd533631e6064
7
- data.tar.gz: 0c803b1a0de4e96a55be9ccdeac092f868eea3a56058144b29224b6b10d2538902ba0d9910da1f5eee2bb55d7693dd301526f986f9820e5d1533626fb1a59245
6
+ metadata.gz: 500c3e4bb3ed0866c0cef1ca68794d9d82e9d2b679bb1590f69e14428bb1020a64d5c357c166e25b0c522902b670e5138269e649f395455f48acc4bca50785eb
7
+ data.tar.gz: bae87908fe1a04c26d1073d569199adda1645bc25f561ec0aa9b2e05943e55d0993223c3d329335c07afcac8c814e87c2a738573a2837be2996cb38339e3078f
@@ -42,8 +42,12 @@ module Furi
42
42
  Expressions.new
43
43
  end
44
44
 
45
- def self.parse(string)
46
- Uri.new(string)
45
+ def self.parse(argument)
46
+ Uri.new(argument)
47
+ end
48
+
49
+ def self.build(argument)
50
+ Uri.new(argument).to_s
47
51
  end
48
52
 
49
53
  class << self
@@ -201,26 +205,20 @@ module Furi
201
205
  define_method(aliaz) do
202
206
  send(origin)
203
207
  end
204
- end
205
- end
206
208
 
207
- def initialize(string)
208
- string, *@anchor = string.split("#")
209
- @anchor = @anchor.empty? ? nil : @anchor.join("#")
210
- if string.include?("?")
211
- string, query_string = string.split("?", 2)
212
- @query_string = query_string
209
+ define_method(:"#{aliaz}=") do |*args|
210
+ send(:"#{origin}=", *args)
211
+ end
213
212
  end
213
+ end
214
214
 
215
- if string.include?("://")
216
- @protocol, string = string.split(":", 2)
217
- @protocol = '' if @protocol.empty?
218
- end
219
- if string.start_with?("//")
220
- @protocol ||= ''
221
- string = string[2..-1]
215
+ def initialize(argument)
216
+ case argument
217
+ when String
218
+ parse_uri_string(argument)
219
+ when Hash
220
+ update(argument)
222
221
  end
223
- parse_authority(string)
224
222
  end
225
223
 
226
224
  def update(parts)
@@ -256,6 +254,15 @@ module Furi
256
254
  end
257
255
  end
258
256
 
257
+ def host=(string)
258
+ @port = nil
259
+ if string.include?(":")
260
+ string, @port = string.split(":", 2)
261
+ @port = @port.to_i
262
+ end
263
+ @hostname = string.empty? ? nil : string
264
+ end
265
+
259
266
  def to_s
260
267
  result = []
261
268
  if protocol
@@ -275,27 +282,6 @@ module Furi
275
282
  result.join
276
283
  end
277
284
 
278
- def parse_authority(string)
279
- if string.include?("/")
280
- string, @path = string.split("/", 2)
281
- @path = "/" + @path
282
- end
283
-
284
- if string.include?("@")
285
- userinfo, string = string.split("@", 2)
286
- @username, @password = userinfo.split(":", 2)
287
- end
288
- if string.include?(":")
289
- string, @port = string.split(":", 2)
290
- @port = @port.to_i
291
- end
292
- if string.empty?
293
- @hostname = nil
294
- else
295
- @hostname = string
296
- end
297
- end
298
-
299
285
  def query
300
286
  return @query if query_level?
301
287
  @query = Furi.parse_nested_query(@query_string)
@@ -358,23 +344,48 @@ module Furi
358
344
  protocol ? PORT_MAPPING[protocol] : nil
359
345
  end
360
346
 
361
- def default_port?
362
- default_port && (!port || port == default_port)
363
- end
347
+ protected
364
348
 
365
- def custom_port?
366
- !default_port
349
+ def query_level?
350
+ !!@query
367
351
  end
368
352
 
369
353
  def explicit_port
370
354
  port == default_port ? nil : port
371
355
  end
372
356
 
373
- protected
357
+ def parse_uri_string(string)
358
+ string, *@anchor = string.split("#")
359
+ @anchor = @anchor.empty? ? nil : @anchor.join("#")
360
+ if string.include?("?")
361
+ string, query_string = string.split("?", 2)
362
+ @query_string = query_string
363
+ end
374
364
 
375
- def query_level?
376
- !!@query
365
+ if string.include?("://")
366
+ @protocol, string = string.split(":", 2)
367
+ @protocol = '' if @protocol.empty?
368
+ end
369
+ if string.start_with?("//")
370
+ @protocol ||= ''
371
+ string = string[2..-1]
372
+ end
373
+ parse_authority(string)
374
+ end
375
+
376
+ def parse_authority(string)
377
+ if string.include?("/")
378
+ string, @path = string.split("/", 2)
379
+ @path = "/" + @path
380
+ end
381
+
382
+ if string.include?("@")
383
+ userinfo, string = string.split("@", 2)
384
+ @username, @password = userinfo.split(":", 2)
385
+ end
386
+ self.host = string
377
387
  end
388
+
378
389
  end
379
390
 
380
391
  class FormattingError < StandardError
@@ -1,3 +1,3 @@
1
1
  module Furi
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -180,6 +180,16 @@ describe Furi do
180
180
 
181
181
  end
182
182
 
183
+ describe ".build" do
184
+ it "should work correctly" do
185
+ expect(Furi.build(hostname: 'hello.com')).to eq('hello.com')
186
+ expect(Furi.build(hostname: 'hello.com', port: 88)).to eq('hello.com:88')
187
+ expect(Furi.build(hostname: 'hello.com', port: 88)).to eq('hello.com:88')
188
+ expect(Furi.build(schema: 'https', hostname: 'hello.com', port: 88)).to eq('https://hello.com:88')
189
+ expect(Furi.build(schema: 'http', hostname: 'hello.com', port: 80)).to eq('http://hello.com')
190
+ end
191
+ end
192
+
183
193
 
184
194
  describe "serialize" do
185
195
  it "should work" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: furi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bogdan Gusiev