Module: CcApiStub::Helper
- Included in:
- AppUsageEvents, Applications, Domains, Events, Frameworks, OrganizationUsers, Organizations, Routes, Runtimes, ServiceBindings, ServiceInstances, Services, SpaceUsers, Spaces, Users
- Defined in:
- lib/cc_api_stub/helper.rb
Class Method Summary collapse
- .fail_request(method = :any, code = 500, response_body = {}, path = /#{CcApiStub::Helper.host}/) ⇒ Object
- .fail_with_error(method, error_attributes = nil) ⇒ Object
- .host ⇒ Object
- .host=(host) ⇒ Object
- .load_fixtures(fixture_name_or_path, options = {}) ⇒ Object
- .response(code, body = nil) ⇒ Object
Instance Method Summary collapse
- #fail_to_load(options = {}) ⇒ Object
- #fail_to_load_many ⇒ Object
- #fail_to_update(options = {}) ⇒ Object
- #find_fixture(fixture_name) ⇒ Object
- #fixture_prefix ⇒ Object
- #object_class ⇒ Object
- #object_name ⇒ Object
- #response(code, body = nil) ⇒ Object
- #stub_delete(*args) ⇒ Object
- #stub_get(*args) ⇒ Object
- #stub_post(*args) ⇒ Object
- #stub_put(*args) ⇒ Object
- #stub_request(method, path, params = nil, response = nil) ⇒ Object
- #succeed_to_create ⇒ Object
- #succeed_to_delete(options = {}) ⇒ Object (also: #succeed_to_leave)
- #succeed_to_load(options = {}) ⇒ Object
- #succeed_to_load_empty(options = {}) ⇒ Object
- #succeed_to_load_many(options = {}) ⇒ Object
- #succeed_to_update(options = {}) ⇒ Object
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 |
.host ⇒ Object
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(fixture_name_or_path, options = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cc_api_stub/helper.rb', line 32 def load_fixtures(fixture_name_or_path, = {}) path = if .delete(:use_local_fixture) fixture_name_or_path else File.join(File.dirname(__FILE__), "..", "..", "spec/fixtures/#{fixture_name_or_path.to_s}.json") end JSON.parse(File.read(path)).tap do |fixture| fixture["entity"].merge!(.stringify_keys) if .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_load(options = {}) ⇒ Object
98 99 100 |
# File 'lib/cc_api_stub/helper.rb', line 98 def fail_to_load( = {}) stub_get(object_endpoint([:id]), {}, response(500)) end |
#fail_to_load_many ⇒ Object
112 113 114 |
# File 'lib/cc_api_stub/helper.rb', line 112 def fail_to_load_many stub_get(collection_endpoint, {}, response(500)) end |
#fail_to_update(options = {}) ⇒ Object
125 126 127 |
# File 'lib/cc_api_stub/helper.rb', line 125 def fail_to_update( = {}) stub_put(object_endpoint([:id]), nil, response(500, {})) end |
#find_fixture(fixture_name) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/cc_api_stub/helper.rb', line 71 def find_fixture(fixture_name) begin Helper.load_fixtures("fake_#{fixture_name}") rescue Helper.load_fixtures("fake_organization_#{fixture_name}") end end |
#fixture_prefix ⇒ Object
135 136 137 |
# File 'lib/cc_api_stub/helper.rb', line 135 def fixture_prefix "_cc" end |
#object_class ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/cc_api_stub/helper.rb', line 79 def object_class begin object_name.camelcase.constantize rescue "Organization::#{object_name.camelcase}".constantize rescue "User::#{object_name.camelcase}".constantize end end |
#object_name ⇒ Object
67 68 69 |
# File 'lib/cc_api_stub/helper.rb', line 67 def object_name name.demodulize.underscore.singularize end |
#response(code, body = nil) ⇒ Object
89 90 91 |
# File 'lib/cc_api_stub/helper.rb', line 89 def response(code, body=nil) CcApiStub::Helper.response(code, body) end |
#stub_delete(*args) ⇒ Object
56 57 58 |
# File 'lib/cc_api_stub/helper.rb', line 56 def stub_delete(*args) stub_request(:delete, *args) end |
#stub_get(*args) ⇒ Object
44 45 46 |
# File 'lib/cc_api_stub/helper.rb', line 44 def stub_get(*args) stub_request(:get, *args) end |
#stub_post(*args) ⇒ Object
48 49 50 |
# File 'lib/cc_api_stub/helper.rb', line 48 def stub_post(*args) stub_request(:post, *args) end |
#stub_put(*args) ⇒ Object
52 53 54 |
# File 'lib/cc_api_stub/helper.rb', line 52 def stub_put(*args) stub_request(:put, *args) end |
#stub_request(method, path, params = nil, response = nil) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/cc_api_stub/helper.rb', line 60 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.nil? && !params.empty? stub end |
#succeed_to_create ⇒ Object
116 117 118 119 |
# File 'lib/cc_api_stub/helper.rb', line 116 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_delete(options = {}) ⇒ Object Also known as: succeed_to_leave
129 130 131 |
# File 'lib/cc_api_stub/helper.rb', line 129 def succeed_to_delete( = {}) stub_delete(object_endpoint([:id]), nil, response(200)) end |
#succeed_to_load(options = {}) ⇒ Object
93 94 95 96 |
# File 'lib/cc_api_stub/helper.rb', line 93 def succeed_to_load(={}) response_body = (.reverse_merge!({:fixture => "fake_cc_#{object_name}"})) stub_get(object_endpoint([:id]), {}, response(200, response_body)) end |
#succeed_to_load_empty(options = {}) ⇒ Object
107 108 109 110 |
# File 'lib/cc_api_stub/helper.rb', line 107 def succeed_to_load_empty( = {}) root = [:root] || object_name.pluralize stub_get(collection_endpoint, {}, response(200, {root => [], "pagination" => {}})) end |
#succeed_to_load_many(options = {}) ⇒ Object
102 103 104 105 |
# File 'lib/cc_api_stub/helper.rb', line 102 def succeed_to_load_many(={}) response_body = (.reverse_merge!({:fixture => "fake_cc_#{object_name.pluralize}"})) stub_get(collection_endpoint, {}, response(200, response_body)) end |
#succeed_to_update(options = {}) ⇒ Object
121 122 123 |
# File 'lib/cc_api_stub/helper.rb', line 121 def succeed_to_update( = {}) stub_put(object_endpoint([:id]), nil, response(200, ())) end |