Class: Shutterstock::Collection

Inherits:
Driver
  • Object
show all
Defined in:
lib/client/collection.rb

Constant Summary

Constants inherited from Driver

Driver::TRUTHY_JSON_VALUES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Driver

api, client, #client, #json_true?, #methods, #respond_to, #to_date

Constructor Details

#initialize(params = {}) ⇒ Collection

Returns a new instance of Collection.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/client/collection.rb', line 7

def initialize(params={})
	@hash         = params
	@id           = params["id"].to_i
	@name         = params["name"]
    @share_code   = params["share_code"]
	@share_url    = params["share_url"]
    @total_count  = params["total_item_count"].to_i
    @created      = to_date(params["created_time"])
    @updated      = to_date(params["updated_time"])
    @items_updated = to_date(params["items_updated_time"])
    @cover_item   = Image.new(params["cover_item"]) if params["cover_item"]
end

Instance Attribute Details

#cover_itemObject (readonly)

Returns the value of attribute cover_item.



3
4
5
# File 'lib/client/collection.rb', line 3

def cover_item
  @cover_item
end

#createdObject (readonly)

Returns the value of attribute created.



3
4
5
# File 'lib/client/collection.rb', line 3

def created
  @created
end

#hashObject (readonly)

Returns the value of attribute hash.



3
4
5
# File 'lib/client/collection.rb', line 3

def hash
  @hash
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/client/collection.rb', line 3

def id
  @id
end

#items_updatedObject (readonly)

Returns the value of attribute items_updated.



3
4
5
# File 'lib/client/collection.rb', line 3

def items_updated
  @items_updated
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/client/collection.rb', line 3

def name
  @name
end

#share_codeObject (readonly)

Returns the value of attribute share_code.



3
4
5
# File 'lib/client/collection.rb', line 3

def share_code
  @share_code
end

#share_urlObject (readonly)

Returns the value of attribute share_url.



3
4
5
# File 'lib/client/collection.rb', line 3

def share_url
  @share_url
end

#total_countObject (readonly)

Returns the value of attribute total_count.



3
4
5
# File 'lib/client/collection.rb', line 3

def total_count
  @total_count
end

#updatedObject (readonly)

Returns the value of attribute updated.



3
4
5
# File 'lib/client/collection.rb', line 3

def updated
  @updated
end

Class Method Details

.add_image(id:, image_id:) ⇒ Object

collection.add_image(1234556, image_id: 98765432)

Raises:



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/client/collection.rb', line 80

def self.add_image(id:, image_id:)

			raise IncorrectArguments, "collection id not provided" unless id
			raise IncorrectArguments, "image id not provided" unless image_id

  resp = client.request do |r|
    r.path            "/v2/images/collections/#{id}/items"
    r.method          :post
    r.body            ({ items: image_ids_to_array_of_hash(image_id)})
    r.success_status  204
  end
  self
end

.create(name) ⇒ Object

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/client/collection.rb', line 32

def self.create(name)
  raise IncorrectArguments, "collection name not provided" unless name

  resp = client.request do |r|
    r.path            "/v2/images/collections"
    r.method          :post
    r.body            ({ name: name })
    r.success_status  201
  end

  resp_hash = resp.body
  resp_hash.merge!("name" => name)
  self.new(resp_hash)
end

.destroy(id) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/client/collection.rb', line 66

def self.destroy(id)
  resp = client.request do |r|
    r.path            "/v2/images/collections/#{id}"
    r.method          :delete
    r.success_status  204
  end
  nil
end

.find(id) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/client/collection.rb', line 20

def self.find(id)
    resp = client.request do
      path "/v2/images/collections/#{id}"
      method :get
    end
    self.new(resp.body)
end

.items(id) ⇒ Object

Raises:



114
115
116
117
118
119
120
121
122
# File 'lib/client/collection.rb', line 114

def self.items(id)
  raise IncorrectArguments, "collection id not provided" unless id

  resp = client.request do |r|
    r.path            "/v2/images/collections/#{id}/items"
    r.method          :get
  end
  Images.new(resp.body["data"])
end

.listObject



47
48
49
50
51
52
53
# File 'lib/client/collection.rb', line 47

def self.list
    resp = client.request do
      path            "/v2/images/collections"
      method          :get
    end
    Collections.new(resp.body)
end

.remove_image(id:, image_id:) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/client/collection.rb', line 99

def self.remove_image(id:, image_id:)

    resp = client.request do |r|
      r.path            "/v2/images/collections/#{id}/items"
      r.method          :delete
      r.params          image_ids_to_hash_of_array(image_id)
      r.success_status  204
    end
    self
end

.update(id:, name:) ⇒ Object

Raises:



55
56
57
58
59
60
61
62
63
64
# File 'lib/client/collection.rb', line 55

def self.update(id:, name:)
			raise IncorrectArguments, "collection name not provided" unless name

  resp = client.request do |r|
    r.path            "/v2/images/collections/#{id}"
    r.method          :post
    r.body            ({ name: name })
    r.success_status  204
  end
end

Instance Method Details

#add_image(image_id) ⇒ Object

image_id can be a single or array of either image ids or Image objects



95
96
97
# File 'lib/client/collection.rb', line 95

def add_image(image_id)
  self.class.add_image({id: self.id, image_id: image_id})
end

#destroyObject



75
76
77
# File 'lib/client/collection.rb', line 75

def destroy
    self.class.destroy(self.id)
end

#findObject



28
29
30
# File 'lib/client/collection.rb', line 28

def find
	self.class.find(self.id)
end

#itemsObject



124
125
126
# File 'lib/client/collection.rb', line 124

def items
  self.class.items(self.id)
end

#remove_image(image_id) ⇒ Object



110
111
112
# File 'lib/client/collection.rb', line 110

def remove_image(image_id)
	self.class.remove_image(id: self.id, image_id: image_id)
end