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!

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

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Unmarshaller

Returns a new instance of Unmarshaller.


24
25
26
# File 'lib/redd/utilities/unmarshaller.rb', line 24

def initialize(client)
  @client = client
end

Instance Method Details

#unmarshal(res) ⇒ Object


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

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