Class: Restforce::Concerns::CompositeAPI::Subrequests

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Subrequests

Returns a new instance of Subrequests.



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

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



47
48
49
# File 'lib/restforce/concerns/composite_api.rb', line 47

def options
  @options
end

#requestsObject (readonly)

Returns the value of attribute requests.



47
48
49
# File 'lib/restforce/concerns/composite_api.rb', line 47

def requests
  @requests
end

Instance Method Details

#create(sobject, reference_id, attrs) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/restforce/concerns/composite_api.rb', line 49

def create(sobject, reference_id, attrs)
  requests << {
    method: 'POST',
    url: composite_api_path(sobject),
    body: attrs,
    referenceId: reference_id
  }
end

#destroy(sobject, reference_id, id) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/restforce/concerns/composite_api.rb', line 71

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

#find(sobject, reference_id, id) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/restforce/concerns/composite_api.rb', line 96

def find(sobject, reference_id, id)
  requests << {
    method: 'GET',
    url: composite_api_path("#{sobject}/#{id}"),
    referenceId: reference_id
  }
end

#update(sobject, reference_id, attrs) ⇒ Object

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/restforce/concerns/composite_api.rb', line 58

def update(sobject, reference_id, 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: composite_api_path("#{sobject}/#{id}"),
    body: attrs_without_id,
    referenceId: reference_id
  }
end

#upsert(sobject, reference_id, ext_field, attrs) ⇒ Object

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/restforce/concerns/composite_api.rb', line 79

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

  ext_id = attrs.fetch(attrs.keys.find do |k, _v|
    k.to_s.casecmp?(ext_field.to_s)
  end, 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: composite_api_path("#{sobject}/#{ext_field}/#{ext_id}"),
    body: attrs_without_ext_id,
    referenceId: reference_id
  }
end