Class: FlightStats::Resource

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

Defined Under Namespace

Classes: UnknownModel

Constant Summary collapse

@@string_to_model_cache_lock =

Performance optimizations

Mutex.new
@@string_to_model_cache =
{}
@@underscore_cache_lock =
Mutex.new
@@underscore_cache =
{}

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:



104
105
106
107
108
109
110
111
112
# 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

Instance Attribute Details

#attributesHash

Returns The raw hash of record attributes.

Returns:

  • (Hash)

    The raw hash of record attributes.



91
92
93
# File 'lib/flightstats/resource.rb', line 91

def attributes
  @attributes
end

#etagString? (readonly)

Returns An ETag for the current record.

Returns:

  • (String, nil)

    An ETag for the current record.



97
98
99
# File 'lib/flightstats/resource.rb', line 97

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.



94
95
96
# File 'lib/flightstats/resource.rb', line 94

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.



100
101
102
# File 'lib/flightstats/resource.rb', line 100

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:



31
32
33
34
35
# 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

.from_parsed_json(json, model_string) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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

.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:



19
20
21
22
23
# 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

.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:



42
43
44
45
46
47
48
49
50
51
# 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

.string_to_model_with_caching(model_string) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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

Instance Method Details

#to_paramObject



139
140
141
# File 'lib/flightstats/resource.rb', line 139

def to_param
  self[self.class.param_name]
end

#underscore_with_caching(input_string) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# 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