Class: ResoTransport::BaseMetadata

Inherits:
Object
  • Object
show all
Defined in:
lib/reso_transport/base_metadata.rb

Direct Known Subclasses

Datasystem, Metadata

Constant Summary collapse

MIME_TYPES =
{
  xml: 'application/xml',
  json: 'application/json'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ BaseMetadata

Returns a new instance of BaseMetadata.



10
11
12
13
14
# File 'lib/reso_transport/base_metadata.rb', line 10

def initialize(client)
  @client = client
  @prefix = nil
  @classname = nil
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/reso_transport/base_metadata.rb', line 8

def client
  @client
end

Instance Method Details

#cacheObject



40
41
42
# File 'lib/reso_transport/base_metadata.rb', line 40

def cache
  @cache ||= client.send("#{prefix}_cache").new(cache_file)
end

#cache_fileObject



44
45
46
# File 'lib/reso_transport/base_metadata.rb', line 44

def cache_file
  @cache_file ||= client.send "#{prefix}_file"
end

#classnameObject



22
23
24
25
26
# File 'lib/reso_transport/base_metadata.rb', line 22

def classname
  raise 'classname not set' unless @classname

  @classname
end

#dataObject



32
33
34
35
36
37
38
# File 'lib/reso_transport/base_metadata.rb', line 32

def data
  if cache_file
    cache.read || cache.write(raw)
  else
    raw
  end
end

#parserObject



28
29
30
# File 'lib/reso_transport/base_metadata.rb', line 28

def parser
  @parser ||= Object::const_get("#{classname}Parser").new.parse(data)
end

#prefixObject



16
17
18
19
20
# File 'lib/reso_transport/base_metadata.rb', line 16

def prefix
  raise 'prefix not set' unless @prefix

  @prefix
end

#rawObject

Raises:



48
49
50
51
52
# File 'lib/reso_transport/base_metadata.rb', line 48

def raw
  return response.body if response.success?

  raise RequestError.new(request, response, classname)
end

#requestObject



58
59
60
61
62
# File 'lib/reso_transport/base_metadata.rb', line 58

def request
  return @request.to_h if @request.respond_to? :to_h

  {}
end

#responseObject



54
55
56
# File 'lib/reso_transport/base_metadata.rb', line 54

def response
  raise 'Must implement response method'
end