Skip to content

Commit

Permalink
Merge pull request #11 from jarib/custom_timings
Browse files Browse the repository at this point in the history
Do not use define_singleton_method for custom timings #7
  • Loading branch information
mbeasley committed Apr 8, 2019
2 parents db6607e + 82345a0 commit 3a6dd6c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
15 changes: 7 additions & 8 deletions lib/har/page_timings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ module HAR
class PageTimings < SchemaType
def initialize(input)
super(input)
define_custom_timings(input)
@input = input
end

private
def custom
@custom ||= begin
# TODO: move to `transform_keys` once ruby 2.0.0 support is dropped
custom_timings = Hash[@input.select { |key| key.start_with?('_') }
.map { |key, val| [key[1..-1], val] }]

# As defined by the HAR spec, custom timings must start with an underscore.
# To prevent collisions, the methods generated for those custom timings will
# also start with an underscore.
def define_custom_timings(input)
input.each do |key, value|
define_singleton_method(key) { value } if key.start_with?('_')
OpenStruct.new(custom_timings)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/hars/google.com.har
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"pageTimings":{
"onContentLoad":90,
"onLoad":245,
"_custom_example":123
"_customExample":123
}
}
],
Expand Down
2 changes: 1 addition & 1 deletion spec/har/page_timings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module HAR
let(:timings) { Archive.from_file(google_path).pages.first.timings }

it "defines methods for custom timings" do
timings._custom_example.should == 123
timings.custom.customExample.should == 123
end
end # PageTimings
end # HAR

0 comments on commit 3a6dd6c

Please sign in to comment.