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:



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_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



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

.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.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

Returns:

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



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

def file_name
  @file_name ||= generate_file_name
end

#fs_pathObject



89
90
91
# File 'lib/ephemeral_response/fixture.rb', line 89

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

#identifierObject



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

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

#methodObject



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

def method
  request.method
end

#new?Boolean

Returns:

  • (Boolean)


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

def new?
  !self.class.fixtures.has_key?(identifier)
end

#normalized_nameObject



85
86
87
# File 'lib/ephemeral_response/fixture.rb', line 85

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

#pathObject



93
94
95
# File 'lib/ephemeral_response/fixture.rb', line 93

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

#registerObject



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

#saveObject



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_identifierObject



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