Class: SDSActiveResource::SDSConnection

Inherits:
ActiveResource::Connection
  • Object
show all
Defined in:
lib/sds-activeresource.rb

Instance Method Summary collapse

Instance Method Details

#Boolean(string) ⇒ Object

Raises:

  • (ArgumentError)


176
177
178
179
180
# File 'lib/sds-activeresource.rb', line 176

def Boolean(string)
    return true if string == true || string =~ /^true$/i
    return false if string == false || string.nil? || string =~ /^false$/i
    raise ArgumentError.new("invalid value for Boolean: \"#{string}\"")
end

#delete(path, body = '', headers = {}) ⇒ Object



64
65
66
67
68
# File 'lib/sds-activeresource.rb', line 64

def delete(path, body = '', headers = {})
  container = get_container(path)
  id = get_id(path)
  service.delete_entity(container, id)
end

#get(path, headers = {}) ⇒ Object



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
# File 'lib/sds-activeresource.rb', line 77

def get(path, headers = {})
  container = get_container(path)
  entityname = get_entity(path)
  id = get_id(path)
  if(id.nil?)
    query = 'from e in entities where e.Kind == "' + entityname.singularize + '" select e'
    response = service.query(container, query)
    entity = REXML::Document.new(response.body)
    entities = []
    
    entity.root().elements.each { |element| 
      options = {}      
      element.elements.each { |element2|       
        options[element2.name] = parse_value(element2.attributes["xsi:type"], element2.text)
      }
      entities.push(options)
      }

    entities        
  else
    
  response = service.get_entity(container, id)
      
  entity = REXML::Document.new(response.body)
 
  options = {}      
  entity.root().elements.each { |element|      
      options[element.name] = parse_value(element.attributes["xsi:type"], element.text)
  }
  options['id'] = id
  options
  
  end
end

#get_authority(path) ⇒ Object



112
113
114
115
# File 'lib/sds-activeresource.rb', line 112

def get_authority(path)
 splitpath = URI::split(path.to_s)
 splitpath[2].split('.')[0]
end

#get_container(path) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/sds-activeresource.rb', line 132

def get_container(path)
  container = path.to_s.split('/')[1]
  
  if(container.nil?)
    raise "no container found"
  end
  container
end

#get_entity(path) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/sds-activeresource.rb', line 141

def get_entity(path)
  path.delete! '.xml'
  entity = path.split('/')[2]
  
  if(entity.include? "?")
    entity = entity.split('?')[0]  
  end
  
  if(entity.nil?)
    raise "no entity found"
  end
  entity
end

#get_id(path) ⇒ Object



171
172
173
174
# File 'lib/sds-activeresource.rb', line 171

def get_id(path)
  path.delete! '.xml'
  path.to_s.split('/')[3]
end

#get_params(path) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/sds-activeresource.rb', line 124

def get_params(path)
  puts path
  if(path.include? "?")
    params = path.split('?')[1]  
    params || ""
  end
end

#get_url(path) ⇒ Object



117
118
119
120
121
122
# File 'lib/sds-activeresource.rb', line 117

def get_url(path)
  splitpath = URI::split(path.to_s)
  host = splitpath[2]
  host[get_authority(path) + "."] = ""
  host
end

#parse_value(type, value) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/sds-activeresource.rb', line 155

def parse_value(type, value)
  if(type == "x:decimal")
    if(value.include?("."))
      Float(value)
    else
      Integer(value)
    end
  elsif(type == "x:boolean")
    Boolean(value)
  elsif(type == "x:dateTime")
    DateTime.parse(value)
  else
    value
  end    
end

#post(path, body = '', headers = {}) ⇒ Object



57
58
59
60
61
62
# File 'lib/sds-activeresource.rb', line 57

def post(path, body = '', headers = {})
  container = get_container(path)
  response = service.create_entity(container, body["entityname"], body["Id"], body)
  response['location'] = "test/" + body["Id"].to_s
  response
end

#put(path, body = '', headers = {}) ⇒ Object



70
71
72
73
74
75
# File 'lib/sds-activeresource.rb', line 70

def put(path, body = '', headers = {})
  container = get_container(path)  
  response = service.update_entity(container, body["name"], body["Id"], nil, body)
  response['location'] = "test/" + body["Id"].to_s
  response
end

#query(path, query) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sds-activeresource.rb', line 39

def query(path, query)
  container = get_container(path)
  query.gsub!(/AND/i, "%26%26 ")
  response = service.query(container, query)
  entity = REXML::Document.new(response.body)
  entities = []
  
  entity.root().elements.each { |element| 
    options = {}      
    element.elements.each { |element2|       
      options[element2.name] = parse_value(element.attributes["xsi:type"], element2.text)
    }
    entities.push(options)
    }

  entities
end

#serviceObject



34
35
36
37
# File 'lib/sds-activeresource.rb', line 34

def service
  @service = SDSRest::Service.new :username => user, :password => password, :authority => get_authority(site), :url => get_url(site) if @service.nil?
  @service
end