Class: ShotgridApiRuby::Entities

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/shotgrid_api_ruby/entities.rb,
lib/shotgrid_api_ruby/entities/params.rb,
lib/shotgrid_api_ruby/entities/schema.rb,
lib/shotgrid_api_ruby/entities/summarize.rb

Defined Under Namespace

Classes: Params, Schema, Summarize

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, type) ⇒ Entities

Returns a new instance of Entities.



11
12
13
14
15
16
17
18
# File 'lib/shotgrid_api_ruby/entities.rb', line 11

def initialize(connection, type)
  @connection = T.let(connection.dup, Faraday::Connection)
  @type = T.let(type.to_s, String)
  @base_url_prefix = T.let(@connection.url_prefix, URI)
  @connection.url_prefix = "#{@connection.url_prefix}/entity/#{type}"
  @schema_client = T.let(nil, T.nilable(Schema))
  @summary_client = T.let(nil, T.nilable(Summarize))
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



21
22
23
# File 'lib/shotgrid_api_ruby/entities.rb', line 21

def connection
  @connection
end

#typeObject (readonly)

Returns the value of attribute type.



24
25
26
# File 'lib/shotgrid_api_ruby/entities.rb', line 24

def type
  @type
end

Instance Method Details

#all(fields: nil, logical_operator: 'and', sort: nil, filter: nil, page: nil, page_size: nil, retired: nil, include_archived_projects: nil) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/shotgrid_api_ruby/entities.rb', line 205

def all(
  fields: nil,
  logical_operator: 'and',
  sort: nil,
  filter: nil,
  page: nil,
  page_size: nil,
  retired: nil,
  include_archived_projects: nil
)
  if filter && !Params.filters_are_simple?(filter)
    return(
      search(
        fields: fields,
        logical_operator: logical_operator,
        sort: sort,
        filter: filter,
        page: page,
        page_size: page_size,
        retired: retired,
        include_archived_projects: include_archived_projects,
      )
    )
  end

  params = Params.new

  params.add_fields(fields)
  params.add_sort(sort)
  params.add_filter(filter)
  params.add_page(page, page_size)
  params.add_options(retired, include_archived_projects)

  resp = @connection.get('', params)
  resp_body = JSON.parse(resp.body)

  if resp.status >= 300
    raise ShotgridCallError.new(
            response: resp,
            message: "Error while getting #{type}: #{resp_body['errors']}",
          )
  end

  resp_body['data'].map do |entity|
    Entity.new(
      type: entity['type'],
      attributes: OpenStruct.new(entity['attributes']),
      relationships: entity['relationships'],
      id: entity['id'],
      links: entity['links'],
    )
  end
end

#count(filter: nil, logical_operator: 'and') ⇒ Object



365
366
367
# File 'lib/shotgrid_api_ruby/entities.rb', line 365

def count(filter: nil, logical_operator: 'and')
  summary_client.count(filter: filter, logical_operator: logical_operator)
end

#create(attributes) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/shotgrid_api_ruby/entities.rb', line 95

def create(attributes)
  resp =
    @connection.post('', attributes.to_json) do |req|
      req.headers['Content-Type'] = 'application/json'
    end

  resp_body = JSON.parse(resp.body)

  if resp.status >= 300
    raise ShotgridCallError.new(
            response: resp,
            message:
              "Error while creating #{type} with #{attributes}: #{
                resp_body['errors']
              }",
          )
  end

  entity = resp_body['data']
  Entity.new(
    type: entity['type'],
    attributes: OpenStruct.new(entity['attributes']),
    relationships: entity['relationships'],
    id: entity['id'],
    links: entity['links'],
  )
end

#delete(id) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/shotgrid_api_ruby/entities.rb', line 158

def delete(id)
  resp =
    @connection.delete(id.to_s) do |req|
      req.headers['Content-Type'] = 'application/json'
    end

  if resp.status >= 300
    resp_body = JSON.parse(resp.body)
    raise ShotgridCallError.new(
            response: resp,
            message:
              "Error while deleting #{type}##{id}: #{resp_body['errors']}",
          )
  end

  true
end

#fieldsObject



349
350
351
# File 'lib/shotgrid_api_ruby/entities.rb', line 349

def fields
  schema_client.fields
end

#find(id, fields: nil, retired: nil, include_archived_projects: nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/shotgrid_api_ruby/entities.rb', line 65

def find(id, fields: nil, retired: nil, include_archived_projects: nil)
  params = Params.new

  params.add_fields(fields)
  params.add_options(retired, include_archived_projects)

  resp = @connection.get(id.to_s, params)
  resp_body = JSON.parse(resp.body)

  if resp.status >= 300
    raise ShotgridCallError.new(
            message: "Error while getting #{type}: #{resp_body['errors']}",
            response: resp,
          )
  end

  entity = resp_body['data']
  Entity.new(
    type: entity['type'],
    attributes: OpenStruct.new(entity['attributes']),
    relationships: entity['relationships'],
    id: entity['id'],
    links: entity['links'],
  )
end

#first(fields: nil, sort: nil, filter: nil, retired: nil, include_archived_projects: nil, logical_operator: 'and') ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/shotgrid_api_ruby/entities.rb', line 37

def first(
  fields: nil,
  sort: nil,
  filter: nil,
  retired: nil,
  include_archived_projects: nil,
  logical_operator: 'and'
)
  all(
    fields: fields,
    sort: sort,
    filter: filter,
    retired: retired,
    logical_operator: logical_operator,
    include_archived_projects: include_archived_projects,
    page_size: 1,
  ).first
end

#revive(id) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/shotgrid_api_ruby/entities.rb', line 177

def revive(id)
  resp = @connection.post("#{id}?revive=true")

  if resp.status >= 300
    resp_body = JSON.parse(resp.body)
    raise ShotgridCallError.new(
            response: resp,
            message:
              "Error while reviving #{type}##{id}: #{resp_body['errors']}",
          )
  end

  true
end

#schemaObject



344
345
346
# File 'lib/shotgrid_api_ruby/entities.rb', line 344

def schema
  schema_client.read
end

#schema_clientObject



339
340
341
# File 'lib/shotgrid_api_ruby/entities.rb', line 339

def schema_client
  @schema_client ||= Schema.new(connection, type, @base_url_prefix)
end

#search(fields: nil, logical_operator: 'and', sort: nil, filter: nil, page: nil, page_size: nil, retired: nil, include_archived_projects: nil) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/shotgrid_api_ruby/entities.rb', line 272

def search(
  fields: nil,
  logical_operator: 'and',
  sort: nil,
  filter: nil,
  page: nil,
  page_size: nil,
  retired: nil,
  include_archived_projects: nil
)
  if filter.nil? || Params.filters_are_simple?(filter)
    return(
      all(
        fields: fields,
        logical_operator: logical_operator,
        sort: sort,
        filter: filter,
        page: page,
        page_size: page_size,
        retired: retired,
        include_archived_projects: include_archived_projects,
      )
    )
  end
  params = Params.new

  params.add_fields(fields)
  params.add_sort(sort)
  params.add_page(page, page_size)
  params.add_options(retired, include_archived_projects)
  params.add_filter(filter, logical_operator)

  # In search: The name is filters and not filter
  params[:filters] = params[:filter] if params[:filter]
  params.delete(:filter)

  resp =
    @connection.post('_search', params) do |req|
      req.headers['Content-Type'] =
        if params[:filters].is_a? Array
          'application/vnd+shotgun.api3_array+json'
        else
          'application/vnd+shotgun.api3_hash+json'
        end
      req.body = params.to_h.to_json
    end
  resp_body = JSON.parse(resp.body)

  if resp.status >= 300
    raise ShotgridCallError.new(
            response: resp,
            message: "Error while getting #{type}: #{resp_body['errors']}",
          )
  end

  resp_body['data'].map do |entity|
    Entity.new(
      type: entity['type'],
      attributes: OpenStruct.new(entity['attributes']),
      relationships: entity['relationships'],
      id: entity['id'],
      links: entity['links'],
    )
  end
end

#summarize(filter: nil, grouping: nil, summary_fields: nil, logical_operator: 'and', include_archived_projects: nil) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/shotgrid_api_ruby/entities.rb', line 379

def summarize(
  filter: nil,
  grouping: nil,
  summary_fields: nil,
  logical_operator: 'and',
  include_archived_projects: nil
)
  summary_client.summarize(
    filter: filter,
    grouping: grouping,
    summary_fields: summary_fields,
    logical_operator: logical_operator,
    include_archived_projects: include_archived_projects,
  )
end

#summary_clientObject



354
355
356
# File 'lib/shotgrid_api_ruby/entities.rb', line 354

def summary_client
  @summary_client ||= Summarize.new(connection, type, @base_url_prefix)
end

#update(id, changes) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/shotgrid_api_ruby/entities.rb', line 127

def update(id, changes)
  return find(id) if changes.empty?

  resp =
    @connection.put(id.to_s, changes.to_json) do |req|
      req.headers['Content-Type'] = 'application/json'
    end

  resp_body = JSON.parse(resp.body)

  if resp.status >= 300
    raise ShotgridCallError.new(
            response: resp,
            message:
              "Error while updating #{type}##{id} with #{changes}: #{
                resp_body['errors']
              }",
          )
  end

  entity = resp_body['data']
  Entity.new(
    type: entity['type'],
    attributes: OpenStruct.new(entity['attributes']),
    relationships: entity['relationships'],
    id: entity['id'],
    links: entity['links'],
  )
end