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(resource_type) ⇒ Resource

Returns a new instance of Resource.



7
8
9
10
# File 'lib/puree/resource.rb', line 7

def initialize(resource_type)
  @resource_type = resource_type
  @api_map = Puree::Map.new.get
end

Instance Method Details

#contentHash

Content

Returns:

  • (Hash)


78
79
80
# File 'lib/puree/resource.rb', line 78

def content
  @content ? @content : {}
end

#get(endpoint: nil, username: nil, password: nil, uuid: nil, id: nil) ⇒ HTTParty::Response

Get

Parameters:

  • endpoint (String) (defaults to: nil)
  • username (String) (defaults to: nil)
  • password (String) (defaults to: nil)
  • uuid (String) (defaults to: nil)
  • id (String) (defaults to: nil)

Returns:

  • (HTTParty::Response)


20
21
22
23
24
25
26
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
# File 'lib/puree/resource.rb', line 20

def get(endpoint:nil, username:nil, password:nil, uuid:nil, id:nil)
  # 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

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

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

#responseHTTParty::Response, Nil

Response, if get method has been called

Returns:

  • (HTTParty::Response)
  • (Nil)


60
61
62
# File 'lib/puree/resource.rb', line 60

def response
  @response ? @response : nil
end

#set_content(content) ⇒ Object

Set content

Parameters:

  • content (Hash)


67
68
69
70
71
72
73
# File 'lib/puree/resource.rb', line 67

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