class ComicVine::Resource

Public Class Methods

create_resource(attr) click to toggle source

Takes hash and returns a {www.rubydoc.info/github/mongoid/mongoid/Mongoid/Document Mongoid::Document} subclass of {ComicVine::Resource} based on identified type

@example

ComicVine::Resource.create_resource(hash) #=> #<ComicVine::Resource::Issue:0x007fa6a427bbe8>

@param attr [Hash] @macro return.sub_resources @since 0.1.0

# 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