addressable 2.2.0 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ === Addressable 2.2.1
2
+ - added support for application/x-www-form-urlencoded.
3
+
1
4
  === Addressable 2.2.0
2
5
  - added site methods
3
6
  - improved documentation
@@ -560,6 +560,81 @@ module Addressable
560
560
  end
561
561
  end
562
562
 
563
+ ##
564
+ # Encodes a set of key/value pairs according to the rules for the
565
+ # <code>application/x-www-form-urlencoded</code> MIME type.
566
+ #
567
+ # @param [#to_hash, #to_ary] form_values
568
+ # The form values to encode.
569
+ #
570
+ # @param [TrueClass, FalseClass] sort
571
+ # Sort the key/value pairs prior to encoding.
572
+ # Defaults to <code>false</code>.
573
+ #
574
+ # @return [String]
575
+ # The encoded value.
576
+ def self.form_encode(form_values, sort=false)
577
+ if form_values.respond_to?(:to_hash)
578
+ form_values = form_values.to_hash.to_a
579
+ elsif form_values.respond_to?(:to_ary)
580
+ form_values = form_values.to_ary
581
+ else
582
+ raise TypeError, "Can't convert #{form_values.class} into Array."
583
+ end
584
+ form_values = form_values.map do |(key, value)|
585
+ [key.to_s, value.to_s]
586
+ end
587
+ if sort
588
+ # Useful for OAuth and optimizing caching systems
589
+ form_values = form_values.sort
590
+ end
591
+ escaped_form_values = form_values.map do |(key, value)|
592
+ # Line breaks are CRLF pairs
593
+ [
594
+ self.encode_component(
595
+ key.gsub(/(\r\n|\n|\r)/, "\r\n"),
596
+ CharacterClasses::UNRESERVED
597
+ ).gsub("%20", "+"),
598
+ self.encode_component(
599
+ value.gsub(/(\r\n|\n|\r)/, "\r\n"),
600
+ CharacterClasses::UNRESERVED
601
+ ).gsub("%20", "+")
602
+ ]
603
+ end
604
+ return (escaped_form_values.map do |(key, value)|
605
+ "#{key}=#{value}"
606
+ end).join("&")
607
+ end
608
+
609
+ ##
610
+ # Decodes a <code>String</code> according to the rules for the
611
+ # <code>application/x-www-form-urlencoded</code> MIME type.
612
+ #
613
+ # @param [String, #to_str] encoded_value
614
+ # The form values to decode.
615
+ #
616
+ # @return [Array]
617
+ # The decoded values.
618
+ # This is not a <code>Hash</code> because of the possibility for
619
+ # duplicate keys.
620
+ def self.form_unencode(encoded_value)
621
+ if !encoded_value.respond_to?(:to_str)
622
+ raise TypeError, "Can't convert #{encoded_value.class} into String."
623
+ end
624
+ encoded_value = encoded_value.to_str
625
+ split_values = encoded_value.split("&").map do |pair|
626
+ pair.split("=", 2)
627
+ end
628
+ return split_values.map do |(key, value)|
629
+ [
630
+ key ? self.unencode_component(
631
+ key.gsub("+", "%20")).gsub(/(\r\n|\n|\r)/, "\n") : nil,
632
+ value ? (self.unencode_component(
633
+ value.gsub("+", "%20")).gsub(/(\r\n|\n|\r)/, "\n")) : nil
634
+ ]
635
+ end
636
+ end
637
+
563
638
  ##
564
639
  # Creates a new uri object from component parts.
565
640
  #
@@ -28,7 +28,7 @@ if !defined?(Addressable::VERSION)
28
28
  module VERSION #:nodoc:
29
29
  MAJOR = 2
30
30
  MINOR = 2
31
- TINY = 0
31
+ TINY = 1
32
32
 
33
33
  STRING = [MAJOR, MINOR, TINY].join('.')
34
34
  end
@@ -3693,6 +3693,95 @@ describe Addressable::URI, "when parsing a non-String object" do
3693
3693
  end
3694
3694
  end
3695
3695
 
3696
+ describe Addressable::URI, "when form encoding a hash" do
3697
+ it "should result in correct percent encoded sequence" do
3698
+ Addressable::URI.form_encode(
3699
+ {"&one" => "/1", "=two" => "?2", ":three" => "#3"}
3700
+ ).should == "%26one=%2F1&%3Dtwo=%3F2&%3Athree=%233"
3701
+ end
3702
+
3703
+ it "should result in correct percent encoded sequence" do
3704
+ Addressable::URI.form_encode(
3705
+ {"q" => "one two three"}
3706
+ ).should == "q=one+two+three"
3707
+ end
3708
+
3709
+ it "should result in correct percent encoded sequence" do
3710
+ Addressable::URI.form_encode(
3711
+ {"key" => nil}
3712
+ ).should == "key="
3713
+ end
3714
+
3715
+ it "should result in correctly encoded newlines" do
3716
+ Addressable::URI.form_encode(
3717
+ {"text" => "one\ntwo\rthree\r\nfour\n\r"}
3718
+ ).should == "text=one%0D%0Atwo%0D%0Athree%0D%0Afour%0D%0A%0D%0A"
3719
+ end
3720
+
3721
+ it "should result in a sorted percent encoded sequence" do
3722
+ Addressable::URI.form_encode(
3723
+ [["a", "1"], ["dup", "3"], ["dup", "2"]], true
3724
+ ).should == "a=1&dup=2&dup=3"
3725
+ end
3726
+ end
3727
+
3728
+ describe Addressable::URI, "when form encoding a non-Array object" do
3729
+ it "should raise a TypeError for objects than cannot be converted" do
3730
+ (lambda do
3731
+ Addressable::URI.form_encode(42)
3732
+ end).should raise_error(TypeError, "Can't convert Fixnum into Array.")
3733
+ end
3734
+ end
3735
+
3736
+ describe Addressable::URI, "when form unencoding a string" do
3737
+ it "should result in correct values" do
3738
+ Addressable::URI.form_unencode(
3739
+ "%26one=%2F1&%3Dtwo=%3F2&%3Athree=%233"
3740
+ ).should == [["&one", "/1"], ["=two", "?2"], [":three", "#3"]]
3741
+ end
3742
+
3743
+ it "should result in correct values" do
3744
+ Addressable::URI.form_unencode(
3745
+ "q=one+two+three"
3746
+ ).should == [["q", "one two three"]]
3747
+ end
3748
+
3749
+ it "should result in correct values" do
3750
+ Addressable::URI.form_unencode(
3751
+ "text=one%0D%0Atwo%0D%0Athree%0D%0Afour%0D%0A%0D%0A"
3752
+ ).should == [["text", "one\ntwo\nthree\nfour\n\n"]]
3753
+ end
3754
+
3755
+ it "should result in correct values" do
3756
+ Addressable::URI.form_unencode(
3757
+ "a=1&dup=2&dup=3"
3758
+ ).should == [["a", "1"], ["dup", "2"], ["dup", "3"]]
3759
+ end
3760
+
3761
+ it "should result in correct values" do
3762
+ Addressable::URI.form_unencode(
3763
+ "key"
3764
+ ).should == [["key", nil]]
3765
+ end
3766
+
3767
+ it "should result in correct values" do
3768
+ Addressable::URI.form_unencode("GivenName=Ren%C3%A9").should ==
3769
+ [["GivenName", "René"]]
3770
+ end
3771
+ end
3772
+
3773
+ describe Addressable::URI, "when form unencoding a non-String object" do
3774
+ it "should correctly parse anything with a 'to_str' method" do
3775
+ Addressable::URI.form_unencode(SuperString.new(42))
3776
+ end
3777
+
3778
+ it "should raise a TypeError for objects than cannot be converted" do
3779
+ (lambda do
3780
+ Addressable::URI.form_unencode(42)
3781
+ end).should raise_error(TypeError, "Can't convert Fixnum into String.")
3782
+ end
3783
+ end
3784
+
3696
3785
  describe Addressable::URI, "when normalizing a non-String object" do
3697
3786
  it "should correctly parse anything with a 'to_str' method" do
3698
3787
  Addressable::URI.normalize_component(SuperString.new(42))
@@ -3770,6 +3859,13 @@ describe Addressable::URI, "when encoding a multibyte string" do
3770
3859
  end
3771
3860
  end
3772
3861
 
3862
+ describe Addressable::URI, "when form encoding a multibyte string" do
3863
+ it "should result in correct percent encoded sequence" do
3864
+ Addressable::URI.form_encode({"GivenName" => "René"}).should ==
3865
+ "GivenName=Ren%C3%A9"
3866
+ end
3867
+ end
3868
+
3773
3869
  describe Addressable::URI, "when encoding a string with ASCII chars 0-15" do
3774
3870
  it "should result in correct percent encoded sequence" do
3775
3871
  Addressable::URI.encode_component("one\ntwo").should == "one%0Atwo"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: addressable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 2
9
- - 0
10
- version: 2.2.0
9
+ - 1
10
+ version: 2.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bob Aman
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-09 00:00:00 -07:00
18
+ date: 2010-08-19 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency