Sha256: 6282f4fa54e7a1a51e420006094186b8188a3b6c6b2b011d4838b1c27f92c6fd

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module Redd
  module Utilities
    # Unmarshals hashes into objects.
    class Unmarshaller
      # Contains the mapping from 'kind' strings to classes.
      # TODO: UserList type!
      MAPPING = {
        't1'           => Models::Comment,
        't2'           => Models::User,
        't3'           => Models::Submission,
        't4'           => Models::PrivateMessage,
        't5'           => Models::Subreddit,
        'more'         => Models::MoreComments,
        'wikipage'     => Models::WikiPage,
        'Listing'      => Models::Listing,
        'modaction'    => Models::Subreddit::ModAction,
        'LabeledMulti' => Models::Multireddit,
        'LiveUpdate'   => Models::LiveThread::LiveUpdate
      }.freeze

      def initialize(client)
        @client = client
      end

      def unmarshal(response)
        if response[:json] && response[:json][:data]
          if response[:json][:data][:things]
            Models::Listing.new(@client, children: response[:json][:data][:things])
          else
            Models::BasicModel.new(@client, response[:json][:data])
          end
        elsif MAPPING.key?(response[:kind])
          MAPPING[response[:kind]].new(@client, response[:data])
        else
          raise "unknown type to unmarshal: #{response[:kind].inspect}"
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
redd-0.8.4 lib/redd/utilities/unmarshaller.rb
redd-0.8.3 lib/redd/utilities/unmarshaller.rb
redd-0.8.2 lib/redd/utilities/unmarshaller.rb