Sha256: 82284941d450ec9df35ec85950fe17e5908453128578d4bf5ac4b750f9cd2a95

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module Redd
  module Utilities
    # Unmarshals hashes into objects.
    class Unmarshaller
      # Contains the mapping from 'kind' strings to classes.
      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,
        'LabeledMulti' => Models::Multireddit
      }.freeze

      def initialize(client)
        @client = client
      end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redd-0.8.0.pre.2 lib/redd/utilities/unmarshaller.rb
redd-0.8.0.pre.1 lib/redd/utilities/unmarshaller.rb