Class: Redd::Utilities::Unmarshaller

Inherits:
Object
  • Object
show all
Defined in:
lib/redd/utilities/unmarshaller.rb

Overview

Unmarshals hashes into objects.

Constant Summary collapse

MAPPING =

Contains the mapping from ‘kind’ strings to classes. TODO: UserList type!

{
  't1'           => Models::Comment,
  't2'           => Models::User,
  't3'           => Models::Submission,
  't4'           => Models::PrivateMessage,
  't5'           => Models::Subreddit,
  'more'         => Models::MoreComments,
  'wikipage'     => Models::WikiPage,
  'modaction'    => Models::Subreddit::ModAction,
  'LabeledMulti' => Models::Multireddit,
  'LiveUpdate'   => Models::LiveThread::LiveUpdate
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Unmarshaller

Returns a new instance of Unmarshaller.



22
23
24
# File 'lib/redd/utilities/unmarshaller.rb', line 22

def initialize(client)
  @client = client
end

Instance Method Details

#unmarshal(res) ⇒ Object



26
27
28
29
30
31
# File 'lib/redd/utilities/unmarshaller.rb', line 26

def unmarshal(res)
  # I'm loving the hell out of this pattern.
  model = js_listing(res) || js_model(res) || api_listing(res) || api_model(res)
  raise "cannot unmarshal: #{res.inspect}" if model.nil?
  model
end