Class: Ey::Core::Collection

Inherits:
Cistern::Collection
  • Object
show all
Defined in:
lib/ey-core/collection.rb

Direct Known Subclasses

Ey::Core::Client::AccountCancellations, Ey::Core::Client::AccountReferrals, Ey::Core::Client::Accounts, Ey::Core::Client::Addons, Ey::Core::Client::Addresses, Ey::Core::Client::Alerts, Ey::Core::Client::ApplicationArchives, Ey::Core::Client::ApplicationDeployments, Ey::Core::Client::Applications, Ey::Core::Client::BackupFiles, Ey::Core::Client::Blueprints, Ey::Core::Client::Components, Ey::Core::Client::Contacts, Ey::Core::Client::Costs, Ey::Core::Client::DatabasePlanUsages, Ey::Core::Client::DatabaseServerRevisions, Ey::Core::Client::DatabaseServerSnapshots, Ey::Core::Client::DatabaseServerUsages, Ey::Core::Client::DatabaseServers, Ey::Core::Client::DatabaseServices, Ey::Core::Client::Deployments, Ey::Core::Client::EnvironmentPlanUsages, Ey::Core::Client::Environments, Ey::Core::Client::Features, Ey::Core::Client::FirewallRules, Ey::Core::Client::Firewalls, Ey::Core::Client::Gems, Ey::Core::Client::KeypairDeployments, Ey::Core::Client::Keypairs, Ey::Core::Client::LegacyAlerts, Ey::Core::Client::LoadBalancerNodes, Ey::Core::Client::LoadBalancerServices, Ey::Core::Client::LoadBalancers, Ey::Core::Client::LogicalDatabases, Ey::Core::Client::Logs, Ey::Core::Client::Memberships, Ey::Core::Client::Messages, Ey::Core::Client::PlanUsages, Ey::Core::Client::ProviderLocations, Ey::Core::Client::Providers, Ey::Core::Client::Requests, Ey::Core::Client::ServerEvents, Ey::Core::Client::ServerUsages, Ey::Core::Client::Servers, Ey::Core::Client::SslCertificates, Ey::Core::Client::StorageUsers, Ey::Core::Client::Storages, Ey::Core::Client::Tasks, Ey::Core::Client::Tokens, Ey::Core::Client::UntrackedAddresses, Ey::Core::Client::UntrackedServers, Ey::Core::Client::Users, Ey::Core::Client::Volumes

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.collection_requestObject

Returns the value of attribute collection_request.



3
4
5
# File 'lib/ey-core/collection.rb', line 3

def collection_request
  @collection_request
end

.collection_rootObject

Returns the value of attribute collection_root.



3
4
5
# File 'lib/ey-core/collection.rb', line 3

def collection_root
  @collection_root
end

.model_requestObject

Returns the value of attribute model_request.



3
4
5
# File 'lib/ey-core/collection.rb', line 3

def model_request
  @model_request
end

.model_rootObject

Returns the value of attribute model_root.



3
4
5
# File 'lib/ey-core/collection.rb', line 3

def model_root
  @model_root
end

Instance Method Details

#==(comparison_object) ⇒ Object



63
64
65
66
67
# File 'lib/ey-core/collection.rb', line 63

def ==(comparison_object)
  comparison_object.equal?(self) ||
    (comparison_object.is_a?(self.class) &&
     comparison_object.map(&:identity) == self.map(&:identity))
end

#all(params = {}) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/ey-core/collection.rb', line 69

def all(params={})
  params["url"] ||= self.url

  self.load(
    self.connection.send(self.collection_request, Cistern::Hash.stringify_keys(params))
  )
end

#collection_requestObject



47
48
49
# File 'lib/ey-core/collection.rb', line 47

def collection_request
  self.class.instance_variable_get(:@collection_request)
end

#collection_rootObject



43
44
45
# File 'lib/ey-core/collection.rb', line 43

def collection_root
  self.class.instance_variable_get(:@collection_root)
end

#create!(*args) ⇒ Object



12
13
14
15
# File 'lib/ey-core/collection.rb', line 12

def create!(*args)
  model = self.new(*args)
  model.save!
end

#each_entryObject



101
102
103
104
105
106
107
108
# File 'lib/ey-core/collection.rb', line 101

def each_entry
  return to_enum(:each_entry) unless block_given?
  page = self
  while page
    page.to_a.each { |r| yield r }
    page = page.next_page
  end
end

#each_pageObject



92
93
94
95
96
97
98
99
# File 'lib/ey-core/collection.rb', line 92

def each_page
  return to_enum(:each_page) unless block_given?
  page = self
  while page
    yield page
    page = page.next_page
  end
end

#first(params = {}) ⇒ Object



77
78
79
# File 'lib/ey-core/collection.rb', line 77

def first(params={})
  params.empty? ? super() : all(params).to_a.first
end

#get(id) ⇒ Object



37
38
39
40
41
# File 'lib/ey-core/collection.rb', line 37

def get(id)
  get!(id)
rescue Ey::Core::Response::NotFound
  nil
end

#get!(id) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/ey-core/collection.rb', line 29

def get!(id)
  if data = perform_get("id" => id)
    new(data)
  else
    nil
  end
end

#last_pageObject



59
60
61
# File 'lib/ey-core/collection.rb', line 59

def last_page
  all("url" => self.last_link)
end

#load(data) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/ey-core/collection.rb', line 123

def load(data)
  if data.is_a?(Ey::Core::Response)
    self.merge_attributes(page_parameters(data))
    super(data.body[self.collection_root])
  else
    super
  end
end

#model_requestObject



21
22
23
# File 'lib/ey-core/collection.rb', line 21

def model_request
  self.class.model_request
end

#model_rootObject



17
18
19
# File 'lib/ey-core/collection.rb', line 17

def model_root
  self.class.model_root
end

#new_pageObject



119
120
121
# File 'lib/ey-core/collection.rb', line 119

def new_page
  self.class.new(connection: self.connection)
end

#next_pageObject



51
52
53
# File 'lib/ey-core/collection.rb', line 51

def next_page
  new_page.all("url" => self.next_link) if self.next_link
end

#oneObject

asserts the results contain only a single record and returns it



82
83
84
85
86
87
88
89
90
# File 'lib/ey-core/collection.rb', line 82

def one
  if size == 0
    raise "Could not find any records"
  elsif size > 1
    raise "Search returned multiple records when only one was expected"
  else
    first
  end
end

#page_parameters(response) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/ey-core/collection.rb', line 110

def page_parameters(response)
  links = (response.headers['Link'] || "").split(", ").inject({}) do |r, link|
    value, key = link.match(/<(.*)>; rel="(\w+)"/).captures
    r.merge("#{key}_link" => value)
  end

  links.merge(total_count: response.headers['X-Total-Count'])
end

#perform_get(params) ⇒ Object



25
26
27
# File 'lib/ey-core/collection.rb', line 25

def perform_get(params)
  connection.send(self.model_request, params).body[self.model_root]
end

#previous_pageObject



55
56
57
# File 'lib/ey-core/collection.rb', line 55

def previous_page
  new_page.all("url" => self.prev_link) if self.prev_link
end