collection_json_serializer 0.0.1 → 0.2.0
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/.gitignore +4 -0
- data/.travis.yml +1 -3
- data/README.md +38 -13
- data/lib/collection_json_serializer.rb +1 -1
- data/lib/collection_json_serializer/builder.rb +10 -0
- data/lib/collection_json_serializer/objects/item.rb +11 -2
- data/lib/collection_json_serializer/objects/query.rb +43 -0
- data/lib/collection_json_serializer/serializer.rb +10 -0
- data/lib/collection_json_serializer/validator.rb +39 -2
- data/lib/collection_json_serializer/version.rb +1 -1
- data/test/builder/builder_test.rb +23 -8
- data/test/fixtures/serializers/user_serializer.rb +11 -0
- data/test/minitest_helper.rb +13 -0
- data/test/objects/item_test.rb +36 -8
- data/test/objects/queries_test.rb +41 -0
- data/test/serializer/href_test.rb +3 -3
- data/test/serializer/queries_test.rb +31 -0
- data/test/validator/invalid_test.rb +63 -9
- metadata +7 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 2ec98d08e1b7a1d83f75d774fdee42b77895dfdd
         | 
| 4 | 
            +
              data.tar.gz: 3e79907d1643600fe2ba316efdb65ab532bee250
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 7ed704824347ead480a3081c0be6ed6045880f348dc25dd19517aa4fc72ed86c9b58c12153363b49e5797bd9391ecb2f7d148167b6bc89114f325d507bf54968
         | 
| 7 | 
            +
              data.tar.gz: d065ea2525e44d26844b4d864cd381a345904077e0e2562904a98b1bfba363f873798af779e23ff5d06137f52e5248daf14f38a24b0a782e887875fe3ed041d1
         | 
    
        data/.gitignore
    CHANGED
    
    
    
        data/.travis.yml
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -2,12 +2,14 @@ | |
| 2 2 |  | 
| 3 3 | 
             
            [](https://travis-ci.org/carlesjove/collection_json_serializer)
         | 
| 4 4 |  | 
| 5 | 
            -
            | :warning:  | 
| 6 | 
            -
             | 
| 5 | 
            +
            | :warning: This is _not finished_ yet, so use it at your own risk. |
         | 
| 6 | 
            +
            --------------------------------------------------------------------
         | 
| 7 7 |  | 
| 8 8 | 
             
            A Ruby gem to respond with Collection+JSON.
         | 
| 9 9 |  | 
| 10 | 
            -
            CollectionJson::Serializer formats JSON responses following the Collection+JSON media type by Mike Amudsen. | 
| 10 | 
            +
            CollectionJson::Serializer formats JSON responses following the Collection+JSON media type by Mike Amudsen.
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            Please note that CollectionJson::Serializer only serializes data. You still need to set the proper Headers or media-types in your app.
         | 
| 11 13 |  | 
| 12 14 | 
             
            ## Installation
         | 
| 13 15 |  | 
| @@ -40,28 +42,39 @@ class UserSerializer < CollectionJson::Serializer | |
| 40 42 |  | 
| 41 43 | 
             
              # Please note that links can only be passed as hashes
         | 
| 42 44 | 
             
              links dashboard: { href: "http://example.com/my-dashboard" }
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              queries search: {
         | 
| 47 | 
            +
                href: "http://example.com/search",
         | 
| 48 | 
            +
                name: false # Don't automatically include the name attribute
         | 
| 49 | 
            +
              }, pagination: {
         | 
| 50 | 
            +
                rel: "page",
         | 
| 51 | 
            +
                href: "http://example.com/page",
         | 
| 52 | 
            +
                prompt: "Select a page number",
         | 
| 53 | 
            +
                data: [
         | 
| 54 | 
            +
                  { name: "page" }
         | 
| 55 | 
            +
                ]
         | 
| 56 | 
            +
              }
         | 
| 43 57 | 
             
            end
         | 
| 44 58 | 
             
            ```
         | 
| 45 59 |  | 
| 46 60 | 
             
            Then, you pass your objects to the serializer:
         | 
| 47 61 |  | 
| 48 62 | 
             
            ```ruby
         | 
| 49 | 
            -
            # Create your object as you wish
         | 
| 50 63 | 
             
            @user = User.new(name: "Carles Jove", email: "hola@carlus.cat")
         | 
| 51 64 |  | 
| 65 | 
            +
            # Pass it to the serializer
         | 
| 66 | 
            +
            user_serializer = UserSerializer.new(@user)
         | 
| 67 | 
            +
             | 
| 52 68 | 
             
            # You can also pass an array of objects
         | 
| 53 69 | 
             
            # user_serializer = UserSerializer.new([@user1, @user2, etc])
         | 
| 54 70 |  | 
| 55 | 
            -
            # Pass  | 
| 56 | 
            -
             | 
| 71 | 
            +
            # Pass the serializer to the builder
         | 
| 72 | 
            +
            collection = CollectionJson::Serializer::Builder.new(user_serializer)
         | 
| 73 | 
            +
            collection.to_json
         | 
| 57 74 |  | 
| 58 | 
            -
            #  | 
| 59 | 
            -
             | 
| 60 | 
            -
            builder.pack
         | 
| 75 | 
            +
            # You can get the collection as a hash, too
         | 
| 76 | 
            +
            collection.pack
         | 
| 61 77 | 
             
            # => { collection: { version: "1.0" } }
         | 
| 62 | 
            -
             | 
| 63 | 
            -
            # Get it as JSON
         | 
| 64 | 
            -
            builder.to_json
         | 
| 65 78 | 
             
            ```
         | 
| 66 79 |  | 
| 67 80 | 
             
            This will generate this Collection+JSON response:
         | 
| @@ -87,7 +100,19 @@ This will generate this Collection+JSON response: | |
| 87 100 | 
             
                    { "name": "name", "value": "" },
         | 
| 88 101 | 
             
                    { "name": "email", "value": "", "prompt": "My email" }
         | 
| 89 102 | 
             
                  ]
         | 
| 90 | 
            -
                }
         | 
| 103 | 
            +
                },
         | 
| 104 | 
            +
                "queries": [{
         | 
| 105 | 
            +
                  "rel": "search",
         | 
| 106 | 
            +
                  "href": "http://example.com/search"
         | 
| 107 | 
            +
                },{
         | 
| 108 | 
            +
                  "rel": "page",
         | 
| 109 | 
            +
                  "href": "http://example.com/page",
         | 
| 110 | 
            +
                  "name": "pagination",
         | 
| 111 | 
            +
                  "prompt": "Select a page number",
         | 
| 112 | 
            +
                  "data": [
         | 
| 113 | 
            +
                    { "name": "page", "value": "" }
         | 
| 114 | 
            +
                  ]
         | 
| 115 | 
            +
                }]
         | 
| 91 116 | 
             
              }
         | 
| 92 117 | 
             
            }
         | 
| 93 118 | 
             
            ```
         | 
| @@ -14,5 +14,5 @@ require "collection_json_serializer/validator/url" | |
| 14 14 | 
             
            require "collection_json_serializer/validator/value"
         | 
| 15 15 |  | 
| 16 16 | 
             
            require "collection_json_serializer/objects/item"
         | 
| 17 | 
            -
             | 
| 18 17 | 
             
            require "collection_json_serializer/objects/template"
         | 
| 18 | 
            +
            require "collection_json_serializer/objects/query"
         | 
| @@ -28,6 +28,7 @@ module CollectionJson | |
| 28 28 | 
             
                    add_href if @serializer.href.respond_to? :key
         | 
| 29 29 | 
             
                    add_items if @serializer.attributes.present?
         | 
| 30 30 | 
             
                    add_template if @serializer.template.present?
         | 
| 31 | 
            +
                    add_queries if @serializer.queries.present?
         | 
| 31 32 | 
             
                  end
         | 
| 32 33 |  | 
| 33 34 | 
             
                  def add_href
         | 
| @@ -51,6 +52,15 @@ module CollectionJson | |
| 51 52 | 
             
                      new(@serializer)
         | 
| 52 53 | 
             
                    @collection[:template].store :data, template.create
         | 
| 53 54 | 
             
                  end
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                  def add_queries
         | 
| 57 | 
            +
                    @collection.store :queries, Array.new
         | 
| 58 | 
            +
                    @serializer.queries.each_index do |i|
         | 
| 59 | 
            +
                      query = CollectionJson::Serializer::Objects::Query.
         | 
| 60 | 
            +
                        new(@serializer, item: i)
         | 
| 61 | 
            +
                      @collection[:queries] << query.create
         | 
| 62 | 
            +
                    end
         | 
| 63 | 
            +
                  end
         | 
| 54 64 | 
             
                end
         | 
| 55 65 | 
             
              end
         | 
| 56 66 | 
             
            end
         | 
| @@ -50,8 +50,9 @@ module CollectionJson | |
| 50 50 |  | 
| 51 51 | 
             
                        start_object :links, Array.new
         | 
| 52 52 | 
             
                        @item[:links] << {
         | 
| 53 | 
            -
                           | 
| 54 | 
            -
                          href: params[:properties][:href]
         | 
| 53 | 
            +
                          rel: set_rel(params),
         | 
| 54 | 
            +
                          href: params[:properties][:href],
         | 
| 55 | 
            +
                          name: params[:name].to_s
         | 
| 55 56 | 
             
                        }.merge!(params[:properties])
         | 
| 56 57 | 
             
                      end if @serializer.links.present?
         | 
| 57 58 | 
             
                    end
         | 
| @@ -64,6 +65,14 @@ module CollectionJson | |
| 64 65 | 
             
                      url = @serializer.href[:self] || @serializer.href
         | 
| 65 66 | 
             
                      parse_url(url, @resource)
         | 
| 66 67 | 
             
                    end
         | 
| 68 | 
            +
             | 
| 69 | 
            +
                    def set_rel(params)
         | 
| 70 | 
            +
                      if params[:properties].key? :rel
         | 
| 71 | 
            +
                        params[:properties][:rel].to_s
         | 
| 72 | 
            +
                      else
         | 
| 73 | 
            +
                        params[:name].to_s
         | 
| 74 | 
            +
                      end
         | 
| 75 | 
            +
                    end
         | 
| 67 76 | 
             
                  end
         | 
| 68 77 | 
             
                end
         | 
| 69 78 | 
             
              end
         | 
| @@ -0,0 +1,43 @@ | |
| 1 | 
            +
            module CollectionJson
         | 
| 2 | 
            +
              class Serializer
         | 
| 3 | 
            +
                class Objects
         | 
| 4 | 
            +
                  class Query
         | 
| 5 | 
            +
                    def initialize(serializer, item: 0)
         | 
| 6 | 
            +
                      @serializer = serializer
         | 
| 7 | 
            +
                      @index = item >= 0 ? item : 0
         | 
| 8 | 
            +
                      @key = @serializer.queries.first.keys[@index]
         | 
| 9 | 
            +
                      @resource = @serializer.queries.first[@key]
         | 
| 10 | 
            +
                      @query = Hash.new
         | 
| 11 | 
            +
                    end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                    def create
         | 
| 14 | 
            +
                      @query[:rel]  = @resource[:rel] || @key
         | 
| 15 | 
            +
                      @query[:href] = @resource[:href]
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                      # Optional fields
         | 
| 18 | 
            +
                      @query[:name]   = extract_name if name?
         | 
| 19 | 
            +
                      @query[:prompt] = @resource[:prompt] if @resource[:prompt]
         | 
| 20 | 
            +
                      @query[:data]   = add_data if @resource[:data]
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                      @query
         | 
| 23 | 
            +
                    end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                    private
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                    def extract_name
         | 
| 28 | 
            +
                      @resource[:name].present? ? @resource[:name] : @key
         | 
| 29 | 
            +
                    end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                    def add_data
         | 
| 32 | 
            +
                      @resource[:data].each_with_object([]) do |attr, data|
         | 
| 33 | 
            +
                        data << { name: attr[:name], value: nil.to_s }
         | 
| 34 | 
            +
                      end
         | 
| 35 | 
            +
                    end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                    def name?
         | 
| 38 | 
            +
                      @resource[:name] != false
         | 
| 39 | 
            +
                    end
         | 
| 40 | 
            +
                  end
         | 
| 41 | 
            +
                end
         | 
| 42 | 
            +
              end
         | 
| 43 | 
            +
            end
         | 
| @@ -5,6 +5,7 @@ module CollectionJson | |
| 5 5 | 
             
                  attr_accessor :attributes
         | 
| 6 6 | 
             
                  attr_accessor :template
         | 
| 7 7 | 
             
                  attr_accessor :links
         | 
| 8 | 
            +
                  attr_accessor :queries
         | 
| 8 9 | 
             
                end
         | 
| 9 10 |  | 
| 10 11 | 
             
                def self.inherited(base)
         | 
| @@ -12,6 +13,7 @@ module CollectionJson | |
| 12 13 | 
             
                  base.attributes = []
         | 
| 13 14 | 
             
                  base.template = []
         | 
| 14 15 | 
             
                  base.links = []
         | 
| 16 | 
            +
                  base.queries = []
         | 
| 15 17 | 
             
                end
         | 
| 16 18 |  | 
| 17 19 | 
             
                def self.href(*attrs)
         | 
| @@ -30,6 +32,10 @@ module CollectionJson | |
| 30 32 | 
             
                  @links.concat attrs
         | 
| 31 33 | 
             
                end
         | 
| 32 34 |  | 
| 35 | 
            +
                def self.queries(*attrs)
         | 
| 36 | 
            +
                  @queries.concat attrs
         | 
| 37 | 
            +
                end
         | 
| 38 | 
            +
             | 
| 33 39 | 
             
                attr_accessor :resources
         | 
| 34 40 |  | 
| 35 41 | 
             
                def initialize(resource)
         | 
| @@ -56,6 +62,10 @@ module CollectionJson | |
| 56 62 | 
             
                  self.class.links
         | 
| 57 63 | 
             
                end
         | 
| 58 64 |  | 
| 65 | 
            +
                def queries
         | 
| 66 | 
            +
                  self.class.queries
         | 
| 67 | 
            +
                end
         | 
| 68 | 
            +
             | 
| 59 69 | 
             
                def invalid?
         | 
| 60 70 | 
             
                  Validator.new(self).invalid?
         | 
| 61 71 | 
             
                end
         | 
| @@ -26,7 +26,8 @@ module CollectionJson | |
| 26 26 | 
             
                      :attributes,
         | 
| 27 27 | 
             
                      :href,
         | 
| 28 28 | 
             
                      :links,
         | 
| 29 | 
            -
                      :template
         | 
| 29 | 
            +
                      :template,
         | 
| 30 | 
            +
                      :queries
         | 
| 30 31 | 
             
                    ].each { |m| send("validate_#{m}") }
         | 
| 31 32 | 
             
                  end
         | 
| 32 33 |  | 
| @@ -98,6 +99,42 @@ module CollectionJson | |
| 98 99 | 
             
                    end if @serializer.template.any?
         | 
| 99 100 | 
             
                  end
         | 
| 100 101 |  | 
| 102 | 
            +
                  def validate_queries
         | 
| 103 | 
            +
                    @serializer.queries.each do |query|
         | 
| 104 | 
            +
                      params = query.extract_params
         | 
| 105 | 
            +
             | 
| 106 | 
            +
                      unless params[:properties].key? :href
         | 
| 107 | 
            +
                        error_for :missing_attribute,
         | 
| 108 | 
            +
                                  root: :queries,
         | 
| 109 | 
            +
                                  path: [params[:name], "href"]
         | 
| 110 | 
            +
             | 
| 111 | 
            +
                        next
         | 
| 112 | 
            +
                      end
         | 
| 113 | 
            +
             | 
| 114 | 
            +
                      if url_is_invalid? params[:properties][:href]
         | 
| 115 | 
            +
                        error_for :url, root: :queries, path: [params[:name], "href"]
         | 
| 116 | 
            +
                      end
         | 
| 117 | 
            +
             | 
| 118 | 
            +
                      params[:properties].each do |key, value|
         | 
| 119 | 
            +
                        next if key == :data || key == :href
         | 
| 120 | 
            +
             | 
| 121 | 
            +
                        if value_is_invalid?(value)
         | 
| 122 | 
            +
                          error_for :value, root: :queries, path: [params[:name], key]
         | 
| 123 | 
            +
                        end
         | 
| 124 | 
            +
                      end
         | 
| 125 | 
            +
             | 
| 126 | 
            +
                      if params[:properties].key?(:data)
         | 
| 127 | 
            +
                        params[:properties][:data].each do |hash|
         | 
| 128 | 
            +
                          if value_is_invalid?(hash[:name])
         | 
| 129 | 
            +
                            error_for :value,
         | 
| 130 | 
            +
                                      root: :queries,
         | 
| 131 | 
            +
                                      path: [params[:name], "data", "name"]
         | 
| 132 | 
            +
                          end
         | 
| 133 | 
            +
                        end
         | 
| 134 | 
            +
                      end
         | 
| 135 | 
            +
                    end if @serializer.queries.present?
         | 
| 136 | 
            +
                  end
         | 
| 137 | 
            +
             | 
| 101 138 | 
             
                  def value_is_invalid?(value)
         | 
| 102 139 | 
             
                    v = CollectionJson::Serializer::Validator::Value.new(value)
         | 
| 103 140 | 
             
                    v.invalid?
         | 
| @@ -108,7 +145,7 @@ module CollectionJson | |
| 108 145 | 
             
                    v.invalid?
         | 
| 109 146 | 
             
                  end
         | 
| 110 147 |  | 
| 111 | 
            -
                  def error_for(kind, root | 
| 148 | 
            +
                  def error_for(kind, root: root, path: [])
         | 
| 112 149 | 
             
                    case kind.to_sym
         | 
| 113 150 | 
             
                    when :url
         | 
| 114 151 | 
             
                      ending = " is an invalid URL"
         | 
| @@ -33,8 +33,9 @@ module CollectionJson | |
| 33 33 | 
             
                              ],
         | 
| 34 34 | 
             
                              links: [
         | 
| 35 35 | 
             
                                {
         | 
| 36 | 
            -
                                   | 
| 37 | 
            -
                                  href: "http://example.com/my-dashboard"
         | 
| 36 | 
            +
                                  rel: "dashboard",
         | 
| 37 | 
            +
                                  href: "http://example.com/my-dashboard",
         | 
| 38 | 
            +
                                  name: "dashboard"
         | 
| 38 39 | 
             
                                }
         | 
| 39 40 | 
             
                              ]
         | 
| 40 41 | 
             
                            }
         | 
| @@ -51,7 +52,13 @@ module CollectionJson | |
| 51 52 | 
             
                                prompt: "My email"
         | 
| 52 53 | 
             
                              }
         | 
| 53 54 | 
             
                            ]
         | 
| 54 | 
            -
                          }
         | 
| 55 | 
            +
                          },
         | 
| 56 | 
            +
                          queries: [
         | 
| 57 | 
            +
                            {
         | 
| 58 | 
            +
                              rel: "search",
         | 
| 59 | 
            +
                              href: "http://example.com/search"
         | 
| 60 | 
            +
                            }
         | 
| 61 | 
            +
                          ]
         | 
| 55 62 | 
             
                        }
         | 
| 56 63 | 
             
                      }
         | 
| 57 64 |  | 
| @@ -80,8 +87,9 @@ module CollectionJson | |
| 80 87 | 
             
                              ],
         | 
| 81 88 | 
             
                              links: [
         | 
| 82 89 | 
             
                                {
         | 
| 83 | 
            -
                                   | 
| 84 | 
            -
                                  href: "http://example.com/my-dashboard"
         | 
| 90 | 
            +
                                  rel: "dashboard",
         | 
| 91 | 
            +
                                  href: "http://example.com/my-dashboard",
         | 
| 92 | 
            +
                                  name: "dashboard"
         | 
| 85 93 | 
             
                                }
         | 
| 86 94 | 
             
                              ]
         | 
| 87 95 | 
             
                            },
         | 
| @@ -99,8 +107,9 @@ module CollectionJson | |
| 99 107 | 
             
                              ],
         | 
| 100 108 | 
             
                              links: [
         | 
| 101 109 | 
             
                                {
         | 
| 102 | 
            -
                                   | 
| 103 | 
            -
                                  href: "http://example.com/my-dashboard"
         | 
| 110 | 
            +
                                  rel: "dashboard",
         | 
| 111 | 
            +
                                  href: "http://example.com/my-dashboard",
         | 
| 112 | 
            +
                                  name: "dashboard"
         | 
| 104 113 | 
             
                                }
         | 
| 105 114 | 
             
                              ]
         | 
| 106 115 | 
             
                            }
         | 
| @@ -117,7 +126,13 @@ module CollectionJson | |
| 117 126 | 
             
                                prompt: "My email"
         | 
| 118 127 | 
             
                              }
         | 
| 119 128 | 
             
                            ]
         | 
| 120 | 
            -
                          }
         | 
| 129 | 
            +
                          },
         | 
| 130 | 
            +
                          queries: [
         | 
| 131 | 
            +
                            {
         | 
| 132 | 
            +
                              rel: "search",
         | 
| 133 | 
            +
                              href: "http://example.com/search"
         | 
| 134 | 
            +
                            }
         | 
| 135 | 
            +
                          ]
         | 
| 121 136 | 
             
                        }
         | 
| 122 137 | 
             
                      }
         | 
| 123 138 |  | 
| @@ -4,4 +4,15 @@ class UserSerializer < CollectionJson::Serializer | |
| 4 4 | 
             
              attributes :name, :email
         | 
| 5 5 | 
             
              template :name, email: { prompt: "My email" }
         | 
| 6 6 | 
             
              links dashboard: { href: "http://example.com/my-dashboard" }
         | 
| 7 | 
            +
              queries search: {
         | 
| 8 | 
            +
                href: "http://example.com/search",
         | 
| 9 | 
            +
                name: false
         | 
| 10 | 
            +
              }, pagination: {
         | 
| 11 | 
            +
                rel: "page",
         | 
| 12 | 
            +
                href: "http://example.com/page",
         | 
| 13 | 
            +
                prompt: "Select a page number",
         | 
| 14 | 
            +
                data: [
         | 
| 15 | 
            +
                  { name: "page" }
         | 
| 16 | 
            +
                ]
         | 
| 17 | 
            +
              }
         | 
| 7 18 | 
             
            end
         | 
    
        data/test/minitest_helper.rb
    CHANGED
    
    | @@ -9,3 +9,16 @@ Dir.glob(File.dirname(__FILE__) + "/fixtures/serializers/**/*.rb") { |file| requ | |
| 9 9 |  | 
| 10 10 | 
             
            require "active_support/json"
         | 
| 11 11 | 
             
            require "active_support/inflector"
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            module TestHelper
         | 
| 14 | 
            +
              def empty_serializer_for(object)
         | 
| 15 | 
            +
                serializer = CollectionJson::Serializer.new(object)
         | 
| 16 | 
            +
                serializer.class.attributes = []
         | 
| 17 | 
            +
                serializer.class.href = []
         | 
| 18 | 
            +
                serializer.class.links = []
         | 
| 19 | 
            +
                serializer.class.template = []
         | 
| 20 | 
            +
                serializer.class.queries = []
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                serializer
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
            end
         | 
    
        data/test/objects/item_test.rb
    CHANGED
    
    | @@ -5,6 +5,8 @@ module CollectionJson | |
| 5 5 | 
             
                class Objects
         | 
| 6 6 | 
             
                  class Item
         | 
| 7 7 | 
             
                    class TestItem < Minitest::Test
         | 
| 8 | 
            +
                      include TestHelper
         | 
| 9 | 
            +
             | 
| 8 10 | 
             
                      def setup
         | 
| 9 11 | 
             
                        @user1 = User.new(name: "Carles Jove", email: "hola@carlus.cat")
         | 
| 10 12 | 
             
                        @user2 = User.new(name: "Aina Jove", email: "hola@example.com")
         | 
| @@ -12,6 +14,17 @@ module CollectionJson | |
| 12 14 | 
             
                        @item = Item.new(@user_serializer)
         | 
| 13 15 | 
             
                      end
         | 
| 14 16 |  | 
| 17 | 
            +
                      def test_that_rel_will_beadded_from_the_name_when_missing
         | 
| 18 | 
            +
                        serializer = empty_serializer_for(@user1)
         | 
| 19 | 
            +
                        serializer.class.attributes = [:name]
         | 
| 20 | 
            +
                        serializer.class.links = [dashboard: { href: "http://example.com" }]
         | 
| 21 | 
            +
                        item = Item.new(serializer)
         | 
| 22 | 
            +
                        actual = item.create[:links].first
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                        assert actual.include? :rel
         | 
| 25 | 
            +
                        assert_equal "dashboard", actual[:rel]
         | 
| 26 | 
            +
                      end
         | 
| 27 | 
            +
             | 
| 15 28 | 
             
                      def test_that_an_item_can_be_build
         | 
| 16 29 | 
             
                        expected = {
         | 
| 17 30 | 
             
                          href: "http://example.com/users/#{@user1.id}",
         | 
| @@ -20,7 +33,11 @@ module CollectionJson | |
| 20 33 | 
             
                            { name: "email", value: "hola@carlus.cat" }
         | 
| 21 34 | 
             
                          ],
         | 
| 22 35 | 
             
                          links: [
         | 
| 23 | 
            -
                            { | 
| 36 | 
            +
                            {
         | 
| 37 | 
            +
                              rel: "dashboard",
         | 
| 38 | 
            +
                              href: "http://example.com/my-dashboard",
         | 
| 39 | 
            +
                              name: "dashboard"
         | 
| 40 | 
            +
                            }
         | 
| 24 41 | 
             
                          ]
         | 
| 25 42 | 
             
                        }
         | 
| 26 43 |  | 
| @@ -38,7 +55,11 @@ module CollectionJson | |
| 38 55 | 
             
                            { name: "email", value: "hola@example.com" }
         | 
| 39 56 | 
             
                          ],
         | 
| 40 57 | 
             
                          links: [
         | 
| 41 | 
            -
                            { | 
| 58 | 
            +
                            {
         | 
| 59 | 
            +
                              rel: "dashboard",
         | 
| 60 | 
            +
                              href: "http://example.com/my-dashboard",
         | 
| 61 | 
            +
                              name: "dashboard"
         | 
| 62 | 
            +
                            }
         | 
| 42 63 | 
             
                          ]
         | 
| 43 64 | 
             
                        }
         | 
| 44 65 |  | 
| @@ -50,9 +71,12 @@ module CollectionJson | |
| 50 71 | 
             
                        item = Item.new(custom_serializer)
         | 
| 51 72 |  | 
| 52 73 | 
             
                        expected = {
         | 
| 53 | 
            -
                          data: [
         | 
| 54 | 
            -
                             | 
| 55 | 
            -
             | 
| 74 | 
            +
                          data: [{
         | 
| 75 | 
            +
                            name: "name",
         | 
| 76 | 
            +
                            value: "Carles Jove",
         | 
| 77 | 
            +
                            anything: "at all",
         | 
| 78 | 
            +
                            whatever: "really"
         | 
| 79 | 
            +
                          }]
         | 
| 56 80 | 
             
                        }
         | 
| 57 81 | 
             
                        assert_equal expected.to_json, item.create.to_json
         | 
| 58 82 | 
             
                      end
         | 
| @@ -62,9 +86,13 @@ module CollectionJson | |
| 62 86 | 
             
                        item = Item.new(custom_serializer)
         | 
| 63 87 |  | 
| 64 88 | 
             
                        expected = {
         | 
| 65 | 
            -
                          links: [
         | 
| 66 | 
            -
                             | 
| 67 | 
            -
             | 
| 89 | 
            +
                          links: [{
         | 
| 90 | 
            +
                            rel: "dashboard",
         | 
| 91 | 
            +
                            name: "dashboard",
         | 
| 92 | 
            +
                            href: "/my-dashboard",
         | 
| 93 | 
            +
                            anything: "at all",
         | 
| 94 | 
            +
                            whatever: "really"
         | 
| 95 | 
            +
                          }]
         | 
| 68 96 | 
             
                        }
         | 
| 69 97 |  | 
| 70 98 | 
             
                        assert_equal expected[:links], item.create[:links]
         | 
| @@ -0,0 +1,41 @@ | |
| 1 | 
            +
            require "minitest_helper"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module CollectionJson
         | 
| 4 | 
            +
              class Serializer
         | 
| 5 | 
            +
                class Objects
         | 
| 6 | 
            +
                  class Query
         | 
| 7 | 
            +
                    class TestQuery < Minitest::Test
         | 
| 8 | 
            +
                      def setup
         | 
| 9 | 
            +
                        @user = User.new(name: "Carles Jove", email: "hola@carlus.cat")
         | 
| 10 | 
            +
                        @user_serializer = UserSerializer.new(@user)
         | 
| 11 | 
            +
                      end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                      def test_that_a_query_can_be_build
         | 
| 14 | 
            +
                        query = Query.new(@user_serializer, item: 0)
         | 
| 15 | 
            +
                        expected = {
         | 
| 16 | 
            +
                          rel: "search",
         | 
| 17 | 
            +
                          href: "http://example.com/search"
         | 
| 18 | 
            +
                        }
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                        assert_equal expected.to_json, query.create.to_json
         | 
| 21 | 
            +
                      end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                      def test_that_a_query_can_include_optional_fields
         | 
| 24 | 
            +
                        query = Query.new(@user_serializer, item: 1)
         | 
| 25 | 
            +
                        expected = {
         | 
| 26 | 
            +
                          rel: "page",
         | 
| 27 | 
            +
                          href: "http://example.com/page",
         | 
| 28 | 
            +
                          name: "pagination",
         | 
| 29 | 
            +
                          prompt: "Select a page number",
         | 
| 30 | 
            +
                          data: [
         | 
| 31 | 
            +
                            { name: "page", value: "" }
         | 
| 32 | 
            +
                          ]
         | 
| 33 | 
            +
                        }
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                        assert_equal expected.to_json, query.create.to_json
         | 
| 36 | 
            +
                      end
         | 
| 37 | 
            +
                    end
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
                end
         | 
| 40 | 
            +
              end
         | 
| 41 | 
            +
            end
         | 
| @@ -3,6 +3,8 @@ require "minitest_helper" | |
| 3 3 | 
             
            module CollectionJson
         | 
| 4 4 | 
             
              class Serializer
         | 
| 5 5 | 
             
                class TestHref < Minitest::Test
         | 
| 6 | 
            +
                  include TestHelper
         | 
| 7 | 
            +
             | 
| 6 8 | 
             
                  def setup
         | 
| 7 9 | 
             
                    @user = User.new(name: "Carles Jove", email: "hola@carlus.cat")
         | 
| 8 10 | 
             
                    @user_serializer = UserSerializer.new(@user)
         | 
| @@ -23,10 +25,8 @@ module CollectionJson | |
| 23 25 | 
             
                  end
         | 
| 24 26 |  | 
| 25 27 | 
             
                  def test_that_a_placeholder_can_be_used_for_urls
         | 
| 26 | 
            -
                    user_serializer =  | 
| 28 | 
            +
                    user_serializer = empty_serializer_for(@user)
         | 
| 27 29 | 
             
                    user_serializer.class.attributes = [:name]
         | 
| 28 | 
            -
                    user_serializer.class.links = []
         | 
| 29 | 
            -
                    user_serializer.class.template = []
         | 
| 30 30 | 
             
                    user_serializer.class.href = [self: "http://example.com/users/{id}"]
         | 
| 31 31 | 
             
                    builder = Builder.new(user_serializer)
         | 
| 32 32 |  | 
| @@ -0,0 +1,31 @@ | |
| 1 | 
            +
            require "minitest_helper"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module CollectionJson
         | 
| 4 | 
            +
              class Serializer
         | 
| 5 | 
            +
                class TestQueries < Minitest::Test
         | 
| 6 | 
            +
                  def setup
         | 
| 7 | 
            +
                    @user = User.new(name: "Carles Jove", email: "hola@carlus.cat")
         | 
| 8 | 
            +
                    @user_serializer = UserSerializer.new(@user)
         | 
| 9 | 
            +
                  end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                  def test_template_attributes
         | 
| 12 | 
            +
                    expected = [
         | 
| 13 | 
            +
                      search: {
         | 
| 14 | 
            +
                        href: "http://example.com/search",
         | 
| 15 | 
            +
                        name: false
         | 
| 16 | 
            +
                      },
         | 
| 17 | 
            +
                      pagination: {
         | 
| 18 | 
            +
                        rel: "page",
         | 
| 19 | 
            +
                        href: "http://example.com/page",
         | 
| 20 | 
            +
                        prompt: "Select a page number",
         | 
| 21 | 
            +
                        data: [
         | 
| 22 | 
            +
                          { name: "page" }
         | 
| 23 | 
            +
                        ]
         | 
| 24 | 
            +
                      }
         | 
| 25 | 
            +
                    ]
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                    assert_equal expected, @user_serializer.class.queries
         | 
| 28 | 
            +
                  end
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
            end
         | 
| @@ -4,6 +4,8 @@ module CollectionJson | |
| 4 4 | 
             
              class Serializer
         | 
| 5 5 | 
             
                class Validator
         | 
| 6 6 | 
             
                  class TestInvalid < Minitest::Test
         | 
| 7 | 
            +
                    include TestHelper
         | 
| 8 | 
            +
             | 
| 7 9 | 
             
                    def setup
         | 
| 8 10 | 
             
                      @user = User.new(
         | 
| 9 11 | 
             
                        name: "Carles Jove",
         | 
| @@ -23,14 +25,10 @@ module CollectionJson | |
| 23 25 | 
             
                        []
         | 
| 24 26 | 
             
                      ]
         | 
| 25 27 |  | 
| 26 | 
            -
                      @invalid =  | 
| 27 | 
            -
                      @invalid.class.attributes = []
         | 
| 28 | 
            -
                      @invalid.class.href = []
         | 
| 29 | 
            -
                      @invalid.class.links = []
         | 
| 30 | 
            -
                      @invalid.class.template = []
         | 
| 28 | 
            +
                      @invalid = empty_serializer_for(@user)
         | 
| 31 29 | 
             
                    end
         | 
| 32 30 |  | 
| 33 | 
            -
                    #  | 
| 31 | 
            +
                    # Href
         | 
| 34 32 | 
             
                    def test_that_href_generates_errors
         | 
| 35 33 | 
             
                      @invalid.class.href = [self: "/users/1", collection: "www.users.com"]
         | 
| 36 34 |  | 
| @@ -49,7 +47,7 @@ module CollectionJson | |
| 49 47 | 
             
                              include? "href is an invalid URL"
         | 
| 50 48 | 
             
                    end
         | 
| 51 49 |  | 
| 52 | 
            -
                    #  | 
| 50 | 
            +
                    # Links
         | 
| 53 51 | 
             
                    def test_that_links_generates_errors
         | 
| 54 52 | 
             
                      @invalid.class.links = [dashboard: { href: "/my-dashboard" }]
         | 
| 55 53 | 
             
                      assert @invalid.invalid?
         | 
| @@ -77,7 +75,7 @@ module CollectionJson | |
| 77 75 | 
             
                              include? "links:dashboard:href is missing"
         | 
| 78 76 | 
             
                    end
         | 
| 79 77 |  | 
| 80 | 
            -
                    #  | 
| 78 | 
            +
                    # Attributes
         | 
| 81 79 | 
             
                    def test_that_invalid_attributes_return_values_generate_errors
         | 
| 82 80 | 
             
                      @invalid.class.attributes = [:name]
         | 
| 83 81 |  | 
| @@ -112,7 +110,7 @@ module CollectionJson | |
| 112 110 | 
             
                      end
         | 
| 113 111 | 
             
                    end
         | 
| 114 112 |  | 
| 115 | 
            -
                    #  | 
| 113 | 
            +
                    # Template
         | 
| 116 114 | 
             
                    def test_that_template_values_validate
         | 
| 117 115 | 
             
                      @invalid_value_types.each do |invalidate|
         | 
| 118 116 | 
             
                        @invalid.class.template = [
         | 
| @@ -132,6 +130,62 @@ module CollectionJson | |
| 132 130 | 
             
                                include? "template:name:name is an invalid value"
         | 
| 133 131 | 
             
                      end
         | 
| 134 132 | 
             
                    end
         | 
| 133 | 
            +
             | 
| 134 | 
            +
                    # Queries
         | 
| 135 | 
            +
                    def test_that_queries_validate_href_format
         | 
| 136 | 
            +
                      @invalid.class.queries = [
         | 
| 137 | 
            +
                        search: {
         | 
| 138 | 
            +
                          href: "not-valid"
         | 
| 139 | 
            +
                        }
         | 
| 140 | 
            +
                      ]
         | 
| 141 | 
            +
             | 
| 142 | 
            +
                      assert @invalid.invalid?,
         | 
| 143 | 
            +
                             "not-valid should be invalid"
         | 
| 144 | 
            +
                      assert @invalid.errors.include?(:queries),
         | 
| 145 | 
            +
                             "not-valid should be invalid"
         | 
| 146 | 
            +
                      assert @invalid.errors[:queries][0].
         | 
| 147 | 
            +
                              include? "queries:search:href is an invalid URL"
         | 
| 148 | 
            +
                    end
         | 
| 149 | 
            +
             | 
| 150 | 
            +
                    def test_that_queries_href_is_required
         | 
| 151 | 
            +
                      @invalid.class.queries = [
         | 
| 152 | 
            +
                        search: {
         | 
| 153 | 
            +
                          name: "missing href"
         | 
| 154 | 
            +
                        }
         | 
| 155 | 
            +
                      ]
         | 
| 156 | 
            +
             | 
| 157 | 
            +
                      assert @invalid.invalid?
         | 
| 158 | 
            +
                      assert @invalid.errors.include?(:queries),
         | 
| 159 | 
            +
                             "should include queries errors"
         | 
| 160 | 
            +
                      assert @invalid.errors[:queries][0].
         | 
| 161 | 
            +
                              include? "queries:search:href is missing"
         | 
| 162 | 
            +
                    end
         | 
| 163 | 
            +
             | 
| 164 | 
            +
                    def test_that_queries_values_are_validated
         | 
| 165 | 
            +
                      @invalid_value_types.each do |invalidate|
         | 
| 166 | 
            +
                        @invalid.class.queries = [
         | 
| 167 | 
            +
                          search: {
         | 
| 168 | 
            +
                            href: "http://example.com/",
         | 
| 169 | 
            +
                            name: invalidate,
         | 
| 170 | 
            +
                            rel: invalidate,
         | 
| 171 | 
            +
                            data: [
         | 
| 172 | 
            +
                              name: invalidate
         | 
| 173 | 
            +
                            ]
         | 
| 174 | 
            +
                          }
         | 
| 175 | 
            +
                        ]
         | 
| 176 | 
            +
             | 
| 177 | 
            +
                        assert @invalid.invalid?,
         | 
| 178 | 
            +
                                "#{invalidate} should be invalid"
         | 
| 179 | 
            +
                        assert @invalid.errors.include?(:queries),
         | 
| 180 | 
            +
                               "should include errors for queries"
         | 
| 181 | 
            +
                        assert @invalid.errors[:queries][0].
         | 
| 182 | 
            +
                                include? "queries:search:name is an invalid value"
         | 
| 183 | 
            +
                        assert @invalid.errors[:queries][1].
         | 
| 184 | 
            +
                                include? "queries:search:rel is an invalid value"
         | 
| 185 | 
            +
                        assert @invalid.errors[:queries][2].
         | 
| 186 | 
            +
                                include? "queries:search:data:name is an invalid value"
         | 
| 187 | 
            +
                      end
         | 
| 188 | 
            +
                    end
         | 
| 135 189 | 
             
                  end
         | 
| 136 190 | 
             
                end
         | 
| 137 191 | 
             
              end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: collection_json_serializer
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0 | 
| 4 | 
            +
              version: 0.2.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Carles Jove i Buxeda
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2014-12- | 
| 11 | 
            +
            date: 2014-12-19 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: activesupport
         | 
| @@ -86,6 +86,7 @@ files: | |
| 86 86 | 
             
            - lib/collection_json_serializer/core_ext/hash.rb
         | 
| 87 87 | 
             
            - lib/collection_json_serializer/core_ext/symbol.rb
         | 
| 88 88 | 
             
            - lib/collection_json_serializer/objects/item.rb
         | 
| 89 | 
            +
            - lib/collection_json_serializer/objects/query.rb
         | 
| 89 90 | 
             
            - lib/collection_json_serializer/objects/template.rb
         | 
| 90 91 | 
             
            - lib/collection_json_serializer/serializer.rb
         | 
| 91 92 | 
             
            - lib/collection_json_serializer/support.rb
         | 
| @@ -106,10 +107,12 @@ files: | |
| 106 107 | 
             
            - test/fixtures/serializers/valid_serializer.rb
         | 
| 107 108 | 
             
            - test/minitest_helper.rb
         | 
| 108 109 | 
             
            - test/objects/item_test.rb
         | 
| 110 | 
            +
            - test/objects/queries_test.rb
         | 
| 109 111 | 
             
            - test/objects/template_test.rb
         | 
| 110 112 | 
             
            - test/serializer/data_test.rb
         | 
| 111 113 | 
             
            - test/serializer/href_test.rb
         | 
| 112 114 | 
             
            - test/serializer/links_test.rb
         | 
| 115 | 
            +
            - test/serializer/queries_test.rb
         | 
| 113 116 | 
             
            - test/serializer/template_test.rb
         | 
| 114 117 | 
             
            - test/support/ext_test.rb
         | 
| 115 118 | 
             
            - test/support/support_test.rb
         | 
| @@ -155,10 +158,12 @@ test_files: | |
| 155 158 | 
             
            - test/fixtures/serializers/valid_serializer.rb
         | 
| 156 159 | 
             
            - test/minitest_helper.rb
         | 
| 157 160 | 
             
            - test/objects/item_test.rb
         | 
| 161 | 
            +
            - test/objects/queries_test.rb
         | 
| 158 162 | 
             
            - test/objects/template_test.rb
         | 
| 159 163 | 
             
            - test/serializer/data_test.rb
         | 
| 160 164 | 
             
            - test/serializer/href_test.rb
         | 
| 161 165 | 
             
            - test/serializer/links_test.rb
         | 
| 166 | 
            +
            - test/serializer/queries_test.rb
         | 
| 162 167 | 
             
            - test/serializer/template_test.rb
         | 
| 163 168 | 
             
            - test/support/ext_test.rb
         | 
| 164 169 | 
             
            - test/support/support_test.rb
         |