Class: EphemeralResponse::Fixture

Inherits:
Object
  • Object
show all
Defined in:
lib/ephemeral_response/fixture.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, request) {|_self| ... } ⇒ Fixture

Returns a new instance of Fixture.

Yields:

  • (_self)

Yield Parameters:



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_atObject (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

#requestObject (readonly)

Returns the value of attribute request.



4
5
6
# File 'lib/ephemeral_response/fixture.rb', line 4

def request
  @request
end

#responseObject

Returns the value of attribute response.



3
4
5
# File 'lib/ephemeral_response/fixture.rb', line 3

def response
  @response
end

#uriObject (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

.clearObject



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

.fixturesObject



6
7
8
# File 'lib/ephemeral_response/fixture.rb', line 6

def self.fixtures
  @fixtures ||= {}
end

.load_allObject



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

Returns:

  • (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_nameObject



60
61
62
# File 'lib/ephemeral_response/fixture.rb', line 60

def file_name
  @file_name ||= generate_file_name
end

#fs_pathObject



76
77
78
# File 'lib/ephemeral_response/fixture.rb', line 76

def fs_path
  uri.path.dup.sub!(/^\/(.+)$/, '\1')
end

#identifierObject



64
65
66
# File 'lib/ephemeral_response/fixture.rb', line 64

def identifier
  Digest::SHA1.hexdigest(registered_identifier || default_identifier)
end

#methodObject



68
69
70
# File 'lib/ephemeral_response/fixture.rb', line 68

def method
  request.method
end

#normalized_nameObject



72
73
74
# File 'lib/ephemeral_response/fixture.rb', line 72

def normalized_name
  [uri.host, method, fs_path].compact.join("_").gsub(/[\/]/, '-')
end

#pathObject



80
81
82
# File 'lib/ephemeral_response/fixture.rb', line 80

def path
  File.join(Configuration.fixture_directory, file_name)
end

#registerObject



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

#saveObject



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_identifierObject



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