class Setsuzoku::Service::WebService::ApiStrategy
Public Instance Methods
api_headers()
click to toggle source
Specific API headers an API strategy must have when making requests.
@return [Hash] the additional required headers for a request.
# File lib/setsuzoku/service/web_service/api_strategy.rb, line 52 def api_headers {} end
parse_response(response:, **options)
click to toggle source
# File lib/setsuzoku/service/web_service/api_strategy.rb, line 81 def parse_response(response:, **options) case options[:response_type] when :json resp = JSON.parse(response.body) resp.is_a?(Hash) ? resp.deep_symbolize_keys : resp when :xml convert_xml_to_hash(response.body) when :html response.body when :txt, :png, :jpg, :gif # for now just return the file response.body # for now just put in tmp # tmp_dir = "#{Dir.tmpdir}/setsuzoku/#{self.plugin.class.system_name.underscore}/#{self.plugin.name.underscore}" # file_name = "#{self.plugin.class.system_name.underscore}_#{self.plugin.name.underscore}_#{Time.now.strftime("%Y_%m_%d_%T")}" # t = Tempfile.new([file_name, ".#{options[:response_type]}"], encoding: 'ascii-8bit') # t.write(response.body) # t.rewind # t else resp = JSON.parse(response.body) resp.is_a?(Hash) ? resp.deep_symbolize_keys : resp end end
perform_external_call(request:, action_details:, **options)
click to toggle source
# File lib/setsuzoku/service/web_service/api_strategy.rb, line 71 def perform_external_call(request:, action_details:, **options); end
Private Instance Methods
convert_hash_to_xml(hash, mutate_keys = true)
click to toggle source
# File lib/setsuzoku/service/web_service/api_strategy.rb, line 109 def convert_hash_to_xml(hash, mutate_keys = true) hash = hash.map do |k, v| text = if v.is_a?(Hash) convert_hash_to_xml(v) elsif v.is_a?(Array) v.map do |elem| convert_hash_to_xml(elem) end.join else v end k = k.to_s.camelize if mutate_keys "<%s>%s</%s>" % [k, text, k] end.join hash end
convert_xml_to_hash(xml)
click to toggle source
# File lib/setsuzoku/service/web_service/api_strategy.rb, line 128 def convert_xml_to_hash(xml) begin result = Nokogiri::XML(xml) key = result.root.name.underscore.to_sym val = xml_node_to_hash(result.root) { key => val } rescue ::Exception => e {} end end
xml_node_to_hash(node)
click to toggle source
# File lib/setsuzoku/service/web_service/api_strategy.rb, line 139 def xml_node_to_hash(node) # If we are at the root of the document, start the hash if node.element? result_hash = {} if node.attributes != {} attributes = {} node.attributes.keys.each do |key| attributes[node.attributes[key].name.underscore.to_sym] = node.attributes[key].value end end if node.children.size > 0 node.children.each do |child| result = xml_node_to_hash(child) if child.name == "text" unless child.next_sibling || child.previous_sibling return result unless attributes result_hash[child.name.underscore.to_sym] = result end elsif result_hash[child.name.to_sym] if result_hash[child.name.to_sym].is_a?(Array) result_hash[child.name.underscore.to_sym] << result else result_hash[child.name.underscore.to_sym] = [result_hash[child.name.to_sym]] << result end else stripped_children = node.children.reject{ |n| n.text.strip.blank? } if stripped_children.length > 1 && stripped_children.combination(2).all? { |a,b| a.name == b.name } return result_hash[node.name.underscore.to_sym] = stripped_children.map{|n| xml_node_to_hash(n) } else result_hash[child.name.underscore.to_sym] = result end end end if attributes #add code to remove non-data attributes e.g. xml schema, namespace here #if there is a collision then node content supersets attributes result_hash = attributes.merge(result_hash) end return result_hash else return attributes end else return node.content.to_s end end