Class: RemoteDwsRegistry

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

Instance Method Summary collapse

Constructor Details

#initialize(url2_base = nil, domain: '127.0.0.1', port: '9292', url_base: "http://#{domain}:#{port}/", debug: false) ⇒ RemoteDwsRegistry

Returns a new instance of RemoteDwsRegistry.



16
17
18
19
20
21
22
23
24
25
# File 'lib/remote_dwsregistry.rb', line 16

def initialize(url2_base=nil, domain: '127.0.0.1', 
               port: '9292', url_base: "http://#{domain}:#{port}/", 
               debug: false)

  @url_base = url2_base || url_base
  @url_base += '/' unless url_base[-1] == '/'
  @req = GPDRequest.new
  @debug = debug

end

Instance Method Details

#delete_key(key) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/remote_dwsregistry.rb', line 27

def delete_key(key)

  r = @req.get(@url_base + key, {action: 'delete_key'})    

  case r.content_type
  when 'application/json'

    JSON.parse r.body

  else

    r.body
  
  end

end

#gem_register(gemfile) ⇒ Object



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
# File 'lib/remote_dwsregistry.rb', line 44

def gem_register(gemfile)
  
  if gemfile =~ /^\w+\:\/\// then
    
    puts 'about to remotely request gemfile' if @debug
    code = Requestor.read(File.dirname(gemfile)) do |x| 
      x.require File.basename(gemfile)
    end
    
    eval code
    
  else
    
    require gemfile
    
  end
  
  if defined? RegGem::register then
    
    reg = RegGem::register
    puts 'importing registry' + reg.inspect
    import reg 
    
  else
    nil
  end
  
end

#get_key(key = '', auto_detect_type: false) ⇒ Object



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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/remote_dwsregistry.rb', line 73

def get_key(key='', auto_detect_type: false)

  r = @req.get(@url_base + key)    
  
  puts 'r.content_type: ' +  r.content_type.inspect if @debug
  
  case r.content_type
  when 'application/xml'

    doc = Rexle.new(r.body)
    e = doc.root
    
   
    if auto_detect_type == false then
      
      def e.to_os()
         OpenStruct.new self.elements.inject({}) \
             {|r,x| r.merge(x.name => x.text) }
      end 
    
      return e         
    end
      
    
    c = e.attributes[:type]
    s = e.text

    return e if e.elements.length > 0 or s.nil?    
    return s.to_s unless c
          
    h = {
      string: ->(x) {x},
      boolean: ->(x){ 
        case x
        when 'true' then true
        when 'false' then false
        when 'on' then true
        when 'off' then false
        else x
        end
      },
      number: ->(x){  x[/^[0-9]+$/] ? x.to_i : x.to_f },
      time:   ->(x) {Time.parse x},
      json:   ->(x) {JSON.parse x}
    }
                            
    h[c.to_sym].call s         

  when 'application/json'

    h = JSON.parse r.body
    return nil if h == {get_key: 'key not found'}

  else

    r.body
  
  end
  
  

end

#get_keys(key) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/remote_dwsregistry.rb', line 136

def get_keys(key)

  r = @req.get(@url_base + key, {action: 'get_keys'})    

  case r.content_type
  when 'application/xml'

    doc = Rexle.new(r.body)
    
    if doc.root.elements then
      doc.root.elements.to_a 
    else
      []
    end

  when 'application/json'

    JSON.parse r.body

  else

    r.body
  
  end

end

#import(s) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/remote_dwsregistry.rb', line 163

def import(s)

  url = @url_base + 'import'

  r = @req.post(url, 's' => s)

  case r.content_type
  when 'application/xml'

    doc = Rexle.new(r.body)
    doc.root

  when 'application/json'

    JSON.parse r.body

  else

    r.body
  
  end

end

#set_key(key, val) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/remote_dwsregistry.rb', line 187

def set_key(key, val)

  url = @url_base + key

  r = @req.post(url, 'v' => val)

  case r.content_type
  when 'application/xml'

    doc = Rexle.new(r.body)
    doc.root

  when 'application/json'

    JSON.parse r.body

  else

    r.body
  
  end

end

#xmlObject



211
212
213
214
# File 'lib/remote_dwsregistry.rb', line 211

def xml()
  r = get_key()
  r.xml if r.is_a? Rexle::Element
end

#xpath(path) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/remote_dwsregistry.rb', line 216

def xpath(path)
  
  r = @req.get(@url_base, {xpath: path})

  case r.content_type
  when 'application/xml'

    doc = Rexle.new(r.body)
    
    if doc.root.elements then
      doc.root.elements.to_a 
    else
      []
    end

  when 'application/json'

    JSON.parse r.body

  else

    r.body
  
  end
end