Class: ActiveResourceHttpMock::DataFile

Inherits:
Object
  • Object
show all
Defined in:
lib/active_resource_http_mock/data_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ DataFile

Returns a new instance of DataFile.



7
8
9
# File 'lib/active_resource_http_mock/data_file.rb', line 7

def initialize(context)
  @context = context
end

Instance Method Details

#data_dirObject



11
12
13
14
15
16
17
# File 'lib/active_resource_http_mock/data_file.rb', line 11

def data_dir
  if defined?(Rails)
    File.join(Rails.root, ActiveResourceHttpMock.config.httpmock_path)
  else
    ActiveResourceHttpMock.config.httpmock_path
  end
end

#dump_mocks(responses) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/active_resource_http_mock/data_file.rb', line 33

def dump_mocks(responses)
  yaml = YAML.dump(
    responses.map {|response|
      response[1] = ActiveResourceHttpMock.config.url_encoder.call(response[1])
      response
    }
  )

  File.write data_file_path, yaml
end

#load_mocksObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_resource_http_mock/data_file.rb', line 19

def load_mocks
  return unless data_file_exists?

  requests = YAML.load_file(data_file_path).map {|request|
    url = ActiveResourceHttpMock.config.url_decoder.call(request[1])
    request[1] = URI.parse(url).request_uri
    request
  }

  ::ActiveResource::HttpMock.respond_to(false) {|mock|
    requests.each {|request| mock.send *request }
  }
end