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(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, options = {})
  path = if options.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!(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_load(options = {}) ⇒ Object



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

def fail_to_load(options = {})
  stub_get(object_endpoint(options[:id]), {}, response(500))
end

#fail_to_load_manyObject



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(options = {})
  stub_put(object_endpoint(options[: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_prefixObject



135
136
137
# File 'lib/cc_api_stub/helper.rb', line 135

def fixture_prefix
  "_cc"
end

#object_classObject



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_nameObject



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
  stub
end

#succeed_to_createObject



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(options = {})
  stub_delete(object_endpoint(options[: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(options={})
  response_body = response_from_options(options.reverse_merge!({:fixture => "fake_cc_#{object_name}"}))
  stub_get(object_endpoint(options[: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(options = {})
  root = options[: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(options={})
  response_body = response_from_options(options.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(options = {})
  stub_put(object_endpoint(options[:id]), nil, response(200, response_from_options(options)))
end