Module: CcApiStub::Helper

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fail_request(method = :any, code = 500, response_body = {}, path = /#{CcApiStub::Helper.host}/) ⇒ Object



14
15
16
# File 'lib/cc_api_stub/helper.rb', line 14

def fail_request(method = :any, code = 500, response_body = {}, path = /#{CcApiStub::Helper.host}/)
  WebMock::API.stub_request(method, path).to_return(response(code, response_body))
end

.fail_with_error(method, error_attributes = nil) ⇒ Object



26
27
28
29
30
# File 'lib/cc_api_stub/helper.rb', line 26

def fail_with_error(method, error_attributes=nil)
  WebMock::API.
    stub_request(method, /#{CcApiStub::Helper.host}/).
    to_return(response(400, error_attributes))
end

.hostObject



22
23
24
# File 'lib/cc_api_stub/helper.rb', line 22

def host
  @@host or raise 'No host set'
end

.host=(host) ⇒ Object



18
19
20
# File 'lib/cc_api_stub/helper.rb', line 18

def host=(host)
  @@host = host
end

.load_fixtures(fixtures_name, options = {}) ⇒ Object



32
33
34
35
36
37
# File 'lib/cc_api_stub/helper.rb', line 32

def load_fixtures(fixtures_name, options = {})
  path = File.join(File.dirname(__FILE__), "..", "..", "spec/fixtures/#{fixtures_name.to_s}.json")
  JSON.parse(File.read(path)).tap do |fixture|
    fixture["entity"].merge!(options.stringify_keys) if options.any?
  end
end

.response(code, body = nil) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/cc_api_stub/helper.rb', line 6

def response(code, body=nil)
  {
    :status => code,
    :headers => {},
    :body => body.nil? ? "--garbage--" : body.to_json
  }
end

Instance Method Details

#fail_to_loadObject



94
95
96
# File 'lib/cc_api_stub/helper.rb', line 94

def fail_to_load
  stub_get(object_endpoint, {}, response(500))
end

#fail_to_load_manyObject



108
109
110
# File 'lib/cc_api_stub/helper.rb', line 108

def fail_to_load_many
  stub_get(collection_endpoint, {}, response(500))
end

#find_fixture(fixture_name) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/cc_api_stub/helper.rb', line 67

def find_fixture(fixture_name)
  begin
    Helper.load_fixtures("fake_#{fixture_name}")
  rescue
    Helper.load_fixtures("fake_organization_#{fixture_name}")
  end
end

#fixture_prefixObject



127
128
129
# File 'lib/cc_api_stub/helper.rb', line 127

def fixture_prefix
  "_cc"
end

#object_classObject



75
76
77
78
79
80
81
82
83
# File 'lib/cc_api_stub/helper.rb', line 75

def object_class
  begin
    object_name.camelcase.constantize
  rescue
    "Organization::#{object_name.camelcase}".constantize
  rescue
    "User::#{object_name.camelcase}".constantize
  end
end

#object_nameObject



63
64
65
# File 'lib/cc_api_stub/helper.rb', line 63

def object_name
  name.demodulize.underscore.singularize
end

#response(code, body = nil) ⇒ Object



85
86
87
# File 'lib/cc_api_stub/helper.rb', line 85

def response(code, body=nil)
  CcApiStub::Helper.response(code, body)
end

#stub_delete(*args) ⇒ Object



52
53
54
# File 'lib/cc_api_stub/helper.rb', line 52

def stub_delete(*args)
  stub_request(:delete, *args)
end

#stub_get(*args) ⇒ Object



40
41
42
# File 'lib/cc_api_stub/helper.rb', line 40

def stub_get(*args)
  stub_request(:get, *args)
end

#stub_post(*args) ⇒ Object



44
45
46
# File 'lib/cc_api_stub/helper.rb', line 44

def stub_post(*args)
  stub_request(:post, *args)
end

#stub_put(*args) ⇒ Object



48
49
50
# File 'lib/cc_api_stub/helper.rb', line 48

def stub_put(*args)
  stub_request(:put, *args)
end

#stub_request(method, path, params = nil, response = nil) ⇒ Object



56
57
58
59
60
61
# File 'lib/cc_api_stub/helper.rb', line 56

def stub_request(method, path, params = nil, response = nil)
  stub = WebMock::API.stub_request(method, path)
  stub.to_return(response) if response
  stub.with(params) if params
  stub
end

#succeed_to_createObject



112
113
114
115
# File 'lib/cc_api_stub/helper.rb', line 112

def succeed_to_create
  response_body = {object_name.to_sym => {:id => "#{object_name.gsub("_", "-")}-id-1"}}
  stub_post(collection_endpoint, {}, response(201, response_body))
end

#succeed_to_deleteObject Also known as: succeed_to_leave



121
122
123
# File 'lib/cc_api_stub/helper.rb', line 121

def succeed_to_delete
  stub_delete(object_endpoint, nil, response(200))
end

#succeed_to_load(options = {}) ⇒ Object



89
90
91
92
# File 'lib/cc_api_stub/helper.rb', line 89

def succeed_to_load(options={})
  response_body = CcApiStub::Helper.load_fixtures(options.delete(:fixture) || "fake_cc_#{object_name}", options)
  stub_get(object_endpoint, {}, response(200, response_body))
end

#succeed_to_load_empty(options = {}) ⇒ Object



103
104
105
106
# File 'lib/cc_api_stub/helper.rb', line 103

def succeed_to_load_empty(options = {})
  root = options[:root] || object_name.pluralize
  stub_get(collection_endpoint, {}, response(200, {root => [], "pagination" => {}}))
end

#succeed_to_load_many(options = {}) ⇒ Object



98
99
100
101
# File 'lib/cc_api_stub/helper.rb', line 98

def succeed_to_load_many(options={})
  response_body = CcApiStub::Helper.load_fixtures(options.delete(:fixture) || "fake_#{object_name.pluralize}", options)
  stub_get(collection_endpoint, {}, response(200, response_body))
end

#succeed_to_update(attributes = {}) ⇒ Object



117
118
119
# File 'lib/cc_api_stub/helper.rb', line 117

def succeed_to_update(attributes = {})
  stub_put(object_endpoint, nil, response(200, {}))
end