Class: Restforce::Concerns::BatchAPI::Subrequests

Inherits:
Object
  • Object
show all
Defined in:
lib/restforce/concerns/batch_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Subrequests

Returns a new instance of Subrequests.



37
38
39
40
# File 'lib/restforce/concerns/batch_api.rb', line 37

def initialize(options)
  @options = options
  @requests = []
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



41
42
43
# File 'lib/restforce/concerns/batch_api.rb', line 41

def options
  @options
end

#requestsObject (readonly)

Returns the value of attribute requests.



41
42
43
# File 'lib/restforce/concerns/batch_api.rb', line 41

def requests
  @requests
end

Instance Method Details

#create(sobject, attrs) ⇒ Object



43
44
45
# File 'lib/restforce/concerns/batch_api.rb', line 43

def create(sobject, attrs)
  requests << { method: 'POST', url: batch_api_path(sobject), richInput: attrs }
end

#destroy(sobject, id) ⇒ Object



59
60
61
# File 'lib/restforce/concerns/batch_api.rb', line 59

def destroy(sobject, id)
  requests << { method: 'DELETE', url: batch_api_path("#{sobject}/#{id}") }
end

#update(sobject, attrs) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/restforce/concerns/batch_api.rb', line 47

def update(sobject, attrs)
  id = attrs.fetch(attrs.keys.find { |k, v| k.to_s.casecmp?('id') }, nil)
  raise ArgumentError, 'Id field missing from attrs.' unless id

  attrs_without_id = attrs.reject { |k, v| k.to_s.casecmp?('id') }
  requests << {
    method: 'PATCH',
    url: batch_api_path("#{sobject}/#{id}"),
    richInput: attrs_without_id
  }
end

#upsert(sobject, ext_field, attrs) ⇒ Object

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/restforce/concerns/batch_api.rb', line 63

def upsert(sobject, ext_field, attrs)
  raise ArgumentError, 'External id field missing.' unless ext_field

  ext_id = attrs.fetch(attrs.keys.find { |k, v|
    k.to_s.casecmp?(ext_field.to_s)
  }, nil)
  raise ArgumentError, 'External id missing from attrs.' unless ext_id

  attrs_without_ext_id = attrs.reject { |k, v| k.to_s.casecmp?(ext_field) }
  requests << {
    method: 'PATCH',
    url: batch_api_path("#{sobject}/#{ext_field}/#{ext_id}"),
    richInput: attrs_without_ext_id
  }
end