Sha256: c6cf312b3da01e920206dd0a732a939d1e96861fa96fa06bfc36e7b62c961394

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

module Panda
  module Router
    DEFAULT_FORMAT = "json"
    VAR_PATTERN = /:\w+/

    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods

      def resource_path
        @url || "/#{end_class_name.downcase}s"
      end

      def match(url)
        @url = url
      end

      def many_path
        resource_path
      end

      def one_path
        resource_path + "/:id"
      end

      def build_hash_many_path(end_path, relation_attr)
        relation_class_name = relation_attr[0..relation_attr.rindex("_id")-1].capitalize
        prefix_path = Panda::const_get(relation_class_name).resource_path + "/:" + relation_attr
        prefix_path + end_path
      end

      def object_url(url, map)
        full_object_url(url.clone.gsub(VAR_PATTERN){|key| map[key[1..-1].to_sym] || map[key[1..-1].to_s]})
      end

      def element_params(url, map)
        params = map.clone
        url.clone.scan(VAR_PATTERN).map{|key| params.reject!{|k,v| k==key[1..-1] } }
        params
      end

      def full_object_url(url)
        url + ".#{DEFAULT_FORMAT}"
      end

    end

    def object_url_map(url)
      self.class.full_object_url(url.clone.gsub(VAR_PATTERN) {|key| send(key[1..-1].to_sym)})
    end

  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
panda-1.1.0 lib/panda/modules/router.rb
faraday-panda-1.1.0 lib/panda/modules/router.rb
panda-1.0.0 lib/panda/modules/router.rb