module CcApiStub::Helper

Public Class Methods

fail_request(method = :any, code = 500, response_body = {}, path = / click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/cc_api_stub/helper.rb, line 22
def host
  @@host or raise 'No host set'
end
host=(host) click to toggle source
# File lib/cc_api_stub/helper.rb, line 18
def host=(host)
  @@host = host
end
load_fixtures(fixture_name_or_path, options = {}) click to toggle source
# 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) click to toggle source
# 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

Public Instance Methods

fail_to_load(options = {}) click to toggle source
# 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_many() click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/cc_api_stub/helper.rb, line 135
def fixture_prefix
  "_cc"
end
object_class() click to toggle source
# 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() click to toggle source
# File lib/cc_api_stub/helper.rb, line 67
def object_name
  name.demodulize.underscore.singularize
end
response(code, body=nil) click to toggle source
# File lib/cc_api_stub/helper.rb, line 89
def response(code, body=nil)
  CcApiStub::Helper.response(code, body)
end
stub_delete(*args) click to toggle source
# File lib/cc_api_stub/helper.rb, line 56
def stub_delete(*args)
  stub_request(:delete, *args)
end
stub_get(*args) click to toggle source
# File lib/cc_api_stub/helper.rb, line 44
def stub_get(*args)
  stub_request(:get, *args)
end
stub_post(*args) click to toggle source
# File lib/cc_api_stub/helper.rb, line 48
def stub_post(*args)
  stub_request(:post, *args)
end
stub_put(*args) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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 = {}) click to toggle source
# File lib/cc_api_stub/helper.rb, line 129
def succeed_to_delete(options = {})
  stub_delete(object_endpoint(options[:id]), nil, response(200))
end
Also aliased as: succeed_to_leave
succeed_to_leave(options = {})
Alias for: succeed_to_delete
succeed_to_load(options={}) click to toggle source
# 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 = {}) click to toggle source
# 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={}) click to toggle source
# 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 = {}) click to toggle source
# 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

Private Instance Methods

response_from_options(options) click to toggle source
# File lib/cc_api_stub/helper.rb, line 141
def response_from_options(options)
  fixture = options.delete(:fixture)
  return options[:response] if options[:response]
  return CcApiStub::Helper.load_fixtures(fixture, options) if fixture
  {}
end