Class: AbiquoAPIClient::LinkCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/abiquo-api/collection.rb

Overview

Represents a collection of resources in the Abiquo API.

Instance Method Summary collapse

Constructor Details

#initialize(parsed_response, type, client) ⇒ LinkCollection

Returns a new instance of LinkCollection.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/abiquo-api/collection.rb', line 8

def initialize(parsed_response, type, client)
  @size = parsed_response['totalSize'].nil? ? parsed_response['collection'].count : parsed_response['totalSize']
  if type.include? ";"
    @type = type.split(';').first
  else
    @type = type
  end

  unless parsed_response['links'].empty?
    coluri = URI.parse(parsed_response['links'].first['href'])
    @path = coluri.path

    opts = coluri.query
    unless opts.nil?
      opt_hash = opts.split("&").map{|e| { e.split("=").first.to_sym => e.split("=").last }}.reduce({}) {|h,pairs| pairs.each {|k,v| h[k] ||= v}; h}
      @page_size = opt_hash[:limit].to_i

      st = opt_hash[:startwith].nil? ? 0 : opt_hash[:startwith].to_i
      @current_page = case
        when @page_size == 0 then st
        when @page_size  > 0 then (st / @page_size) + 1
        end
    end

    @links = parsed_response['links']
  end

  @collection = parsed_response['collection'].map {|r| client.new_object(r)}

  @client = client
end

Instance Method Details

#eachObject

Iterates the collection



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/abiquo-api/collection.rb', line 113

def each
  if block_given?
    unless @current_page == 1 or @current_page.nil?
      next_page = retrieve('first')
      @collection = next_page.nil? ? [] : next_page
    end

    loop do
      @collection.each do |item|
        yield item
      end

      break if @links.nil? or @links.select {|l| l['rel'].eql? "next" }.first.nil?

      next_page = retrieve('next')
      @collection = next_page.nil? ? [] : next_page
    end
  else
    self.to_enum
  end
end

#first(count = nil) ⇒ Object

Returns the first element in the collection



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/abiquo-api/collection.rb', line 51

def first(count = nil)
  if count.nil?
    @collection.first
  else
    out = []
    @collection.first(count).each do |item|
      out << item
    end
    out
  end
end

#inspectObject

Pretty print the object.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/abiquo-api/collection.rb', line 138

def inspect
  Thread.current[:formatador] ||= Formatador.new
  data = "#{Thread.current[:formatador].indentation}<#{self.class.name}"
  Thread.current[:formatador].indent do
    unless self.instance_variables.empty?
      vars = self.instance_variables.clone
      vars.delete(:@client)
      vars.delete(:@page)
      data << " "
      data << vars.map { |v| "#{v}=#{instance_variable_get(v.to_s).inspect}" }.join(", ")
    end
  end
  data << " >"
  data
end

#lastObject

Returns the last element in the collection



66
67
68
69
70
71
72
# File 'lib/abiquo-api/collection.rb', line 66

def last
  out = nil

  each {|i| out = i }

  out
end

#mapObject Also known as: collect

Returns an array resulting of applying the provided block to all of the elements of the collection



101
102
103
104
105
106
107
# File 'lib/abiquo-api/collection.rb', line 101

def map
  out = []

  each { |e| out << yield(e) }

  out
end

#selectObject

Selects elements of the collections for which the supplied block evaluates to true



89
90
91
92
93
94
95
# File 'lib/abiquo-api/collection.rb', line 89

def select
  out = []

  each { |e| out << e if yield(e) }

  out
end

#sizeObject Also known as: count

Returns the total size of the collection



43
44
45
# File 'lib/abiquo-api/collection.rb', line 43

def size
  @size
end

#to_aObject

Returns an array representing the collection



77
78
79
80
81
82
83
# File 'lib/abiquo-api/collection.rb', line 77

def to_a
  out = []

  each { |e| out << e }

  out
end