tco_client_core 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/tco_client_core/response.rb +14 -14
- data/lib/tco_client_core/version.rb +1 -1
- data/spec/response_spec.rb +14 -7
- metadata +1 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: bb7e85db0f524ca8d6aea9f9501d3b6a3c8d3d5e
         | 
| 4 | 
            +
              data.tar.gz: bb730570d3131439cc7ca55ad6fa25e5121affc3
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: d32fa588db99fb5745e8116e2aa5509708636a93f52341bea4757e53f4cf849dbf2459cea66913a195119734066791d87bf1749eca1324e8fb8771bd7cb7031a
         | 
| 7 | 
            +
              data.tar.gz: 641a5f4879038e5f005cd51ca9016ad39495c65c97e057f2934217bb10a79ceeb17654d5ba3018542d8493b335e24fa1c309b1ec3a3189c4eab7a3e405694484
         | 
| @@ -3,7 +3,19 @@ require "json" | |
| 3 3 |  | 
| 4 4 | 
             
            module TcoClientCore
         | 
| 5 5 | 
             
              class Response
         | 
| 6 | 
            -
                 | 
| 6 | 
            +
                def self.to_struct(object)
         | 
| 7 | 
            +
                  if object.is_a? Array
         | 
| 8 | 
            +
                    object.map { |v| to_struct(v) }
         | 
| 9 | 
            +
                  elsif object.is_a? Hash
         | 
| 10 | 
            +
                    object.each_with_object(OpenStruct.new) do |(key, val), st|
         | 
| 11 | 
            +
                      st[key] = to_struct(val)
         | 
| 12 | 
            +
                    end
         | 
| 13 | 
            +
                  else
         | 
| 14 | 
            +
                    object
         | 
| 15 | 
            +
                  end
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                attr_reader :raw, :to_struct
         | 
| 7 19 |  | 
| 8 20 | 
             
                def initialize(response)
         | 
| 9 21 | 
             
                  @raw = response
         | 
| @@ -12,6 +24,7 @@ module TcoClientCore | |
| 12 24 | 
             
                      raise TcoClientCore::RecordNotFound, value.first
         | 
| 13 25 | 
             
                    end
         | 
| 14 26 | 
             
                  end
         | 
| 27 | 
            +
                  @to_struct = self.class.to_struct(as_json)
         | 
| 15 28 | 
             
                end
         | 
| 16 29 |  | 
| 17 30 | 
             
                def to_json
         | 
| @@ -26,19 +39,6 @@ module TcoClientCore | |
| 26 39 | 
             
                  raw.to_s
         | 
| 27 40 | 
             
                end
         | 
| 28 41 |  | 
| 29 | 
            -
                def to_struct(object=nil)
         | 
| 30 | 
            -
                  object ||= as_json
         | 
| 31 | 
            -
                  if object.is_a? Array
         | 
| 32 | 
            -
                    object.map { |v| v.nil? ? v : to_struct(v) }
         | 
| 33 | 
            -
                  elsif object.is_a? Hash
         | 
| 34 | 
            -
                    object.each_with_object(OpenStruct.new) do |(key, val), st|
         | 
| 35 | 
            -
                      st[key] = val.nil? ? val : to_struct(val)
         | 
| 36 | 
            -
                    end
         | 
| 37 | 
            -
                  else
         | 
| 38 | 
            -
                    object
         | 
| 39 | 
            -
                  end
         | 
| 40 | 
            -
                end
         | 
| 41 | 
            -
             | 
| 42 42 | 
             
                def errors?
         | 
| 43 43 | 
             
                  json = as_json
         | 
| 44 44 | 
             
                  !!(json.is_a?(Hash) && (json.fetch("errors", nil) || json.fetch(:errors, nil)))
         | 
    
        data/spec/response_spec.rb
    CHANGED
    
    | @@ -34,19 +34,22 @@ describe TcoClientCore::Response do | |
| 34 34 | 
             
                  end
         | 
| 35 35 |  | 
| 36 36 | 
             
                  it "creates a open struct for nested hashes" do
         | 
| 37 | 
            -
                     | 
| 37 | 
            +
                    subject = TcoClientCore::Response.new({
         | 
| 38 38 | 
             
                      one: 1,
         | 
| 39 39 | 
             
                      two: { three: { four: 4 } }
         | 
| 40 | 
            -
                    })
         | 
| 40 | 
            +
                    }.to_json)
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                    ostruct = subject.to_struct
         | 
| 41 43 | 
             
                    expect(ostruct[:two]).to be_kind_of(OpenStruct)
         | 
| 42 44 | 
             
                    expect(ostruct.two.three.four).to eq(4)
         | 
| 43 45 | 
             
                  end
         | 
| 44 46 |  | 
| 45 47 | 
             
                  it "creates a open struct for array values with hashes" do
         | 
| 46 | 
            -
                     | 
| 48 | 
            +
                    subject = TcoClientCore::Response.new({
         | 
| 47 49 | 
             
                      one: 1,
         | 
| 48 50 | 
             
                      two: { three: [{ four: 4 }] }
         | 
| 49 | 
            -
                    })
         | 
| 51 | 
            +
                    }.to_json)
         | 
| 52 | 
            +
                    ostruct = subject.to_struct
         | 
| 50 53 | 
             
                    expect(ostruct.two.three.first).to be_kind_of(OpenStruct)
         | 
| 51 54 | 
             
                  end
         | 
| 52 55 |  | 
| @@ -61,16 +64,20 @@ describe TcoClientCore::Response do | |
| 61 64 |  | 
| 62 65 | 
             
                context "with an array" do
         | 
| 63 66 | 
             
                  it "creates a open struct for every hash property" do
         | 
| 64 | 
            -
                     | 
| 67 | 
            +
                    subject = TcoClientCore::Response.new([{ one: 1 }, { two: 2 }].to_json)
         | 
| 68 | 
            +
             | 
| 69 | 
            +
                    array = subject.to_struct;
         | 
| 65 70 | 
             
                    expect(array.first).to be_kind_of(OpenStruct)
         | 
| 66 71 | 
             
                    expect(array.last).to be_kind_of(OpenStruct)
         | 
| 67 72 | 
             
                  end
         | 
| 68 73 |  | 
| 69 74 | 
             
                  it "creates a open struct for nested arrays with hashes" do
         | 
| 70 | 
            -
                     | 
| 75 | 
            +
                    subject = TcoClientCore::Response.new([
         | 
| 71 76 | 
             
                      { one: 1 },
         | 
| 72 77 | 
             
                      [{ two: 2 }]
         | 
| 73 | 
            -
                    ])
         | 
| 78 | 
            +
                    ].to_json)
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                    array = subject.to_struct
         | 
| 74 81 | 
             
                    expect(array.last.first).to be_kind_of(OpenStruct)
         | 
| 75 82 | 
             
                  end
         | 
| 76 83 |  |