Sha256: 275a4312e9230528fe63d5ffb68a45b9527df910fd8adb22e289fb0a035aa6ed

Contents?: true

Size: 1019 Bytes

Versions: 5

Compression:

Stored size: 1019 Bytes

Contents

module TMDBParty
  class Image
    def initialize(attributes)
      @attributes = attributes
    end
    
    def id
      @attributes['id']
    end
    
    def type
      @attributes['type'].downcase.to_sym
    end
    
    def sizes
      @attributes['sizes'].map { |size| size.downcase.to_sym }.to_set
    end
    
    def url
      original_url
    end
    
    def method_missing(*args, &block)
      if args.first.to_s =~ /\A(.*)_url\Z/
        @attributes["#{$1}_url"]
      else
        super
      end
    end
    
    class << self
      def parse(data)
        data.map { |row| row['image'] }.group_by { |row| row['id'] }.map do |id, images|
          Image.new(reduce_images(images))
        end
      end
      
      protected
        def reduce_images(images)
          images.inject({'sizes' => []}) do |image, row|
            image["#{row['size']}_url"] = row.delete('url')
            image['sizes'] << row.delete('size')
            image.merge(row)
          end
        end
    end
    
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tmdb_party-0.9.0 lib/tmdb_party/image.rb
tmdb_party-0.8.0 lib/tmdb_party/image.rb
tmdb_party-0.7.0 lib/tmdb_party/image.rb
tmdb_party-0.6.0 lib/tmdb_party/image.rb
tmdb_party-0.5.0 lib/tmdb_party/image.rb