Class: ActiveHarmony::Service
- Inherits:
-
Object
- Object
- ActiveHarmony::Service
- Defined in:
- lib/active_harmony/service.rb
Instance Attribute Summary collapse
-
#auth ⇒ Object
Returns the value of attribute auth.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#header ⇒ Object
Returns the value of attribute header.
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
-
#add_custom_url(object_type, action, path, method = nil) ⇒ Object
Adds custom path.
-
#add_object_name(object_type, action, new_object_name) ⇒ Object
Adds new name for some type of object.
-
#clear_contexts ⇒ Object
Clears contexts for service.
-
#create(object_type, data) ⇒ Object
Creates a new remote object.
-
#custom_url_for(object_type, action) ⇒ Service::ServiceUrl
Returns custom path.
- #find_object_in_result(result, object_type, action) ⇒ Object
-
#generate_rest_url(action, object_type, object_id = nil) ⇒ Service::ServiceUrl
Generates url for REST service.
-
#generate_url(path) ⇒ String
Generates URL for path using base_url.
-
#initialize ⇒ Service
constructor
Initializes new Rest Service.
-
#list(object_type) ⇒ Array<Hash>
List collection of remote objects.
-
#object_name_for(object_type, action) ⇒ Object
Returns custom object name for action.
-
#parse_xml(xml_string) ⇒ Array<Hash>
Parses XML.
-
#retrieve(url, method = :get, headers = {}, data = nil) ⇒ String
Retrieves data from URL.
- #retrieve_with_http(url, method, headers, data) ⇒ Object
- #retrieve_with_typhoeus(url, method, headers, data) ⇒ Object
-
#set_contexts(contexts) ⇒ Object
Set contexts for service.
-
#set_header(name, value) ⇒ Object
Adds header.
-
#show(object_type, id) ⇒ Hash
Shows remote object.
-
#update(object_type, id, data) ⇒ Object
Updates remote object.
Constructor Details
#initialize ⇒ Service
Initializes new Rest Service
10 11 12 13 14 15 16 |
# File 'lib/active_harmony/service.rb', line 10 def initialize @header = {} @contexts = {} @paths = [] @object_names = [] @root = nil end |
Instance Attribute Details
#auth ⇒ Object
Returns the value of attribute auth.
3 4 5 |
# File 'lib/active_harmony/service.rb', line 3 def auth @auth end |
#base_url ⇒ Object
Returns the value of attribute base_url.
3 4 5 |
# File 'lib/active_harmony/service.rb', line 3 def base_url @base_url end |
#header ⇒ Object
Returns the value of attribute header.
3 4 5 |
# File 'lib/active_harmony/service.rb', line 3 def header @header end |
#root ⇒ Object
Returns the value of attribute root.
3 4 5 |
# File 'lib/active_harmony/service.rb', line 3 def root @root end |
Instance Method Details
#add_custom_url(object_type, action, path, method = nil) ⇒ Object
Adds custom path
237 238 239 240 241 242 243 244 |
# File 'lib/active_harmony/service.rb', line 237 def add_custom_url(object_type, action, path, method = nil) @paths << { :object_type => object_type, :action => action, :path => path, :method => method } end |
#add_object_name(object_type, action, new_object_name) ⇒ Object
Adds new name for some type of object
269 270 271 272 273 274 275 |
# File 'lib/active_harmony/service.rb', line 269 def add_object_name(object_type, action, new_object_name) @object_names << { :object_type => object_type, :action => action, :new_object_name => new_object_name.to_s } end |
#clear_contexts ⇒ Object
Clears contexts for service
225 226 227 |
# File 'lib/active_harmony/service.rb', line 225 def clear_contexts @contexts = {} end |
#create(object_type, data) ⇒ Object
Creates a new remote object
202 203 204 205 206 207 208 209 |
# File 'lib/active_harmony/service.rb', line 202 def create(object_type, data) url = generate_rest_url(:create, object_type) object_name = object_name_for(object_type, :create) xml_data = data.to_xml(:root => object_name, :skip_instruct => true, :dasherize => false) result = retrieve(url.path, url.method, {'Content-type' => 'application/xml'}, xml_data) parsed_result = parse_xml(result) find_object_in_result(parsed_result, object_type, :create) end |
#custom_url_for(object_type, action) ⇒ Service::ServiceUrl
Returns custom path
251 252 253 254 255 256 257 258 259 |
# File 'lib/active_harmony/service.rb', line 251 def custom_url_for(object_type, action) path = @paths.find do |path| path[:object_type] == object_type && path[:action] == action end if path ServiceUrl.new(generate_url(path[:path]), path[:method]) end end |
#find_object_in_result(result, object_type, action) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/active_harmony/service.rb', line 140 def find_object_in_result(result, object_type, action) data = result # puts result if @root @root.split('/').each do |branch| break if data.nil? data = data[branch] end data else path = if action == :list object_type.to_s.pluralize else object_type.to_s end data[path] # Pluralize? When? action? end end |
#generate_rest_url(action, object_type, object_id = nil) ⇒ Service::ServiceUrl
Generates url for REST service
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/active_harmony/service.rb', line 35 def generate_rest_url(action, object_type, object_id = nil) url = custom_url_for(object_type, action) method = if action == :list || action == :show :get elsif action == :create :post elsif action == :update :put end if url url.request_method ||= method if object_id url.path.sub!(':id', object_id.to_s) end return url if url end path = '' path += @contexts.collect { |name, value| "#{name}/#{value}" }.join('/') path += '/' if action == :list || action == :create path += object_type.to_s.pluralize elsif action == :show || action == :update path += "#{object_type.to_s.pluralize}/#{object_id.to_s}" end url = generate_url(path) ServiceUrl.new(url, method) end |
#generate_url(path) ⇒ String
Generates URL for path using base_url
73 74 75 76 77 78 |
# File 'lib/active_harmony/service.rb', line 73 def generate_url(path) if path =~ /^\// path.sub!('/', '') end path = "#{@base_url}/#{path}" end |
#list(object_type) ⇒ Array<Hash>
List collection of remote objects
166 167 168 169 170 171 |
# File 'lib/active_harmony/service.rb', line 166 def list(object_type) url = generate_rest_url(:list, object_type) result = retrieve(url.path) parsed_result = parse_xml(result) find_object_in_result(parsed_result, object_type, :list) end |
#object_name_for(object_type, action) ⇒ Object
Returns custom object name for action
281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/active_harmony/service.rb', line 281 def object_name_for(object_type, action) object_name = @object_names.find do |object_name| object_name[:object_type] == object_type object_name[:action] == action end object_name = object_name ? object_name[:new_object_name] : nil unless object_name object_name = object_type.to_s.gsub('-', '_') end object_name end |
#parse_xml(xml_string) ⇒ Array<Hash>
Parses XML
131 132 133 134 135 136 137 138 |
# File 'lib/active_harmony/service.rb', line 131 def parse_xml(xml_string) return {} if xml_string.blank? begin Hash.from_xml(xml_string) rescue Exception => e return {} end end |
#retrieve(url, method = :get, headers = {}, data = nil) ⇒ String
Retrieves data from URL
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/active_harmony/service.rb', line 87 def retrieve(url, method = :get, headers = {}, data = nil) puts "[ActiveHarmony] Retrieving data:" puts "[ActiveHarmony] URL: #{url}" puts "[ActiveHarmony] Method: #{method}" puts "[ActiveHarmony] Headers: #{headers.inspect}" puts "[ActiveHarmony] Data: #{data.inspect}" if defined?(Rails) && !Rails.env.test? data = retrieve_with_typhoeus(url, method, headers, data) else data = retrieve_with_http(url, method, headers, data) end data end |
#retrieve_with_http(url, method, headers, data) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/active_harmony/service.rb', line 110 def retrieve_with_http(url, method, headers, data) url = URI.parse(url) response = nil headers = @header.merge(headers) Net::HTTP.start(url.host, url.port) do |http| if method == :get response = http.get(url.path, headers).body elsif [:put, :post].include?(method) response = http.send(method, url.path, data, headers).body end end response end |
#retrieve_with_typhoeus(url, method, headers, data) ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/active_harmony/service.rb', line 101 def retrieve_with_typhoeus(url, method, headers, data) = {:headers => @header.merge(headers), :body => data} .merge!(auth) if auth # puts request_options response = Typhoeus::Request.send(method, url, ) response.body end |
#set_contexts(contexts) ⇒ Object
Set contexts for service
217 218 219 220 221 |
# File 'lib/active_harmony/service.rb', line 217 def set_contexts(contexts) contexts.each do |name, value| @contexts[name.to_sym] = value end end |
#set_header(name, value) ⇒ Object
Adds header
22 23 24 |
# File 'lib/active_harmony/service.rb', line 22 def set_header(name, value) @header[name] = value end |
#show(object_type, id) ⇒ Hash
Shows remote object
178 179 180 181 182 183 |
# File 'lib/active_harmony/service.rb', line 178 def show(object_type, id) url = generate_rest_url(:show, object_type, id) result = retrieve(url.path) parsed_result = parse_xml(result) find_object_in_result(parsed_result, object_type, :show) end |
#update(object_type, id, data) ⇒ Object
Updates remote object
190 191 192 193 194 195 196 |
# File 'lib/active_harmony/service.rb', line 190 def update(object_type, id, data) url = generate_rest_url(:update, object_type, id) object_name = object_name_for(object_type, :update) xml_data = data.to_xml(:root => object_name, :skip_instruct => true, :dasherize => false) result = retrieve(url.path, url.method, {'Content-type' => 'application/xml'}, xml_data) find_object_in_result(result, object_type, :update) end |