Class: SDSRest::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/sds-rest.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Service

Returns a new instance of Service.



21
22
23
24
25
26
# File 'lib/sds-rest.rb', line 21

def initialize(options={})
  @username = options[:username] || ENV['username']
  @password = options[:password] || ENV['password']
  @url = options[:url] || ENV['url']
  @authority = options[:authority] || ENV['authority']
end

Instance Method Details

#authority=(value) ⇒ Object



28
29
30
# File 'lib/sds-rest.rb', line 28

def authority=(value)
  @authority = value
end

#create_authority(authority) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/sds-rest.rb', line 32

def create_authority(authority)   
  request_xml = REXML::Document.new()
  request_xml.add_element('s:Authority')
  request_xml.root().add_attribute('xmlns:s', 'http://schemas.microsoft.com/sitka/2008/03/')
  request_xml.root().add_element("s:Id")
  request_xml.root().elements["s:Id"].text = authority
  req = create_post(request_xml)
  execute_request req
end

#create_blob(container, blob, id) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/sds-rest.rb', line 126

def create_blob(container, blob, id)
  url = get_request_url(container)
  req = Net::HTTP::Post.new(url)       
  req.content_type = 'text'
  req.content_length = blob.to_s.size.to_s
  req['slug'] = id
  req.basic_auth @username, @password
  req.body = blob.to_s
  execute_request(req)     
end

#create_container(container) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/sds-rest.rb', line 47

def create_container(container)
  request_xml = REXML::Document.new()
  request_xml.add_element('s:Container')
  request_xml.root().add_attribute('xmlns:s', 'http://schemas.microsoft.com/sitka/2008/03/')
  request_xml.root().add_element("s:Id")
  request_xml.root().elements["s:Id"].text = container
  req = create_post(request_xml)
  execute_request req     
end

#create_entity(container, entity, id, options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sds-rest.rb', line 65

def create_entity(container, entity, id, options={})
  request_xml = REXML::Document.new()
  request_xml.add_element(entity)
  request_xml.root().add_attribute('xmlns:s', 'http://schemas.microsoft.com/sitka/2008/03/')
  request_xml.root().add_attribute('xmlns:x', 'http://www.w3.org/2001/XMLSchema')
  request_xml.root().add_attribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance' ) 
  request_xml.root().add_element('s:Id')
  request_xml.root().elements['s:Id'].text = id
  
  options.each { |key, value|
    request_xml.root().add_element(key.to_s)
    request_xml.root().elements[key.to_s].add_attribute('xsi:type', infer_type(value))
    request_xml.root().elements[key.to_s].text = value
  }
  req = create_post(request_xml, container)
  execute_request req
end

#delete_blob(container, id) ⇒ Object



148
149
150
# File 'lib/sds-rest.rb', line 148

def delete_blob(container, id)
  delete(get_request_url(container, id))   
end

#delete_container(container) ⇒ Object



57
58
59
# File 'lib/sds-rest.rb', line 57

def delete_container(container)
  delete(get_request_url(container))
end

#delete_entity(container, id, version = nil) ⇒ Object

passing in a version number means that SSDS will only delete the entity if the version number equals the current version number.



85
86
87
88
# File 'lib/sds-rest.rb', line 85

def delete_entity(container, id, version = nil)
  url = get_request_url(container, id)
  delete(url, version)
end

#get_authority(authority) ⇒ Object



42
43
44
45
# File 'lib/sds-rest.rb', line 42

def get_authority(authority)
  @authority = authority
  get(get_request_url)
end

#get_blob(container, id) ⇒ Object



152
153
154
# File 'lib/sds-rest.rb', line 152

def get_blob(container, id)
  get(get_request_url(container, id))
end

#get_container(container) ⇒ Object



61
62
63
# File 'lib/sds-rest.rb', line 61

def get_container(container)
  get(get_request_url(container))
end

#get_entity(container, id, version = nil) ⇒ Object

passing in a version number means that SSDS will only delete the entity if the version number equals the current version number.



92
93
94
95
# File 'lib/sds-rest.rb', line 92

def get_entity(container, id, version = nil)
  url = get_request_url(container, id)
  get(url, version)
end

#infer_type(value) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/sds-rest.rb', line 156

def infer_type(value)
  if(value.is_a?(Integer) || value.is_a?(Float))
    'x:decimal'
  elsif(value.is_a?(true.class) || value.is_a?(false.class))
    'x:boolean'
  elsif(value.is_a?(DateTime))
    'x:dateTime'
  else
    'x:string' 
  end
end

#query(container, query) ⇒ Object

sends a query using the SSDS query syntax

examples:

from c in entities select c - selects all entities
from c in entities where c.Kind = 'Car' select c - select all entities of type 'Car'
from c in entities where c["Make"] = 'Toyota' - select all entities with property Make that equals Toyota


121
122
123
124
# File 'lib/sds-rest.rb', line 121

def query(container, query)
  url = get_request_url(container) + "?q=" + URI.escape(query)
  get(url)
end

#update_blob(container, blob, id) ⇒ Object



137
138
139
140
141
142
143
144
145
146
# File 'lib/sds-rest.rb', line 137

def update_blob(container, blob, id)
  url = get_request_url(container, id)
  req = Net::HTTP::Put.new(url)       
  req.content_type = 'text'
  req.content_length = blob.to_s.size.to_s
  req['slug'] = id
  req.basic_auth @username, @password
  req.body = blob.to_s
  execute_request(req)     
end

#update_entity(container, entity, id, version = nil, options = {}) ⇒ Object

passing in a version number means that SSDS will only delete the entity if the version number equals the current version number.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sds-rest.rb', line 99

def update_entity(container, entity, id, version = nil, options={})
  results = get_entity container, id
  entity = REXML::Document.new(results.body)
  
  options.each { |key, value|
     if(entity.root().elements[key.to_s].nil?)
       entity.root().add_element(key.to_s)
       entity.root().elements[key.to_s].add_attribute('xsi:type', infer_type(value))
     end
     
     entity.root().elements[key.to_s].text = value
   }
  url = get_request_url(container, id)
  put(url, entity, version)
end