Class: Puree::Resource

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

Overview

Abstract base class for resources.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api: nil, endpoint: nil, username: nil, password: nil, bleeding: true, basic_auth: 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]

  • optional

    bleeding [Boolean]

  • optional

    basic_auth [Boolean]



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/puree/resource.rb', line 15

def initialize( api: nil,
                endpoint: nil,
                username: nil,
                password: nil,
                bleeding: true,
                basic_auth: nil)
  @resource_type = api
  @api_map = Puree::Map.new.get
  @endpoint = endpoint.nil? ? Puree.endpoint : endpoint
  @latest_api = bleeding
  @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
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#contentHash

Content

Returns:

  • (Hash)


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

def content
  @content ? @content : {}
end

#createdString

Created (UTC datetime)

Returns:

  • (String)


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

def created
  path = '/created'
  xpath_query_for_single_value path
end

#get(uuid: nil, id: nil, rendering: :xml_long) ⇒ Hash Also known as: find

Get

Parameters:

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

Returns:

  • (Hash)


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
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
# File 'lib/puree/resource.rb', line 37

def get(uuid: nil, id: nil, rendering: :xml_long)
  reset

  @options = {
      basic_auth:     @basic_auth,
      latest_api:     @latest_api,
      resource_type:  @resource_type.to_sym,
      rendering:      rendering,
      uuid:           uuid,
      id:             id
  }

  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[:uuid]
    query['uuids.uuid'] = @options[:uuid]
  else
    if @options[:id]
      query['pureInternalIds.id'] = @options[:id]
    end
  end

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

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

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

  get_data? ?  : {}

end

#metadataHash

All metadata

Returns:

  • (Hash)


146
147
148
149
150
151
152
# File 'lib/puree/resource.rb', line 146

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

#modifiedString

Modified (UTC datetime)

Returns:

  • (String)


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

def modified
  path = '/modified'
  xpath_query_for_single_value path
end

#set_content(content) ⇒ Object

Set content

Parameters:

  • content (Hash)


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

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

#uuidString

UUID

Returns:

  • (String)


138
139
140
141
# File 'lib/puree/resource.rb', line 138

def uuid
  path = '/@uuid'
  xpath_query_for_single_value path
end