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, endpoint: nil, username: nil, password: nil, basic_auth: nil) ⇒ Collection

Returns a new instance of Collection.

Parameters:

  • (defaults to: nil)
  • (defaults to: nil)
  • username [String]

  • password [String]

  • basic_auth [Boolean]



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,
               endpoint: nil,
               username: nil,
               password: nil,
               basic_auth: nil)
  @resource_type = resource
  @api_map = Puree::Map.new.get
  @endpoint = endpoint.nil? ? Puree.endpoint : endpoint
  @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

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

Gets an array of objects of resource type specified in constructor

Parameters:

  • limit [Integer]

  • offset [Integer]

  • created_start [String]

  • created_end [String]

  • modified_start [String]

  • modified_end [String]

  • full [Boolean]

Returns:



38
39
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
150
151
152
153
# File 'lib/puree/collection.rb', line 38

def get(
        limit:            20,
        offset:           0,
        created_start:    nil,
        created_end:      nil,
        modified_start:   nil,
        modified_end:     nil,
        full:             true,
        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,
      record_rendering: rendering
  }

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

  # strip any trailing slash
  @endpoint = @endpoint.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]
    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

  # @response = HTTParty.get(build_url, query: query, headers: headers)

  begin
    # p self.inspect
    # p build_url
    # p query
    # p headers
    # @response = HTTParty.get(build_url, query: query, headers: headers, timeout: 120)
    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!

    code = @response.code
    # body = @response.body
    # puts "#{self.class.name} #{code}"
      # puts "#{self.class.name} #{body}"

  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