Facebook
From Violet Mockingbird, 4 Years ago, written in Ruby.
This paste is a reply to ruby challenge from mohmed sayed - view diff
Embed
Download Paste or View Raw
Hits: 218
  1. # frozen_string_literal: true
  2.  
  3. # The IncludedResourceParams class is responsible for parsing a string containing
  4. # a comma separated list of associated resources to include with a request. See
  5. # http://jsonapi.org/format/#fetching-includes for additional details although
  6. # this is not required knowledge for the task at hand.
  7. #
  8. # Our API requires specific inclusion of related resourses - that is we do NOT
  9. # want to support wildcard inclusion (e.g. `foo.*`)
  10. #
  11. # The IncludedResourceParams class has three public methods making up its API.
  12. #
  13. # [included_resources]
  14. #   returns an array of non-wildcard included elements.
  15. # [has_included_resources?]
  16. #   Returns true if our supplied param has included resources, false otherwise.
  17. # [model_includes]
  18. #   returns an array suitable to supply to ActiveRecord's `includes` method
  19. #   (http://guides.rubyonrails.org/active_record_querying.html#eager-loading-multiple-associations)
  20. #   The included_resources should be transformed as specified in the unit tests
  21. #   included herein.
  22. #
  23. # All three public methods have unit tests written below that must pass. You are
  24. # free to add additional classes/modules as necessary and/or private methods
  25. # within the IncludedResourceParams class.
  26. #
  27. # Feel free to use the Ruby standard libraries available in your
  28. # solution.
  29. #
  30. class IncludedResourceParams
  31.   def initialize(include_param)
  32.     @include_param = include_param
  33.   end
  34.  
  35.   ##
  36.   # Does our IncludedResourceParams instance actually have any valid included
  37.   # resources after parsing?
  38.   #
  39.   # @return [Boolean] whether this instance has included resources
  40.   def parse
  41.     array = @include_param ? @include_param.split(',') : []
  42.     array = array.reject do |element|
  43.       element.include? '*'
  44.     end
  45.   end
  46.  
  47.   def has_included_resources?
  48.     !parse.empty?
  49.   end
  50.  
  51.   ##
  52.   # Fetches the included resourcs as an Array containing only non-wildcard
  53.   # resource specifiers.
  54.   #
  55.   # @example nil
  56.   #   IncludedResourceParams.new(nil).included_resources => []
  57.   #
  58.   # @example "foo,foo.bar,baz.*"
  59.   #   IncludedResourceParams.new("foo,bar,baz.*").included_resources => ["foo", "bar"]
  60.   #
  61.   # @return [Array] an Array of Strings parsed from the include param with
  62.   # wildcard includes removed
  63.   def included_resources
  64.     parse
  65.     # TODO: implement me
  66.   end
  67.  
  68.   ##
  69.   # Converts the resources to be included from their JSONAPI representation to
  70.   # a structure compatible with ActiveRecord's `includes` methods. This can/should
  71.   # be an Array in all cases. Does not do any verification that the resources
  72.   # specified for inclusion are actual ActiveRecord classes.
  73.   #
  74.   # @example nil
  75.   #   IncludedResourceParams.new(nil).model_includes => []
  76.   #
  77.   # @example "foo"
  78.   #   IncludedResourceParams.new("foo").model_includes => [:foo]
  79.   #
  80.   # @see Following unit tests
  81.   #
  82.   # @return [Array] an Array of Symbols and/or Hashes compatible with ActiveRecord
  83.   # `includes`
  84.   def model_includes
  85.     # TODO: implement me
  86.     ret = included_resources.map do |element|
  87.     if (element.include? '.')
  88.      to_active_record element.split '.'
  89.     else
  90.      element.to_sym
  91.     end
  92.    end  
  93.      
  94.   end
  95.  
  96.   # rec function that convert  to nested object
  97.   def to_active_record(elements );
  98.      if elements.length == 1
  99.      return [elements[0].to_sym]
  100.      end
  101.  
  102.     {elements[0].to_sym => to_active_record(elements[1..elements.length])}
  103.    end
  104. end
  105.