class Opera::MobileStoreSDK::FaradayMiddleware::ResponseParser
Parses the responses to models:
Public Instance Methods
call(request_env)
click to toggle source
# File lib/opera/mobile_store_sdk/faraday_middleware/response_parser.rb, line 10 def call(request_env) # do something with the request # request_env[:request_headers].merge!(...) @app.call(request_env).on_complete do |response_env| benchmark = Benchmark.measure "api_response_parsing" do # Parse to XML: xml_data = Nokogiri::XML(response_env[:body]) response_env[:found_rows] = xml_data.xpath("string(/xml/*/@found_rows)").to_i response_env[:timestamp] = xml_data.xpath("string(/xml/*/@timestamp)").to_i real_root_name = xml_data.xpath("name(/xml/*)") parser_class = case real_root_name when "platforms" Opera::MobileStore::DevicePlatform when "platform_families" Opera::MobileStore::DevicePlatformFamily else "Opera::MobileStore::#{real_root_name.classify}".safe_constantize end # Parse each node: collection_path = "/xml/#{real_root_name}" collection_path += case real_root_name when "compatibility" # Add the product id to the compatibility child nodes if # a product compatibility was queried instead of a build compat: product_param = request_env.url.query[/product_id=(\d+)/i] if product_param.present? product_id = product_param.split('=').last.to_i xml_data.xpath("#{collection_path}/product").each do |product_compat_node| product_compat_node.set_attribute('id', product_id) end end # Return an empty string... "" else "/#{real_root_name.singularize}" end parsed_data = xml_data.xpath(collection_path).map do |item_node| parser_class.build_from_nokogiri_node item_node end # replace body with parsed_data: response_env[:body] = parsed_data end response_env[:opera_api_response_parsing_tms] = benchmark end end