class FlightStats::Resource
Attributes
@return [Hash] The raw hash of record attributes.
@return [String, nil] An ETag for the current record.
@return [Net::HTTPResponse, nil] The most recent response object for the record.
@return [String, nil] A writer to override the URI the record saves to.
Public Class Methods
Instantiates a record from a JSON blob.
@return [Resource] @param json [String] @param response_key [String] @see from_response
# File lib/flightstats/resource.rb, line 31 def from_json json, response_key model = nil raw = JSON.parse(json) response_key ? from_parsed_json(raw[response_key], response_key) : raw end
# File lib/flightstats/resource.rb, line 68 def from_parsed_json(json, model_string) # Optimization - native type, nothing to build so bail early if json.is_a? FalseClass or json.is_a? TrueClass or json.is_a? Fixnum or json.is_a? String return json elsif json.is_a? Array value = [] json.each do |one_value| value << from_parsed_json(one_value, model_string) end value else model = string_to_model_with_caching(model_string) if !model.nil? and !json.is_a? Hash json else model.nil? ? json : model.send("new", json) end end end
Instantiates a record from an HTTP response.
@return [Resource] @param response [Net::HTTPResponse] @param response_key [String]
# File lib/flightstats/resource.rb, line 19 def from_response response, response_key=nil record = from_json response.body, response_key record.instance_eval { @etag, @response = response['ETag'], response } record end
@return [Resource] A new resource instance. @param attributes [Hash] A hash of attributes.
# File lib/flightstats/resource.rb, line 104 def initialize attributes = {} if instance_of? Resource raise Error, "#{self.class} is an abstract class and cannot be instantiated" end @attributes, @uri, @href = {}, false, false self.attributes = attributes yield self if block_given? end
Given a string (key in the response json), find the right model.
@return [Resource] class corresponding to the string, nil if no model is associated with it @param model_string [String] @see from_response
# File lib/flightstats/resource.rb, line 42 def string_to_model(model_string) return nil if model_string.nil? begin class_name = Helper.classify(model_string) FlightStats.const_get(class_name) rescue NameError => e # FlightStats.logger.warn e nil end end
# File lib/flightstats/resource.rb, line 53 def string_to_model_with_caching(model_string) @@string_to_model_cache_lock.synchronize do model = @@string_to_model_cache[model_string] return nil if model == UnknownModel return model unless model.nil? # See if it's a series of objects (e.g. schedules) model = string_to_model(Helper.singularize(model_string)) model = string_to_model(model_string) if model.nil? @@string_to_model_cache[model_string] = model.nil? ? UnknownModel : model return model end end
Public Instance Methods
Apply a given hash of attributes to an object.
@return [Hash] @param attributes [Hash] A hash of attributes.
# File lib/flightstats/resource.rb, line 130 def attributes= attributes = {} return if attributes.nil? attributes.each_pair { |k, v| # Parse the nested attributed to instantiate the right model, is applicable value = FlightStats::Resource.from_parsed_json(v, k) send("#{underscore_with_caching(k)}=", value) rescue nil } end
# File lib/flightstats/resource.rb, line 139 def to_param self[self.class.param_name] end
# File lib/flightstats/resource.rb, line 114 def underscore_with_caching(input_string) @@underscore_cache_lock.synchronize do underscored = @@underscore_cache[input_string] return underscored unless underscored.nil? underscored = Helper.underscore(input_string) @@underscore_cache[input_string] = underscored return underscored end end