flightstats-flex 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -8,7 +8,7 @@ Ruby client for the FlightStats Flex API.
8
8
  In your Gemfile:
9
9
 
10
10
  ```ruby
11
- gem "flightstats-flex", "~> 0.2.0"
11
+ gem "flightstats-flex", "~> 0.3.0"
12
12
  ```
13
13
 
14
14
  or
@@ -16,7 +16,8 @@ module FlightStats
16
16
  :layover_duration_minutes,
17
17
  :operator,
18
18
  :carrier_fs_code,
19
- :wetlease_info
19
+ :wetlease_info,
20
+ :stop_codes
20
21
 
21
22
  def to_s
22
23
  "#{operator ? operator.carrier_fs_code : carrier_fs_code}#{flight_number}: #{departure_airport_fs_code} at #{departure_time} - #{arrival_airport_fs_code} at #{arrival_time} (+#{arrival_date_adjustment}) #{distance_miles} miles"
@@ -3,6 +3,13 @@ require 'json'
3
3
 
4
4
  module FlightStats
5
5
  class Resource
6
+ # Performance optimizations
7
+ @@string_to_model_cache_lock = Mutex.new
8
+ @@string_to_model_cache = {}
9
+ class UnknownModel ; end
10
+ @@underscore_cache_lock = Mutex.new
11
+ @@underscore_cache = {}
12
+
6
13
  class << self
7
14
  # Instantiates a record from an HTTP response.
8
15
  #
@@ -43,18 +50,33 @@ module FlightStats
43
50
  end
44
51
  end
45
52
 
53
+ def string_to_model_with_caching(model_string)
54
+ @@string_to_model_cache_lock.synchronize do
55
+ model = @@string_to_model_cache[model_string]
56
+ return nil if model == UnknownModel
57
+ return model unless model.nil?
58
+
59
+ # See if it's a series of objects (e.g. schedules)
60
+ model = string_to_model(Helper.singularize(model_string))
61
+ model = string_to_model(model_string) if model.nil?
62
+
63
+ @@string_to_model_cache[model_string] = model.nil? ? UnknownModel : model
64
+ return model
65
+ end
66
+ end
67
+
46
68
  def from_parsed_json(json, model_string)
47
- if json.is_a? Array
69
+ # Optimization - native type, nothing to build so bail early
70
+ if json.is_a? FalseClass or json.is_a? TrueClass or json.is_a? Fixnum or json.is_a? String
71
+ return json
72
+ elsif json.is_a? Array
48
73
  value = []
49
74
  json.each do |one_value|
50
75
  value << from_parsed_json(one_value, model_string)
51
76
  end
52
77
  value
53
78
  else
54
- model = nil
55
- # See if it's a series of objects (e.g. schedules)
56
- model = string_to_model(Helper.singularize(model_string))
57
- model = string_to_model(model_string) if model.nil?
79
+ model = string_to_model_with_caching(model_string)
58
80
 
59
81
  if !model.nil? and !json.is_a? Hash
60
82
  json
@@ -89,6 +111,18 @@ module FlightStats
89
111
  yield self if block_given?
90
112
  end
91
113
 
114
+ def underscore_with_caching(input_string)
115
+ @@underscore_cache_lock.synchronize do
116
+ underscored = @@underscore_cache[input_string]
117
+ return underscored unless underscored.nil?
118
+
119
+ underscored = Helper.underscore(input_string)
120
+
121
+ @@underscore_cache[input_string] = underscored
122
+ return underscored
123
+ end
124
+ end
125
+
92
126
  # Apply a given hash of attributes to an object.
93
127
  #
94
128
  # @return [Hash]
@@ -96,8 +130,9 @@ module FlightStats
96
130
  def attributes= attributes = {}
97
131
  return if attributes.nil?
98
132
  attributes.each_pair { |k, v|
133
+ # Parse the nested attributed to instantiate the right model, is applicable
99
134
  value = FlightStats::Resource.from_parsed_json(v, k)
100
- respond_to?(name = "#{Helper.underscore k}=") and send(name, value)
135
+ send("#{underscore_with_caching(k)}=", value) rescue nil
101
136
  }
102
137
  end
103
138
 
@@ -1,7 +1,7 @@
1
1
  module FlightStats
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 2
4
+ MINOR = 3
5
5
  PATCH = 0
6
6
  PRE = nil
7
7
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flightstats-flex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-07 00:00:00.000000000 Z
12
+ date: 2013-05-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -122,7 +122,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
122
122
  version: '0'
123
123
  segments:
124
124
  - 0
125
- hash: -169552129748110628
125
+ hash: -665461472920703222
126
126
  required_rubygems_version: !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
@@ -131,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
131
  version: '0'
132
132
  segments:
133
133
  - 0
134
- hash: -169552129748110628
134
+ hash: -665461472920703222
135
135
  requirements: []
136
136
  rubyforge_project:
137
137
  rubygems_version: 1.8.24