Class: RGovData::Service

Inherits:
Object
  • Object
show all
Includes:
CommonConfig, Dn
Defined in:
lib/rgovdata/service/service.rb

Overview

A Service describes a specific service It encapsulates access to the underlying service implementation

Direct Known Subclasses

FileService, OdataService

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dn

#attributes, #id, #initialization_hash, #records, #to_param, #to_s

Methods included from CommonConfig

#config, #require_config_file

Constructor Details

#initialize(options) ⇒ Service

new requires +options may be a RGovData::ServiceListing or a Hash If options is a hash, it requires the following members: uri type transport credentialset



35
36
37
38
39
40
41
42
43
# File 'lib/rgovdata/service/service.rb', line 35

def initialize(options)
  @options = if options.is_a?(Hash)
    OpenStruct.new(options)
  elsif options.class <= RGovData::ServiceListing
    options.dup # avoid circular refs
  else
    OpenStruct.new
  end
end

Instance Attribute Details

#native_instanceObject (readonly)

Returns the native service object if applicable By default, returns self



10
11
12
# File 'lib/rgovdata/service/service.rb', line 10

def native_instance
  @native_instance
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/rgovdata/service/service.rb', line 9

def options
  @options
end

Class Method Details

.get_instance(options = {}) ⇒ Object

Returns the appropriate Service class for the given uri and type +options may be a RGovData::ServiceListing or a Hash If options is a hash, it requires the following members: uri type credentialset



19
20
21
22
23
24
25
# File 'lib/rgovdata/service/service.rb', line 19

def get_instance(options={})
  type = (options.class <= RGovData::ServiceListing) ? options.type : options[:type]
  service_class = "RGovData::#{type.to_s.capitalize}Service".constantize
  service_class.new(options)
rescue # invalid or not a supported type
  nil
end

Instance Method Details

#credentialsetObject



51
# File 'lib/rgovdata/service/service.rb', line 51

def credentialset ; options.credentialset ; end

#dataset_keysObject

Returns an array of DataSets (keys) for the service

> needs to be overridden for each service type



76
77
78
# File 'lib/rgovdata/service/service.rb', line 76

def dataset_keys
  []
end

#datasetsObject

Returns an array of DataSets for the service

> may need to be overridden for a specific service type



67
68
69
70
71
72
# File 'lib/rgovdata/service/service.rb', line 67

def datasets
  dataset_class = "RGovData::#{type.to_s.capitalize}DataSet".constantize
  @datasets ||= dataset_class.load_datasets(self)
rescue
  []
end

#find(id) ⇒ Object Also known as: find_by_id

Returns the first dataset matching key



87
88
89
# File 'lib/rgovdata/service/service.rb', line 87

def find(id)
  Array(get_dataset(id)).first
end

#get_dataset(key) ⇒ Object

Returns the dataset(s) matching key



81
82
83
84
85
# File 'lib/rgovdata/service/service.rb', line 81

def get_dataset(key)
  return nil unless datasets && !datasets.empty?
  matches = datasets.select {|s| s.dataset_key =~ /#{key}/}
  matches.count == 1 ? matches.first : matches
end

#meta_attributesObject

Returns array of attributes that describe the specific entity

> overrides RGovData::Dn.meta_attributes



55
56
57
# File 'lib/rgovdata/service/service.rb', line 55

def meta_attributes
  [:id,:realm,:service_key,:uri,:type,:transport,:credentialset]
end

#realmObject

attribute accessors



46
# File 'lib/rgovdata/service/service.rb', line 46

def realm         ; options.realm         ; end

#service_keyObject



47
# File 'lib/rgovdata/service/service.rb', line 47

def service_key   ; options.service_key   ; end

#transportObject



50
# File 'lib/rgovdata/service/service.rb', line 50

def transport     ; options.transport     ; end

#typeObject



49
# File 'lib/rgovdata/service/service.rb', line 49

def type          ; options.type          ; end

#uriObject



48
# File 'lib/rgovdata/service/service.rb', line 48

def uri           ; options.uri           ; end