Class: ComicVine::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/comicvine/mongo.rb

Defined Under Namespace

Classes: Character, Concept, Episode, Issue, Location, Movie, Object, Origin, Person, Power, Promo, Publisher, Series, StoryArc, Team, Volume

Class Method Summary collapse

Class Method Details

.create_resource(attr) ⇒ Resource::Character, ...

Takes hash and returns a Mongoid::Document subclass of ComicVine::Resource based on identified type



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/comicvine/mongo.rb', line 132

def self.create_resource(attr)
  type = ComicVine::Resource::identify_and_update_response(attr)

  if type

    # if its a review the api doesnt actually exist, so return the hash
    return attr if type.equal? :review

    c = Object.class_eval('ComicVine::Resource::' + type.to_s.camelcase)

    if c.where(id: attr['id']).exists?
      c.find(attr['id'])
    else
      c.new attr
    end
  else
    raise ScriptError, 'Unknown type for api_detail_url: ' + attr['api_detail_url']
  end
end