Class: ShotgunApiRuby::Entities

Inherits:
Object
  • Object
show all
Defined in:
lib/shotgun_api_ruby/entities.rb,
lib/shotgun_api_ruby/entities/params.rb,
lib/shotgun_api_ruby/entities/schema.rb,
lib/shotgun_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.



5
6
7
8
9
10
# File 'lib/shotgun_api_ruby/entities.rb', line 5

def initialize(connection, type)
  @connection = connection.dup
  @type = type
  @base_url_prefix = @connection.url_prefix
  @connection.url_prefix = "#{@connection.url_prefix}/entity/#{type}"
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



12
13
14
# File 'lib/shotgun_api_ruby/entities.rb', line 12

def connection
  @connection
end

#typeObject (readonly)

Returns the value of attribute type.



12
13
14
# File 'lib/shotgun_api_ruby/entities.rb', line 12

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



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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/shotgun_api_ruby/entities.rb', line 131

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 "Error while getting #{type}: #{resp_body['errors']}"
  end

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

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



261
262
263
# File 'lib/shotgun_api_ruby/entities.rb', line 261

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

#create(attributes) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/shotgun_api_ruby/entities.rb', line 56

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 "Error while creating #{type} with #{attributes}: #{
            resp_body['errors']
          }"
  end

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

#delete(id) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/shotgun_api_ruby/entities.rb', line 106

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 "Error while deleting #{type}##{id}: #{resp_body['errors']}"
  end

  true
end

#fieldsObject



253
254
255
# File 'lib/shotgun_api_ruby/entities.rb', line 253

def fields
  schema_client.fields
end

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



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

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 "Error while getting #{type}: #{resp_body['errors']}"
  end

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

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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shotgun_api_ruby/entities.rb', line 14

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



120
121
122
123
124
125
126
127
128
129
# File 'lib/shotgun_api_ruby/entities.rb', line 120

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

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

  true
end

#schemaObject



249
250
251
# File 'lib/shotgun_api_ruby/entities.rb', line 249

def schema
  schema_client.read
end

#schema_clientObject



245
246
247
# File 'lib/shotgun_api_ruby/entities.rb', line 245

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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
# File 'lib/shotgun_api_ruby/entities.rb', line 182

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 "Error while getting #{type}: #{resp_body['errors']}"
  end

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

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



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/shotgun_api_ruby/entities.rb', line 265

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



257
258
259
# File 'lib/shotgun_api_ruby/entities.rb', line 257

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

#update(id, changes) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/shotgun_api_ruby/entities.rb', line 80

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 "Error while updating #{type}##{id} with #{changes}: #{
            resp_body['errors']
          }"
  end

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