Class: FlightStats::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/flightstats/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) {|_self| ... } ⇒ Resource

Returns A new resource instance.

Parameters:

  • attributes (Hash) (defaults to: {})

    A hash of attributes.

Yields:

  • (_self)

Yield Parameters:



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

#attributesHash

Returns The raw hash of record attributes.

Returns:

  • (Hash)

    The raw hash of record attributes.



69
70
71
# File 'lib/flightstats/resource.rb', line 69

def attributes
  @attributes
end

#etagString? (readonly)

Returns An ETag for the current record.

Returns:

  • (String, nil)

    An ETag for the current record.



75
76
77
# File 'lib/flightstats/resource.rb', line 75

def etag
  @etag
end

#responseNet::HTTPResponse? (readonly)

Returns The most recent response object for the record.

Returns:

  • (Net::HTTPResponse, nil)

    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.

Returns:

  • (String, nil)

    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.

Parameters:

  • json (String)
  • response_key (String)

Returns:

See Also:



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.

Parameters:

  • response (Net::HTTPResponse)
  • response_key (String) (defaults to: nil)

Returns:



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.

Parameters:

  • model_string (String)

Returns:

  • (Resource)

    class corresponding to the string, nil if no model is associated with it

See Also:



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_paramObject



104
105
106
# File 'lib/flightstats/resource.rb', line 104

def to_param
  self[self.class.param_name]
end