Class: EphemeralResponse::Fixture
- Inherits:
-
Object
- Object
- EphemeralResponse::Fixture
- Defined in:
- lib/ephemeral_response/fixture.rb
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
Returns the value of attribute response.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
- .clear ⇒ Object
- .find(uri, request) ⇒ Object
- .find_or_initialize(uri, request, &block) ⇒ Object
- .fixtures ⇒ Object
- .load_all ⇒ Object
- .load_fixture(file_name) ⇒ Object
- .register(fixture) ⇒ Object
- .respond_to(uri, request) ⇒ Object
Instance Method Summary collapse
- #expired? ⇒ Boolean
- #file_name ⇒ Object
- #fs_path ⇒ Object
- #identifier ⇒ Object
-
#initialize(uri, request) {|_self| ... } ⇒ Fixture
constructor
A new instance of Fixture.
- #method ⇒ Object
- #normalized_name ⇒ Object
- #path ⇒ Object
- #register ⇒ Object
- #save ⇒ Object
- #uri_identifier ⇒ Object
Constructor Details
#initialize(uri, request) {|_self| ... } ⇒ Fixture
Returns a new instance of Fixture.
49 50 51 52 53 54 |
# File 'lib/ephemeral_response/fixture.rb', line 49 def initialize(uri, request) @uri = uri.normalize @request = deep_dup request @created_at = Time.now yield self if block_given? end |
Instance Attribute Details
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
4 5 6 |
# File 'lib/ephemeral_response/fixture.rb', line 4 def created_at @created_at end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
4 5 6 |
# File 'lib/ephemeral_response/fixture.rb', line 4 def request @request end |
#response ⇒ Object
Returns the value of attribute response.
3 4 5 |
# File 'lib/ephemeral_response/fixture.rb', line 3 def response @response end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
4 5 6 |
# File 'lib/ephemeral_response/fixture.rb', line 4 def uri @uri end |
Class Method Details
.clear ⇒ Object
10 11 12 |
# File 'lib/ephemeral_response/fixture.rb', line 10 def self.clear @fixtures = {} end |
.find(uri, request) ⇒ Object
14 15 16 |
# File 'lib/ephemeral_response/fixture.rb', line 14 def self.find(uri, request) fixtures[Fixture.new(uri, request).identifier] end |
.find_or_initialize(uri, request, &block) ⇒ Object
30 31 32 |
# File 'lib/ephemeral_response/fixture.rb', line 30 def self.find_or_initialize(uri, request, &block) find(uri, request) || new(uri, request, &block) end |
.fixtures ⇒ Object
6 7 8 |
# File 'lib/ephemeral_response/fixture.rb', line 6 def self.fixtures @fixtures ||= {} end |
.load_all ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/ephemeral_response/fixture.rb', line 18 def self.load_all clear if File.directory?(Configuration.fixture_directory) Dir.glob("#{Configuration.fixture_directory}/*.yml", &method(:load_fixture)) end fixtures end |
.load_fixture(file_name) ⇒ Object
26 27 28 |
# File 'lib/ephemeral_response/fixture.rb', line 26 def self.load_fixture(file_name) register YAML.load_file(file_name) end |
.register(fixture) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/ephemeral_response/fixture.rb', line 34 def self.register(fixture) if fixture.expired? FileUtils.rm fixture.path else fixtures[fixture.identifier] = fixture end end |
.respond_to(uri, request) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/ephemeral_response/fixture.rb', line 42 def self.respond_to(uri, request) find_or_initialize(uri, request) do |fixture| fixture.response = yield fixture.register end.response end |
Instance Method Details
#expired? ⇒ Boolean
56 57 58 |
# File 'lib/ephemeral_response/fixture.rb', line 56 def expired? !Configuration.skip_expiration && (created_at + Configuration.expiration) < Time.now end |
#file_name ⇒ Object
60 61 62 |
# File 'lib/ephemeral_response/fixture.rb', line 60 def file_name @file_name ||= generate_file_name end |
#fs_path ⇒ Object
76 77 78 |
# File 'lib/ephemeral_response/fixture.rb', line 76 def fs_path uri.path.dup.sub!(/^\/(.+)$/, '\1') end |
#identifier ⇒ Object
64 65 66 |
# File 'lib/ephemeral_response/fixture.rb', line 64 def identifier Digest::SHA1.hexdigest(registered_identifier || default_identifier) end |
#method ⇒ Object
68 69 70 |
# File 'lib/ephemeral_response/fixture.rb', line 68 def method request.method end |
#normalized_name ⇒ Object
72 73 74 |
# File 'lib/ephemeral_response/fixture.rb', line 72 def normalized_name [uri.host, method, fs_path].compact.join("_").gsub(/[\/]/, '-') end |
#path ⇒ Object
80 81 82 |
# File 'lib/ephemeral_response/fixture.rb', line 80 def path File.join(Configuration.fixture_directory, file_name) end |
#register ⇒ Object
84 85 86 87 88 89 |
# File 'lib/ephemeral_response/fixture.rb', line 84 def register unless Configuration.white_list.include? uri.host save self.class.register self end end |
#save ⇒ Object
91 92 93 94 95 96 |
# File 'lib/ephemeral_response/fixture.rb', line 91 def save FileUtils.mkdir_p Configuration.fixture_directory File.open(path, 'w') do |f| f.write to_yaml end end |
#uri_identifier ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/ephemeral_response/fixture.rb', line 98 def uri_identifier if uri.query parts = uri.to_s.split("?", 2) parts[1] = parts[1].split('&').sort parts else uri.to_s end end |