Best Site_prism code snippet using SitePrism.component_names
addressable_url_matcher.rb
Source:addressable_url_matcher.rb
...16 # or nil if the URL doesn't conform to the matcher template.17 def mappings(url)18 uri = Addressable::URI.parse(url)19 result = {}20 component_names.each do |component|21 component_result = component_matches(component, uri)22 return nil unless component_result23 result.merge!(component_result)24 end25 result26 end27 # Determine whether URL matches our pattern, and28 # optionally whether the extracted mappings match29 # a hash of expected values. You can specify values30 # as strings, numbers or regular expressions.31 def matches?(url, expected_mappings = {})32 actual_mappings = mappings(url)33 return false unless actual_mappings34 expected_mappings.empty? ||35 all_expected_mappings_match?(expected_mappings, actual_mappings)36 end37 private38 def all_expected_mappings_match?(expected_mappings, actual_mappings)39 expected_mappings.all? do |key, expected_value|40 actual_value = actual_mappings[key.to_s]41 case expected_value42 when Numeric; then actual_value == expected_value.to_s43 when Regexp; then actual_value.match(expected_value)44 else expected_value == actual_value45 end46 end47 end48 def component_templates49 @component_templates ||= extract_component_templates50 end51 def extract_component_templates52 component_names.each_with_object({}) do |component, templates|53 component_url = to_substituted_uri.public_send(component).to_s54 next unless component_url && !component_url.empty?55 reverse_substitutions.each_pair do |substituted_value, template_value|56 component_url = component_url.sub(substituted_value, template_value)57 end58 templates[component] = Addressable::Template.new(component_url)59 end60 end61 # Returns empty hash if the template omits the component or a set of62 # substitutions if the provided URI component matches the template63 # component or nil if the match fails.64 def component_matches(component, uri)65 component_template = component_templates[component]66 return {} unless component_template67 component_url = uri.public_send(component).to_s68 mappings = component_template.extract(component_url)69 return mappings if mappings70 # to support Addressable's expansion of queries71 # ensure it's parsing the fragment as appropriate (e.g. {?params*})72 prefix = component_prefixes[component]73 return nil unless prefix74 component_template.extract(prefix + component_url)75 end76 # Convert the pattern into an Addressable URI by substituting77 # the template slugs with nonsense strings.78 def to_substituted_uri79 url = pattern80 substitutions.each_pair { |slug, value| url = url.sub(slug, value) }81 begin82 Addressable::URI.parse(url)83 rescue Addressable::URI::InvalidURIError84 SitePrism.logger.fatal("Ensure you don't use templated port numbers.")85 raise SitePrism::InvalidUrlMatcherError86 end87 end88 def substitutions89 @substitutions ||= slugs.each_with_index.reduce({}) do |memo, slug_index|90 slug, index = slug_index91 memo.merge(slug => slug_prefix(slug) + substitution_value(index))92 end93 end94 def reverse_substitutions95 @reverse_substitutions ||=96 slugs.each_with_index.reduce({}) do |memo, slug_index|97 slug, index = slug_index98 memo.merge(99 slug_prefix(slug) + substitution_value(index) => slug,100 substitution_value(index) => slug101 )102 end103 end104 def slugs105 pattern.scan(/{[^}]+}/)106 end107 # If a slug begins with non-alpha characters, it may denote the start of108 # a new component (e.g. query or fragment). We emit this prefix as part of109 # the substituted slug so that Addressable's URI parser can see it as such.110 def slug_prefix(slug)111 prefix = slug.match(/\A{([^A-Za-z]+)/)112 prefix && prefix[1] || ''113 end114 # Generate a repeatable 5 character uniform alphabetical nonsense string115 # to allow parsing as a URI116 def substitution_value(index)117 sha = Digest::SHA1.digest(index.to_s)118 Base64.urlsafe_encode64(sha).gsub(/[^A-Za-z]/, '')[0..5]119 end120 def component_names121 %i[scheme user password host port path query fragment]122 end123 def component_prefixes124 { query: '?', fragment: '#' }125 end126 end127end...
component_names
Using AI Code Generation
1 self.class.instance_variable_get(:@component_names)2 def search_for(query)3 self.class.instance_variable_get(:@component_names)4 def search_for(query)5 self.class.instance_variable_get(:@component_names
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!