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

Returns a new instance of Resource.

Parameters:

  • api (Symbol) (defaults to: nil)
  • base_url (String) (defaults to: nil)
  • username (String) (defaults to: nil)
  • password (String) (defaults to: nil)
  • bleeding (Boolean) (defaults to: true)
  • basic_auth (Boolean) (defaults to: nil)


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
  @metadata = {}
  @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)

Returns:

  • (String)


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

def created
  @metadata['created']
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)


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

#localeString

Locale (e.g. en-GB)

Returns:

  • (String)


125
126
127
# File 'lib/puree/resource.rb', line 125

def locale
  @metadata['locale']
end

#modifiedString

Modified (UTC datetime)

Returns:

  • (String)


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

def modified
  @metadata['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

Parameters:

  • xml (String)


133
134
135
136
137
138
139
140
# File 'lib/puree/resource.rb', line 133

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

#uuidString

UUID

Returns:

  • (String)


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

def uuid
  @metadata['uuid']
end