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, base_url: nil, username: nil, password: nil, bleeding: true, basic_auth: nil) ⇒ Resource



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

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

#createdString

Created (UTC datetime)



111
112
113
# File 'lib/puree/resource.rb', line 111

def created
  ['created']
end

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

Get



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 43

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

  @options[:rendering] = rendering
  @options[:uuid] = uuid
  @options[: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
  @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[: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)
    make_doc @response.body

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

  get_data? ?  : {}

end

#modifiedString

Modified (UTC datetime)



118
119
120
# File 'lib/puree/resource.rb', line 118

def modified
  ['modified']
end

#set_content(xml) ⇒ Object

Set content from XML. In order for metadata extraction to work, the XML must have been retrieved using the .current version of the Pure API endpoints



126
127
128
129
130
131
132
133
# File 'lib/puree/resource.rb', line 126

def set_content(xml)
  if xml
    make_doc xml
    if get_data?
      
    end
  end
end

#uuidString

UUID



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

def uuid
  ['uuid']
end