Class: Puree::Collection

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

Overview

Collection of resources

Instance Method Summary collapse

Constructor Details

#initialize(resource: nil, base_url: nil, username: nil, password: nil, basic_auth: nil) ⇒ Collection

Returns a new instance of Collection.

Parameters:

  • resource (Symbol) (defaults to: nil)
  • base_url (String) (defaults to: nil)
  • username (String) (defaults to: nil)
  • password (String) (defaults to: nil)
  • basic_auth (Boolean) (defaults to: nil)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/puree/collection.rb', line 12

def initialize(resource: nil,
               base_url: nil,
               username: nil,
               password: nil,
               basic_auth: nil)
  @resource_type = resource
  @api_map = Puree::Map.new.get
  @base_url = base_url.nil? ? Puree.base_url : base_url
  @basic_auth = basic_auth.nil? ? Puree.basic_auth : basic_auth
  if @basic_auth === true
    @username = username.nil? ? Puree.username : username
    @password = password.nil? ? Puree.password : password
  end
  @uuids = []
end

Instance Method Details

#countInteger

Count of records available for a resource type

Returns:

  • (Integer)


155
156
157
# File 'lib/puree/collection.rb', line 155

def count
  @count ||= get_count
end

#get(limit: 20, offset: 0, created_start: nil, created_end: nil, modified_start: nil, modified_end: nil, full: true, instance: false, rendering: :xml_long) ⇒ Array<Object>, Array<Resource subclass> Also known as: find

Gets an array of objects of resource type specified in constructor

Parameters:

  • limit (Integer) (defaults to: 20)
  • offset (Integer) (defaults to: 0)
  • created_start (String) (defaults to: nil)
  • created_end (String) (defaults to: nil)
  • modified_start (String) (defaults to: nil)
  • modified_end (String) (defaults to: nil)
  • full (Boolean) (defaults to: true)
  • instance (Boolean) (defaults to: false)

Returns:

  • (Array<Object>)
  • (Array<Resource subclass>)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
90
91
92
93
94
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/puree/collection.rb', line 40

def get(
        limit:            20,
        offset:           0,
        created_start:    nil,
        created_end:      nil,
        modified_start:   nil,
        modified_end:     nil,
        full:             true,
        instance:         false,
        rendering:        :xml_long
)

  @options = {
      basic_auth:       @basic_auth,
      latest_api:       true,
      resource_type:    @resource_type.to_sym,
      rendering:        :system,
      limit:            limit,
      offset:           offset,
      created_start:    created_start,
      created_end:      created_end,
      modified_start:   modified_start,
      modified_end:     modified_end,
      full:             full,
      instance:         instance,
      record_rendering: rendering
  }

  reset

  missing = missing_credentials
  if !missing.empty?
    missing.each do |m|
      puts "#{self.class.name}" + '#' + "#{__method__} missing #{m}"
    end
    exit
  end

  # strip any trailing slash
  @base_url = @base_url.sub(/(\/)+$/, '')

  headers = {}
  headers['Accept'] = 'application/xml'

  if @options[:basic_auth] === true
    @auth = Base64::strict_encode64(@username + ':' + @password)
    headers['Authorization'] = 'Basic ' + @auth
  end

  query = {}

  query['rendering'] = @options[:rendering]

  if @options[:limit] >= 0
    query['window.size'] = @options[:limit]
  end

  if @options[:offset]
    query['window.offset'] = @options[:offset]
  end

  if @options[:created_start]
    query['createdDate.fromDate'] = @options[:created_start]
  end

  if @options[:created_end]
    query['createdDate.toDate'] = @options[:created_end]
  end

  if @options[:modified_start]
    query['modifiedDate.fromDate'] = @options[:modified_start]
  end

  if @options[:modified_end]
    query['modifiedDate.toDate'] = @options[:modified_end]
  end

  if @options['rendering']
    query['rendering'] = @options['rendering']
  end

  begin
    url = build_url
    req = HTTP.headers accept: 'application/xml'
    if @options[:basic_auth]
      req = req.auth headers['Authorization']
    end
    @response = req.get(url, params: query)
    @doc = Nokogiri::XML @response.body
    @doc.remove_namespaces!

    @count = extract_count

  rescue HTTP::Error => e
    puts 'HTTP::Error '+ e.message
  end

  if @options[:full]
    collect_resource
  else
    data = []
    uuid.each do |u|
      o = {}
      o['uuid'] = u
      data << o
    end
    data
  end

end