Class: RestforceMock::Client

Inherits:
Object
  • Object
show all
Includes:
Restforce::Concerns::API, Sandbox
Defined in:
lib/restforce_mock/client.rb

Defined Under Namespace

Classes: Body

Instance Method Summary collapse

Methods included from Sandbox

add_object, #add_object, client, find_object, get_object, #get_object, #get_object_from_query, initialize, reset!, storage, update_object, #update_object, update_schema, validate_all_present_fields!

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



11
12
# File 'lib/restforce_mock/client.rb', line 11

def initialize(opts = {})
end

Instance Method Details

#api_get(url, attrs = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/restforce_mock/client.rb', line 18

def api_get(url, attrs = nil)
  url=~/sobjects\/(.+)\/(.+)/
  object=$1
  id=$2

  if !attrs.nil? && url == 'query'
    query = attrs.values.first
    response = get_object_from_query(query)
    return Body.new(response, url)
  else
    response = get_object(object, id)
    return Body.new(response)
  end
end

#api_patch(url, attrs) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/restforce_mock/client.rb', line 33

def api_patch(url, attrs)
  url=~/sobjects\/(.+)\/(.+)/
  object=$1
  id=$2
  validate_schema!(object)
  validate_presence!(object, id)
  update_object(object, id, attrs)
end

#api_post(url, attrs) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/restforce_mock/client.rb', line 42

def api_post(url, attrs)
  url=~/sobjects\/(.+)/
  sobject = $1
  id = ::SecureRandom.urlsafe_base64(13) #duplicates possible
  validate_schema!(sobject)
  validate_requires!(sobject, attrs)
  add_object(sobject, id, attrs)
  return Body.new(id)
end

#mashify?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/restforce_mock/client.rb', line 14

def mashify?
  true
end

#validate_presence!(object, id) ⇒ Object



69
70
71
72
73
74
# File 'lib/restforce_mock/client.rb', line 69

def validate_presence!(object, id)
  unless RestforceMock::Sandbox.storage[object][id]
    msg = "Provided external ID field does not exist or is not accessible: #{id}"
    raise Faraday::Error::ResourceNotFound.new(msg)
  end
end

#validate_requires!(sobject, attrs) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/restforce_mock/client.rb', line 52

def validate_requires!(sobject, attrs)
  return unless RestforceMock.configuration.schema_file
  return unless RestforceMock.configuration.error_on_required

  object_schema = schema[sobject]
  required = object_schema.
    select{|k,v|v[:required]}.
    collect{|k,v|k}.
    collect(&:to_sym)

  missing = required - attrs.keys - RestforceMock.configuration.required_exclusions
  if missing.length > 0
    raise Faraday::Error::ResourceNotFound.new(
      "REQUIRED_FIELD_MISSING: Required fields are missing: #{missing}")
  end
end

#validate_schema!(object) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/restforce_mock/client.rb', line 76

def validate_schema!(object)
  if RestforceMock.configuration.raise_on_schema_missing
    unless schema[object]
      raise RestforceMock::Error.new("No schema for Salesforce object #{object}")
    end
  end
end