Class: Puree::Resource

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

Overview

Abstract base class for resources.

Instance Method Summary collapse

Constructor Details

#initialize(api: nil, endpoint: nil, username: nil, password: nil) ⇒ Resource

Returns a new instance of Resource.

Parameters:

  • api (String) (defaults to: nil)
  • endpoint (String) (defaults to: nil)
  • optional

    username [String]

  • optional

    password [String]



11
12
13
14
15
16
17
18
19
20
# File 'lib/puree/resource.rb', line 11

def initialize( api: nil,
                endpoint: nil,
                username: nil,
                password: nil)
  @resource_type = api
  @api_map = Puree::Map.new.get
  @endpoint = endpoint.nil? ? Puree::Configuration.endpoint : endpoint
  @username = username.nil? ? Puree::Configuration.username : username
  @password = password.nil? ? Puree::Configuration.password : password
end

Instance Method Details

#contentHash

Content

Returns:

  • (Hash)


98
99
100
# File 'lib/puree/resource.rb', line 98

def content
  @content ? @content : {}
end

#createdString

Created (UTC datetime)

Returns:

  • (String)


105
106
107
108
# File 'lib/puree/resource.rb', line 105

def created
  data = node 'created'
  !data.nil? && !data.empty? ? data.strip : ''
end

#get(uuid: nil, id: nil) ⇒ HTTParty::Response Also known as: find

Get

Parameters:

  • uuid (String) (defaults to: nil)
  • id (String) (defaults to: nil)

Returns:

  • (HTTParty::Response)


27
28
29
30
31
32
33
34
35
36
37
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
# File 'lib/puree/resource.rb', line 27

def get(uuid: nil, id: nil)
  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(/(\/)+$/, '')
  @auth = Base64::strict_encode64(@username + ':' + @password)

  @options = {
      latest_api:     true,
      resource_type: @resource_type.to_sym,
      rendering:      :xml_long,
      uuid:           uuid,
      id:             id
  }
  headers = {
      'Accept' => 'application/xml',
      'Authorization' => 'Basic ' + @auth
  }
  query = {}
  query['rendering'] = @options[:rendering]

  if @options[:uuid]
    query['uuids.uuid'] = @options[:uuid]
  else
    if @options[:id]
      query['pureInternalIds.id'] = @options[:id]
    end
  end

  begin
    @response = HTTParty.get(build_url, query: query, headers: headers, timeout: 120)
  rescue HTTParty::Error => e
    puts 'HttParty::Error '+ e.message
  end

  if get_data?
    response_name = service_response_name
    content = @response.parsed_response[response_name]['result']['content']
    set_content(content)
  end
  @response
end

#metadataHash

All metadata

Returns:

  • (Hash)


129
130
131
132
133
134
135
# File 'lib/puree/resource.rb', line 129

def 
  o = {}
  o['uuid'] = uuid
  o['created'] = created
  o['modified'] = modified
  o
end

#modifiedString

Modified (UTC datetime)

Returns:

  • (String)


113
114
115
116
# File 'lib/puree/resource.rb', line 113

def modified
  data = node 'modified'
  !data.nil? && !data.empty? ? data.strip : ''
end

#responseHTTParty::Response, Nil

Response, if get method has been called

Returns:

  • (HTTParty::Response)
  • (Nil)


80
81
82
# File 'lib/puree/resource.rb', line 80

def response
  @response ? @response : nil
end

#set_content(content) ⇒ Object

Set content

Parameters:

  • content (Hash)


87
88
89
90
91
92
93
# File 'lib/puree/resource.rb', line 87

def set_content(content)
  if !content.nil? && !content.empty?
    @content = content
  else
    @content = {}
  end
end

#uuidString

UUID

Returns:

  • (String)


121
122
123
124
# File 'lib/puree/resource.rb', line 121

def uuid
  data = node 'uuid'
  !data.nil? && !data.empty? ? data.strip : ''
end