Class: Gom::Client
- Inherits:
-
Object
- Object
- Gom::Client
- Defined in:
- lib/gom/client.rb,
lib/gom/client/version.rb
Constant Summary collapse
- VERSION =
'0.0.5'
- DATE =
'2013-08-15'
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #create!(path_or_uri, attributes = {}) ⇒ Object
- #destroy(uri) ⇒ Object
-
#destroy!(path_or_uri) ⇒ Object
GOM API Change: Destroy will NOT return error on non-existing attributes or nodes.
-
#initialize(gom_root = 'http://gom') ⇒ Client
constructor
A new instance of Client.
-
#register_observer(options = {}) ⇒ Object
supports anonymous and named observers.
- #retrieve(path_or_uri, redirect_limit = 10) ⇒ Object
- #retrieve!(path_or_uri, redirect_limit = 10) ⇒ Object
-
#retrieve_val(path_or_uri) ⇒ Object
returns nil but NOT raise error on non-existing attributes.
-
#run_script(options = {}) ⇒ Object
supports stored and posted scripts.
- #update!(path_or_uri, hash_or_text = nil) ⇒ Object (also: #update)
Constructor Details
#initialize(gom_root = 'http://gom') ⇒ Client
Returns a new instance of Client.
26 27 28 |
# File 'lib/gom/client.rb', line 26 def initialize(gom_root = 'http://gom') @root = URI.parse(gom_root) end |
Instance Attribute Details
#root ⇒ Object (readonly)
Returns the value of attribute root.
24 25 26 |
# File 'lib/gom/client.rb', line 24 def root @root end |
Instance Method Details
#create!(path_or_uri, attributes = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/gom/client.rb', line 82 def create!(path_or_uri, attributes={}) uri = path_or_uri.kind_of?(URI) ? path_or_uri : URI.parse("#{@root}#{path_or_uri}") request_body = attributes_to_xml attributes headers = { 'Content-Type' => 'application/xml' } response = Net::HTTP.new(uri.host, uri.port).request_post(uri.path, request_body, headers) if response.kind_of?(Net::HTTPRedirection) return URI.parse(response['location']).path end raise HttpError.new(response, "while CREATEing (#{uri.path})") end |
#destroy(uri) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/gom/client.rb', line 75 def destroy(uri) self.destroy! uri rescue Gom::HttpError => e return nil if e.response.code == "404" raise e end |
#destroy!(path_or_uri) ⇒ Object
GOM API Change: Destroy will NOT return error on non-existing attributes or nodes. So no error will every be raised from this methods
67 68 69 70 71 72 73 |
# File 'lib/gom/client.rb', line 67 def destroy!(path_or_uri) uri = path_or_uri.kind_of?(URI) ? path_or_uri : URI.parse("#{@root}#{path_or_uri}") response = Net::HTTP.new(uri.host, uri.port).delete(uri.path) return true if response.kind_of?(Net::HTTPSuccess) raise HttpError.new(response, "while DELETEing #{uri.path}") end |
#register_observer(options = {}) ⇒ Object
supports anonymous and named observers
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/gom/client.rb', line 173 def register_observer(={}) name = [:name] || nil callback_url = [:callback_url] || nil node = [:node] || nil filters = [:filters] || {} format = [:format] || "application/json" callback_url.nil? and (raise ArgumentError, "callback_url must not be nil") node.nil? and (raise ArgumentError, "node must not be nil") unless ['application/json', 'application/xml'].include?(format) raise ArgumentError, "invalid format: '#{format}'" end url = URI.parse("#{@root}/gom/observer#{node}") form_data = { 'callback_url' => callback_url, 'accept' => format } form_data.merge!(filters) if name observer_url = url.path.gsub(/\:/,'/') my_uri = "#{observer_url}/.#{name}" destroy my_uri form_data['observed_uri'] = "#{node}" update(my_uri, form_data) else request = Net::HTTP::Post.new(url.path) request.set_form_data(form_data) response = Net::HTTP.new(url.host, url.port).start do |http| http.request(request) end my_uri = URI.parse(response['location']).path end my_uri end |
#retrieve(path_or_uri, redirect_limit = 10) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/gom/client.rb', line 35 def retrieve(path_or_uri, redirect_limit=10) self.retrieve! path_or_uri, redirect_limit rescue Gom::HttpError => e return nil if e.response.code == "404" raise e end |
#retrieve!(path_or_uri, redirect_limit = 10) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/gom/client.rb', line 42 def retrieve!(path_or_uri, redirect_limit=10) uri = path_or_uri.kind_of?(URI) ? path_or_uri : URI.parse("#{@root}#{path_or_uri}") response = Net::HTTP.new(uri.host, uri.port).request_get(uri.path, {"Accept" => "application/json" }) begin if response.kind_of?(Net::HTTPSuccess) return JSON.parse(response.body, symbolize_names: true) end rescue StandardError => e raise HttpError.new(response, "#{e} -- could not parse body: '#{response.body}'") end if (redirect_limit == 0 && response.kind_of?(Net::HTTPRedirection)) raise "too many redirects" end if response.kind_of?(Net::HTTPRedirection) return Gom::retrieve(response['location'], redirect_limit - 1) end raise HttpError.new(response, "while GETting #{uri.path}") end |
#retrieve_val(path_or_uri) ⇒ Object
returns nil but NOT raise error on non-existing attributes
31 32 33 |
# File 'lib/gom/client.rb', line 31 def retrieve_val(path_or_uri) (a = retrieve(path_or_uri)) && a[:attribute][:value] end |
#run_script(options = {}) ⇒ Object
supports stored and posted scripts
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 162 163 164 165 166 167 168 169 170 |
# File 'lib/gom/client.rb', line 137 def run_script( = {}) path = [:path] || nil script = [:script] || nil params = [:params] || {} if path.nil? script.nil? and (raise ArgumentError, "must provide script OR path") else script and (raise ArgumentError, "must not provide script AND path") params[:_script_path] = path end params = params.keys.zip(params.values).map {|k,v| "#{k}=#{v}"} url = URI.parse("#{@root}/gom/script/v8?#{params.join('&')}") response = Net::HTTP.start(url.host, url.port) do |http| if script request = Net::HTTP::Post.new(url.to_s) request.set_content_type "text/javascript" http.request(request, script) else request = Net::HTTP::Get.new(url.to_s) http.request(request) end end if response.kind_of?(Net::HTTPSuccess) return response else raise HttpError.new( response, "while executing server-side-script:\n#{response.body}" ) end end |
#update!(path_or_uri, hash_or_text = nil) ⇒ Object Also known as: update
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 |
# File 'lib/gom/client.rb', line 95 def update!(path_or_uri, hash_or_text=nil) uri = path_or_uri.kind_of?(URI) ? path_or_uri : URI.parse("#{@root}#{path_or_uri}") if is_attribute?(uri.path) raise "update attribute call must include value" if hash_or_text.nil? raise "update attribute value must be a string" unless hash_or_text.kind_of?(String) response_format = "text/plain" if hash_or_text != "" doc = REXML::Document.new attr_node = doc.add_element 'attribute' attr_node.attributes['type'] = 'string'; attr_node.text = hash_or_text request_body = doc.to_s headers = { 'Content-Type' => 'application/xml', 'Accept' => response_format } else request_body = "attribute=#{hash_or_text}&type=string" headers = { 'Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => response_format } end else raise "update node values must be a hash of attributes" unless hash_or_text.nil? or hash_or_text.kind_of?(Hash) request_body = attributes_to_xml hash_or_text || {} response_format = "application/json" headers = { 'Content-Type' => 'application/xml', 'Accept' => response_format } end #headers = { 'Content-Type' => 'application/xml', # 'Accept' => response_format } response = Net::HTTP.new(uri.host, uri.port).request_put(uri.path, request_body, headers) return response.body if response.kind_of?(Net::HTTPSuccess) && response_format == "text/plain" if response.kind_of?(Net::HTTPSuccess) && response_format == "application/json" return JSON.parse(response.body, symbolize_names: true) end raise HttpError.new(response, "while PUTting #{uri.path}") end |