hal_api-rails 0.2.1 → 0.2.2
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/hal_api-rails.gemspec +1 -0
- data/lib/hal_api/controller/actions.rb +8 -1
- data/lib/hal_api/controller/exceptions.rb +1 -1
- data/lib/hal_api/controller/resources.rb +6 -0
- data/lib/hal_api/rails.rb +3 -5
- data/lib/hal_api/rails/version.rb +1 -1
- data/lib/hal_api/representer.rb +6 -1
- data/lib/hal_api/representer/caches.rb +1 -0
- data/lib/hal_api/representer/embeds.rb +6 -6
- data/lib/hal_api/representer/link_serialize.rb +2 -2
- data/lib/hal_api/representer/uri_methods.rb +9 -6
- metadata +16 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 210b5c17cacf07522c44de62e2ac3aa6a10120df
         | 
| 4 | 
            +
              data.tar.gz: 6e2b59bc821a8b40043e796637e9b350f8a34323
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 7e69e13451aca32729cb733846becf67d49fbd1c131c7d252e5af275517163a1e8211e08e8fff6a3983f39f85f5ee87c1dca22453b42542729ec9784982e84bb
         | 
| 7 | 
            +
              data.tar.gz: a09e666bb380e0753d32ae9dff0dbfdbda31fc4557070fb8224f1dcc81fc41418150bd544905fdfefd931131a6551743902c0a87b682618e1cbe315e1a023e4c
         | 
    
        data/hal_api-rails.gemspec
    CHANGED
    
    
| @@ -12,7 +12,7 @@ module HalApi::Controller::Actions | |
| 12 12 | 
             
                  consume_with_content_type! res
         | 
| 13 13 | 
             
                  hal_authorize res
         | 
| 14 14 | 
             
                  res.save!
         | 
| 15 | 
            -
                  respond_with root_resource(res),  | 
| 15 | 
            +
                  respond_with root_resource(res), create_options
         | 
| 16 16 | 
             
                end
         | 
| 17 17 | 
             
              end
         | 
| 18 18 |  | 
| @@ -42,6 +42,13 @@ module HalApi::Controller::Actions | |
| 42 42 | 
             
              def index_options
         | 
| 43 43 | 
             
                valid_params_for_action(:index).tap do |options|
         | 
| 44 44 | 
             
                  options[:_keys] = options.keys
         | 
| 45 | 
            +
                  options[:represent_with] = Api::PagedCollectionRepresenter
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
              end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
              def create_options
         | 
| 50 | 
            +
                show_options.tap do |options|
         | 
| 51 | 
            +
                  options[:location] = ''
         | 
| 45 52 | 
             
                end
         | 
| 46 53 | 
             
              end
         | 
| 47 54 |  | 
| @@ -17,7 +17,7 @@ module HalApi::Controller::Exceptions | |
| 17 17 | 
             
              end
         | 
| 18 18 |  | 
| 19 19 | 
             
              def log_error(env, wrapper)
         | 
| 20 | 
            -
                logger = env['action_dispatch.logger'] || ActiveSupport::Logger.new($stderr)
         | 
| 20 | 
            +
                logger = env['action_dispatch.logger'] || self.logger || ActiveSupport::Logger.new($stderr)
         | 
| 21 21 | 
             
                return unless logger
         | 
| 22 22 |  | 
| 23 23 | 
             
                exception = wrapper.exception
         | 
| @@ -14,10 +14,14 @@ module HalApi::Controller::Resources | |
| 14 14 |  | 
| 15 15 | 
             
              def show_resource
         | 
| 16 16 | 
             
                resource
         | 
| 17 | 
            +
              rescue ::ActiveRecord::RecordNotFound
         | 
| 18 | 
            +
                raise HalApi::Errors::NotFound.new
         | 
| 17 19 | 
             
              end
         | 
| 18 20 |  | 
| 19 21 | 
             
              def update_resource
         | 
| 20 22 | 
             
                resource
         | 
| 23 | 
            +
              rescue ::ActiveRecord::RecordNotFound
         | 
| 24 | 
            +
                raise HalApi::Errors::NotFound.new
         | 
| 21 25 | 
             
              end
         | 
| 22 26 |  | 
| 23 27 | 
             
              def create_resource
         | 
| @@ -26,6 +30,8 @@ module HalApi::Controller::Resources | |
| 26 30 |  | 
| 27 31 | 
             
              def destroy_resource
         | 
| 28 32 | 
             
                resource
         | 
| 33 | 
            +
              rescue ::ActiveRecord::RecordNotFound
         | 
| 34 | 
            +
                raise HalApi::Errors::NotFound.new
         | 
| 29 35 | 
             
              end
         | 
| 30 36 |  | 
| 31 37 | 
             
              def resource
         | 
    
        data/lib/hal_api/rails.rb
    CHANGED
    
    | @@ -6,10 +6,8 @@ module HalApi::Rails | |
| 6 6 |  | 
| 7 7 | 
             
              Mime::Type.register 'application/hal+json', :hal
         | 
| 8 8 |  | 
| 9 | 
            -
               | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
                self.content_type ||= Mime::HAL
         | 
| 13 | 
            -
                js.to_json
         | 
| 9 | 
            +
              ::ActionController::Renderers.add :hal do |obj, options|
         | 
| 10 | 
            +
                self.content_type ||= Mime[:hal]
         | 
| 11 | 
            +
                obj
         | 
| 14 12 | 
             
              end
         | 
| 15 13 | 
             
            end
         | 
    
        data/lib/hal_api/representer.rb
    CHANGED
    
    | @@ -1,6 +1,12 @@ | |
| 1 1 | 
             
            # encoding: utf-8
         | 
| 2 2 |  | 
| 3 3 | 
             
            class HalApi::Representer < Roar::Decorator
         | 
| 4 | 
            +
              include Roar::Hypermedia
         | 
| 5 | 
            +
              include Roar::JSON::HAL
         | 
| 6 | 
            +
              include Roar::JSON::HAL::Links
         | 
| 7 | 
            +
              include Roar::JSON
         | 
| 8 | 
            +
              require 'roar/rails/hal'
         | 
| 9 | 
            +
             | 
| 4 10 | 
             
              require 'hal_api/representer/caches'
         | 
| 5 11 | 
             
              require 'hal_api/representer/curies'
         | 
| 6 12 | 
             
              require 'hal_api/representer/embeds'
         | 
| @@ -8,7 +14,6 @@ class HalApi::Representer < Roar::Decorator | |
| 8 14 | 
             
              require 'hal_api/representer/link_serialize'
         | 
| 9 15 | 
             
              require 'hal_api/representer/uri_methods'
         | 
| 10 16 |  | 
| 11 | 
            -
              include Roar::JSON::HAL
         | 
| 12 17 | 
             
              include HalApi::Representer::FormatKeys
         | 
| 13 18 | 
             
              include HalApi::Representer::UriMethods
         | 
| 14 19 | 
             
              include HalApi::Representer::Curies
         | 
| @@ -13,6 +13,7 @@ module HalApi::Representer::Caches | |
| 13 13 | 
             
                super(options)
         | 
| 14 14 | 
             
              end
         | 
| 15 15 |  | 
| 16 | 
            +
              # This isn't working, comment out this optimization
         | 
| 16 17 | 
             
              # def create_representation_with(doc, options, format)
         | 
| 17 18 | 
             
              #   cache.fetch(cache_key(represented, options), cache_options) do
         | 
| 18 19 | 
             
              #     response = super(doc, options, format)
         | 
| @@ -1,4 +1,6 @@ | |
| 1 | 
            +
            # encoding: utf-8
         | 
| 1 2 | 
             
            require 'active_support/concern'
         | 
| 3 | 
            +
            require 'hal_api/paged_collection'
         | 
| 2 4 |  | 
| 3 5 | 
             
            # expects underlying model to have filename, class, and id attributes
         | 
| 4 6 | 
             
            module HalApi::Representer::Embeds
         | 
| @@ -61,14 +63,12 @@ module HalApi::Representer::Embeds | |
| 61 63 | 
             
                      no_curies: true,
         | 
| 62 64 | 
             
                      item_class: options.delete(:item_class),
         | 
| 63 65 | 
             
                      url: options.delete(:url),
         | 
| 64 | 
            -
                      item_decorator: options.delete(:item_decorator) | 
| 65 | 
            -
                      per: options.delete(:per) || Kaminari.config.default_per_page
         | 
| 66 | 
            +
                      item_decorator: options.delete(:item_decorator)
         | 
| 66 67 | 
             
                    }
         | 
| 68 | 
            +
                    getter_per = options.delete(:per) || Kaminari.config.default_per_page
         | 
| 67 69 | 
             
                    options[:getter] ||= ->(*) do
         | 
| 68 | 
            -
                       | 
| 69 | 
            -
                      per  | 
| 70 | 
            -
                      per = self.send(name).count if per == :all
         | 
| 71 | 
            -
                      PagedCollection.new(self.send(name).page(1).per(per), nil, opts.merge({parent: self}))
         | 
| 70 | 
            +
                      per = getter_per == :all ? send(name).count : getter_per
         | 
| 71 | 
            +
                      HalApi::PagedCollection.new(send(name).page(1).per(per), nil, opts.merge(parent: self))
         | 
| 72 72 | 
             
                    end
         | 
| 73 73 | 
             
                    options[:decorator] = Api::PagedCollectionRepresenter
         | 
| 74 74 | 
             
                  end
         | 
| @@ -13,8 +13,8 @@ module HalApi::Representer::LinkSerialize | |
| 13 13 |  | 
| 14 14 | 
             
                def set_link_property(options)
         | 
| 15 15 | 
             
                  if options.is_a?(Hash) && (options.delete(:writeable) || options[:reader])
         | 
| 16 | 
            -
                    name | 
| 17 | 
            -
                    pname | 
| 16 | 
            +
                    name = options[:rel].to_s.split(':').last.split('/').last
         | 
| 17 | 
            +
                    pname = "set_#{name}_uri"
         | 
| 18 18 | 
             
                    reader = options.delete(:reader) || ->(doc, _args) do
         | 
| 19 19 | 
             
                      try("#{name}_id=", id_from_url(doc[pname])) if doc[pname]
         | 
| 20 20 | 
             
                    end
         | 
| @@ -1,5 +1,4 @@ | |
| 1 1 | 
             
            # encoding: utf-8
         | 
| 2 | 
            -
             | 
| 3 2 | 
             
            require 'active_support/concern'
         | 
| 4 3 |  | 
| 5 4 | 
             
            # expects underlying model to have filename, class, and id attributes
         | 
| @@ -13,13 +12,13 @@ module HalApi::Representer::UriMethods | |
| 13 12 | 
             
                  link(:self) do
         | 
| 14 13 | 
             
                    {
         | 
| 15 14 | 
             
                      href: self_url(represented),
         | 
| 16 | 
            -
                      profile:  | 
| 15 | 
            +
                      profile: profile_url(represented)
         | 
| 17 16 | 
             
                    }
         | 
| 18 17 | 
             
                  end
         | 
| 19 18 | 
             
                end
         | 
| 20 19 |  | 
| 21 20 | 
             
                def profile_link
         | 
| 22 | 
            -
                  link(:profile) {  | 
| 21 | 
            +
                  link(:profile) { profile_url(represented) }
         | 
| 23 22 | 
             
                end
         | 
| 24 23 |  | 
| 25 24 | 
             
                def alternate_link
         | 
| @@ -57,7 +56,7 @@ module HalApi::Representer::UriMethods | |
| 57 56 | 
             
                "http://#{self.class.profile_host}/model/#{joined_names(args)}"
         | 
| 58 57 | 
             
              end
         | 
| 59 58 |  | 
| 60 | 
            -
              alias_method : | 
| 59 | 
            +
              alias_method :profile_url, :model_uri
         | 
| 61 60 |  | 
| 62 61 | 
             
              def joined_names(args)
         | 
| 63 62 | 
             
                (Array(args.map { |arg| model_uri_part_to_string(arg) }) +
         | 
| @@ -67,7 +66,11 @@ module HalApi::Representer::UriMethods | |
| 67 66 | 
             
              def model_uri_suffix(args)
         | 
| 68 67 | 
             
                represented = args.last
         | 
| 69 68 | 
             
                klass = represented.try(:item_decorator) || self.class
         | 
| 70 | 
            -
                klass. | 
| 69 | 
            +
                find_model_name(klass).deconstantize.underscore.dasherize.split('/')[1..-1] || []
         | 
| 70 | 
            +
              end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
              def find_model_name(klass)
         | 
| 73 | 
            +
                klass.try(:name) || klass.ancestors.detect{|c| c.try(:name) }.name
         | 
| 71 74 | 
             
              end
         | 
| 72 75 |  | 
| 73 76 | 
             
              def model_uri_part_to_string(part)
         | 
| @@ -75,7 +78,7 @@ module HalApi::Representer::UriMethods | |
| 75 78 | 
             
                  part.to_s.dasherize
         | 
| 76 79 | 
             
                else
         | 
| 77 80 | 
             
                  klass = part.is_a?(Class) ? part : (part.try(:item_class) || part.class)
         | 
| 78 | 
            -
                  if klass.respond_to?(:base_class) && (klass.superclass != ActiveRecord::Base)
         | 
| 81 | 
            +
                  if klass.respond_to?(:base_class) && (klass.superclass != ActiveRecord::Base) && !klass.superclass.abstract_class
         | 
| 79 82 | 
             
                    base = klass.superclass.name.underscore.dasherize
         | 
| 80 83 | 
             
                    child = klass.name.underscore.gsub(/_#{base}$/, "").dasherize
         | 
| 81 84 | 
             
                    [base, child]
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: hal_api-rails
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.2. | 
| 4 | 
            +
              version: 0.2.2
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Chris Rhoden
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: exe
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2016- | 
| 12 | 
            +
            date: 2016-04-20 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: activemodel
         | 
| @@ -151,6 +151,20 @@ dependencies: | |
| 151 151 | 
             
                - - ">="
         | 
| 152 152 | 
             
                  - !ruby/object:Gem::Version
         | 
| 153 153 | 
             
                    version: '0'
         | 
| 154 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 155 | 
            +
              name: kaminari
         | 
| 156 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 157 | 
            +
                requirements:
         | 
| 158 | 
            +
                - - ">="
         | 
| 159 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 160 | 
            +
                    version: '0'
         | 
| 161 | 
            +
              type: :development
         | 
| 162 | 
            +
              prerelease: false
         | 
| 163 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 164 | 
            +
                requirements:
         | 
| 165 | 
            +
                - - ">="
         | 
| 166 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 167 | 
            +
                    version: '0'
         | 
| 154 168 | 
             
            description: JSON HAL APIs on Rails in the style of PRX v4. Uses ROAR
         | 
| 155 169 | 
             
            email:
         | 
| 156 170 | 
             
            - carhoden@gmail.com
         |