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, request_block) ⇒ 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
- #new? ⇒ Boolean
- #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.
58 59 60 61 62 63 |
# File 'lib/ephemeral_response/fixture.rb', line 58 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
34 35 36 |
# File 'lib/ephemeral_response/fixture.rb', line 34 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.effective_directory) Dir.glob("#{Configuration.effective_directory}/*.yml", &method(:load_fixture)) end fixtures end |
.load_fixture(file_name) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/ephemeral_response/fixture.rb', line 26 def self.load_fixture(file_name) if fixture = YAML.load_file(file_name) register fixture else EphemeralResponse::Configuration.debug_output.puts "EphemeralResponse couldn't load fixture: #{file_name}" end end |
.register(fixture) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/ephemeral_response/fixture.rb', line 38 def self.register(fixture) if fixture.expired? FileUtils.rm_f fixture.path else fixtures[fixture.identifier] = fixture end end |
.respond_to(uri, request, request_block) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ephemeral_response/fixture.rb', line 46 def self.respond_to(uri, request, request_block) fixture = find_or_initialize(uri, request) if fixture.new? fixture.response = yield fixture.response.instance_variable_set(:@body, fixture.response.body.to_s) fixture.register elsif request_block request_block.call fixture.response end fixture.response end |
Instance Method Details
#expired? ⇒ Boolean
65 66 67 |
# File 'lib/ephemeral_response/fixture.rb', line 65 def expired? !Configuration.skip_expiration && (created_at + Configuration.expiration) < Time.now end |
#file_name ⇒ Object
69 70 71 |
# File 'lib/ephemeral_response/fixture.rb', line 69 def file_name @file_name ||= generate_file_name end |
#fs_path ⇒ Object
89 90 91 |
# File 'lib/ephemeral_response/fixture.rb', line 89 def fs_path uri.path.dup.sub!(/^\/(.+)$/, '\1') end |
#identifier ⇒ Object
73 74 75 |
# File 'lib/ephemeral_response/fixture.rb', line 73 def identifier Digest::SHA1.hexdigest(registered_identifier || default_identifier) end |
#method ⇒ Object
77 78 79 |
# File 'lib/ephemeral_response/fixture.rb', line 77 def method request.method end |
#new? ⇒ Boolean
81 82 83 |
# File 'lib/ephemeral_response/fixture.rb', line 81 def new? !self.class.fixtures.has_key?(identifier) end |
#normalized_name ⇒ Object
85 86 87 |
# File 'lib/ephemeral_response/fixture.rb', line 85 def normalized_name [uri.host, method, fs_path].compact.join("_").gsub(/[\/]/, '-') end |
#path ⇒ Object
93 94 95 |
# File 'lib/ephemeral_response/fixture.rb', line 93 def path File.join(Configuration.effective_directory, file_name) end |
#register ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/ephemeral_response/fixture.rb', line 97 def register unless Configuration.white_list.include? uri.host EphemeralResponse::Configuration.debug_output.puts "#{request.method} #{uri} saved as #{path}" save self.class.register self end end |
#save ⇒ Object
105 106 107 108 109 110 |
# File 'lib/ephemeral_response/fixture.rb', line 105 def save FileUtils.mkdir_p Configuration.effective_directory File.open(path, 'w') do |f| f.write to_yaml end end |
#uri_identifier ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/ephemeral_response/fixture.rb', line 112 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 |