Class: Marley::TestClient

Inherits:
Object show all
Includes:
Rack::Test::Methods
Defined in:
lib/marley/test_helpers.rb

Constant Summary collapse

CRUD2REST =
{'create' => 'post','read' => 'get','update' => 'put', 'del' => 'delete'}
DEFAULT_OPTS =
{:url => nil,:root_url => nil, :resource_name => nil, :instance_id => nil, :method => nil, :extention =>nil, :auth => nil, :code => nil, :debug => nil}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ TestClient

Returns a new instance of TestClient.



12
13
14
# File 'lib/marley/test_helpers.rb', line 12

def initialize(opts={})
  @opts=DEFAULT_OPTS.merge(opts)
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



8
9
10
# File 'lib/marley/test_helpers.rb', line 8

def opts
  @opts
end

Instance Method Details

#appObject



9
10
11
# File 'lib/marley/test_helpers.rb', line 9

def app
  Marley::Router.new
end

#date_hack(reggae_obj) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/marley/test_helpers.rb', line 19

def date_hack(reggae_obj)
  if reggae_obj.class==Marley::Reggae
    reggae_obj.map {|o| date_hack(o)}
  elsif reggae_obj.class==Marley::ReggaeInstance
    reggae_obj.set_values(:date_created => 'date_created') if reggae_obj.schema[:date_created]
    reggae_obj.set_values(:date_updated => 'date_updated') if reggae_obj.schema[:date_updated]
    reggae_obj
  else
    reggae_obj
  end
end

#make_url(opts = nil) ⇒ Object



15
16
17
18
# File 'lib/marley/test_helpers.rb', line 15

def make_url(opts=nil)
  opts||=@opts
  opts[:url] || '/' + [:root_url, :resource_name, :instance_id, :method].map {|k| opts[k]}.compact.join('/') + opts[:extention].to_s
end

#process(verb, params = {}, opts = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/marley/test_helpers.rb', line 30

def process(verb,params={},opts={})
  #p 'params:',params,'opts:',opts,"-------------" if opts
  opts||={}
  opts=@opts.merge(opts)
  expected_code=opts[:code] || RESP_CODES[verb]
  params=params.to_params if params.respond_to?(:to_params)
  if opts[:debug]
    p opts
    p "#{verb} to: '#{make_url(opts)}'" 
    p params 
    p opts[:auth]
  end
  authorize opts[:auth][0],opts[:auth][1] if opts[:auth]
  header 'Authorization',nil unless opts[:auth]  #clear auth from previous requests
  send(verb,make_url(opts),params)
  #p last_response.body if opts[:debug]
  #p JSON.parse(last_response.body) if opts[:debug]
  p last_response.status if opts[:debug]
  p expected_code if opts[:debug]
  return false unless (expected_code || RESP_CODES[method])==last_response.status
  date_hack(Reggae.get_resource(JSON.parse(last_response.body))) rescue last_response.body
end