Permalink
Browse files

Underscorize result keys.

  • Loading branch information...
1 parent 5110640 commit 0788fed65ba1c03257694f0233beb0718bd0d77a @abuiles committed Mar 2, 2012
Showing with 30 additions and 11 deletions.
  1. +10 −10 README.md
  2. +20 −1 lib/susuwatari/result.rb
View
@@ -22,20 +22,20 @@ This gem allows you to use the API of Patrick Meenan excellent [webpagetest.org]
=> :completed
mei.result.keys
- => [ "testId", "summary", "testUrl", "location", "connectivity",
- "bwDown", "bwUp", "latency", "plr", "completed", "runs", "average",
+ => [ "test_id", "summary", "test_url", "location", "connectivity",
+ "bw_down", "bw_up", "latency", "plr", "completed", "runs", "average",
"median", "run"]
- mei.result.testId
+ mei.result.test_id
=> "aASFDasfdads2"
- mei.result.run.firstView.images.waterfall
+ mei.result.run.first_view.images.waterfall
=> "http://www.webpagetest.org/results/12/02/09/KG/35XV333/1_waterfall.png"
- mei.result.run.firstView.results.score_cache
+ mei.result.run.first_view.results.score_cache
=> "98"
- mei.result.run.firstView.results.requests
+ mei.result.run.first_view.results.requests
=> 12
You can check what a result looks like [here](https://sites.google.com/a/webpagetest.org/docs/advanced-features/webpagetest-restful-apis#TOC-Getting-test-results).
@@ -73,16 +73,16 @@ Once you've installed the gem, you can use it like this:
=> :completed
mei.result.keys
- => [ "testId", "summary", "testUrl", "location", "connectivity",
- "bwDown", "bwUp", "latency", "plr", "completed", "runs", "average",
+ => [ "test_id", "summary", "test_url", "location", "connectivity",
+ "bw_down", "bw_up", "latency", "plr", "completed", "runs", "average",
"median", "run"]
#You can access the results as a hash
- mei.result["testId"]
+ mei.result[:test_id]
=> 'aBd333'
#Or in a pseudo-object oriented fashion
- mei.result.testId
+ mei.result.test_id
=> 'aBd333'
Locations
@@ -1,4 +1,5 @@
require 'active_support/core_ext/hash/conversions'
+require 'active_support/inflections'
require 'csv'
@@ -40,7 +41,25 @@ def fetch_status
def fetch_result
response = RestClient.get "#{RESULT_URL_PREFIX}/#{test_id}/"
- @test_result = Hashie::Mash.new(Hash.from_xml(response.body)).response.data
+ response = deep_symbolize(Hash.from_xml(response.body)){ |key| key.underscore }
+ @test_result = Hashie::Mash.new(response).response.data
+ end
+
+ # Thanks to https://gist.github.com/998709 with a slightly modification.
+ def deep_symbolize(hsh, &block)
+ hsh.inject({}) { |result, (key, value)|
+ # Recursively deep-symbolize subhashes
+ value = deep_symbolize(value, &block) if value.is_a? Hash
+
+ # Pre-process the key with a block if it was given
+ key = yield key if block_given?
+ # Symbolize the key string if it responds to to_sym
+ sym_key = key.to_sym rescue key
+
+ # write it back into the result and return the updated hash
+ result[sym_key] = value
+ result
+ }
end
end
end

0 comments on commit 0788fed

Please sign in to comment.