Class: FlightStats::Resource
- Inherits:
-
Object
- Object
- FlightStats::Resource
- Defined in:
- lib/flightstats/resource.rb
Direct Known Subclasses
AbstractDate, Airline, Airport, AirportResources, Appendix, Codeshare, Equipment, ExtendedOptions, Flight, FlightDurations, FlightEquipment, FlightId, FlightLeg, FlightStatus, OperationalTimes, Operator, Schedule
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
The raw hash of record attributes.
-
#etag ⇒ String?
readonly
An ETag for the current record.
-
#response ⇒ Net::HTTPResponse?
readonly
The most recent response object for the record.
-
#uri ⇒ String?
writeonly
A writer to override the URI the record saves to.
Class Method Summary collapse
-
.from_json(json, response_key) ⇒ Resource
Instantiates a record from a JSON blob.
- .from_parsed_json(json, model_string) ⇒ Object
-
.from_response(response, response_key = nil) ⇒ Resource
Instantiates a record from an HTTP response.
-
.string_to_model(model_string) ⇒ Resource
Given a string (key in the response json), find the right model.
Instance Method Summary collapse
-
#initialize(attributes = {}) {|_self| ... } ⇒ Resource
constructor
A new resource instance.
- #to_param ⇒ Object
Constructor Details
#initialize(attributes = {}) {|_self| ... } ⇒ Resource
Returns A new resource instance.
82 83 84 85 86 87 88 89 90 |
# File 'lib/flightstats/resource.rb', line 82 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 |
Instance Attribute Details
#attributes ⇒ Hash
Returns The raw hash of record attributes.
69 70 71 |
# File 'lib/flightstats/resource.rb', line 69 def attributes @attributes end |
#etag ⇒ String? (readonly)
Returns An ETag for the current record.
75 76 77 |
# File 'lib/flightstats/resource.rb', line 75 def etag @etag end |
#response ⇒ Net::HTTPResponse? (readonly)
Returns The most recent response object for the record.
72 73 74 |
# File 'lib/flightstats/resource.rb', line 72 def response @response end |
#uri=(value) ⇒ String? (writeonly)
Returns A writer to override the URI the record saves to.
78 79 80 |
# File 'lib/flightstats/resource.rb', line 78 def uri=(value) @uri = value end |
Class Method Details
.from_json(json, response_key) ⇒ Resource
Instantiates a record from a JSON blob.
24 25 26 27 28 |
# File 'lib/flightstats/resource.rb', line 24 def from_json json, response_key model = nil raw = JSON.parse(json) response_key ? from_parsed_json(raw[response_key], response_key) : raw end |
.from_parsed_json(json, model_string) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/flightstats/resource.rb', line 46 def from_parsed_json(json, model_string) if json.is_a? Array value = [] json.each do |one_value| value << from_parsed_json(one_value, model_string) end value else 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? if !model.nil? and !json.is_a? Hash json else model.nil? ? json : model.send("new", json) end end end |
.from_response(response, response_key = nil) ⇒ Resource
Instantiates a record from an HTTP response.
12 13 14 15 16 |
# File 'lib/flightstats/resource.rb', line 12 def from_response response, response_key=nil record = from_json response.body, response_key record.instance_eval { @etag, @response = response['ETag'], response } record end |
.string_to_model(model_string) ⇒ Resource
Given a string (key in the response json), find the right model.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/flightstats/resource.rb', line 35 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 |
Instance Method Details
#to_param ⇒ Object
104 105 106 |
# File 'lib/flightstats/resource.rb', line 104 def to_param self[self.class.param_name] end |