Class: ComicVine::Resource
- Inherits:
-
Object
- Object
- ComicVine::Resource
- Defined in:
- lib/comicvine/resource.rb,
lib/comicvine/resource/resources.rb
Overview
Class container for a singular ComicVine resource
Direct Known Subclasses
Character, Chat, Concept, Episode, Issue, Location, Movie, Object, Origin, Person, Power, Promo, Publisher, Review, Series, StoryArc, Team, Type, Video, VideoCategory, VideoType, Volume
Defined Under Namespace
Classes: Character, Chat, Concept, Episode, Issue, Location, Movie, Object, Origin, Person, Power, Promo, Publisher, Review, Series, StoryArc, Team, Type, Video, VideoCategory, VideoType, Volume
Class Method Summary collapse
-
.create_resource(attr) ⇒ ComicVine::Resource
Takes hash and returns a subclass of Resource based on identified type.
-
.identify_and_update_response(response) ⇒ Symbol
Parses supplied api_detail_url and returns the translated type symbol.
-
.identify_type(api_detail_url) ⇒ Symbol
Parses supplied api_detail_url and returns the translated type symbol.
Instance Method Summary collapse
-
#fetch ⇒ ComicVine::Resource
Fetches data from ComicVine based on objects api_detail_url.
-
#fetch! ⇒ ComicVine::Resource
Fetches data from ComicVine based on objects api_detail_url and updates self with new values.
-
#initialize(args) ⇒ Resource
constructor
A new instance of Resource.
- #method_missing(method_sym, *arguments, &block) ⇒ Object
-
#resource_type ⇒ Symbol
Identifies objects resource type based on its ComicVine api_detail_url.
Constructor Details
#initialize(args) ⇒ Resource
Returns a new instance of Resource.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/comicvine/resource.rb', line 8 def initialize(args) args.each do |k, v| # Add an attribute reader for each key # noinspection RubyResolve self.class.class_eval { attr_reader k } unless self.respond_to? k # Convert sub arrays to objects v.collect! { |i| ComicVine::Resource::create_resource(i) } if v.kind_of?(Array) && !v.empty? && v.first.key?('api_detail_url') # Convert sub hashes to objects if v.kind_of?(Hash) && v.key?('api_detail_url') v = ComicVine::Resource::create_resource(v) end # Set the instance variable to value instance_variable_set "@#{k}", v end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *arguments, &block) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/comicvine/resource.rb', line 64 def method_missing(method_sym, *arguments, &block) if method_sym.to_s =~ /^get_(.*)$/ key = method_sym.to_s.sub 'get_', '' if instance_variable_defined?("@#{key}") item = instance_variable_get("@#{key}") if item.kind_of?(Array) && item.first.kind_of?(Resource) res = [] item.each do |i| res << i.fetch end return res end if item.kind_of?(Resource) return item.fetch end else super end elsif super end end |
Class Method Details
.create_resource(attr) ⇒ ComicVine::Resource
Takes hash and returns a subclass of ComicVine::Resource based on identified type
103 104 105 |
# File 'lib/comicvine/resource.rb', line 103 def create_resource(attr) ComicVine::Resource.new attr end |
.identify_and_update_response(response) ⇒ Symbol
Parses supplied api_detail_url and returns the translated type symbol
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/comicvine/resource.rb', line 113 def identify_and_update_response(response) type = ComicVine::Resource::identify_type(response['api_detail_url']) case type when :first_appeared_in_issue type = :issue response['api_detail_url'].to_s.sub! 'first_appeared_in_issue', 'issue' when :thing type = :object response['api_detail_url'].to_s.sub! 'thing', 'object' when :creator type = :person response['api_detail_url'].to_s.sub! 'creator', 'person' when :writer type = :person response['api_detail_url'].to_s.sub! 'writer', 'person' when :studio type = :publisher response['api_detail_url'].to_s.sub! 'studio', 'publisher' when :arc type = :story_arc response['api_detail_url'].to_s.sub! 'arc', 'story_arc' else end type end |
.identify_type(api_detail_url) ⇒ Symbol
Parses supplied api_detail_url and returns the translated type symbol
147 148 149 150 151 152 153 |
# File 'lib/comicvine/resource.rb', line 147 def identify_type(api_detail_url) if api_detail_url.to_s =~ /comicvine\.gamespot\.com\/api\/(\w+)\/?/ $1.to_s.to_sym else nil end end |
Instance Method Details
#fetch ⇒ ComicVine::Resource
Fetches data from ComicVine based on objects api_detail_url
32 33 34 |
# File 'lib/comicvine/resource.rb', line 32 def fetch ComicVine::API.get_details_by_url(self.api_detail_url) end |
#fetch! ⇒ ComicVine::Resource
Fetches data from ComicVine based on objects api_detail_url and updates self with new values
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/comicvine/resource.rb', line 41 def fetch! obj = ComicVine::API.get_details_by_url(self.api_detail_url) self.methods.each do |m| # Look for methods that can be set (i.e. ends with a =) if m.to_s =~ /^(?!_)([\w\d\_]+)=$/ # Save our method symbols in a more readable fashion get_m = $1.to_sym set_m = m # Skip setting ids, as this should be handled by the classes when setting the children objects if needed next if get_m.to_s =~ /\_ids?$/ || set_m.to_s =~ /attributes/ # Verify the new object has a get method and it is not nil or empty if self.respond_to?(set_m) && obj.respond_to?(get_m) && !obj.method(get_m).call.nil? # Now set the method to the new value self.method(set_m).call obj.method(get_m).call end end end self end |