Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add List#[] method for checking whether a rule exists in the list #67

Closed
postmodern opened this issue Feb 17, 2015 · 3 comments
Closed
Assignees

Comments

@postmodern
Copy link
Contributor

I would like to query the default list to determine if a domain wildcard rule is actually a public suffix rule.

list['*.wired.com']
# => nil

list['*.com']
# => #<PublicSuffix::Rule::Normal:0x007f9870738bc8 @name="com", @value="com", @type=:normal, @labels=["com"]>
@weppos weppos added the feature label Feb 6, 2016
@weppos
Copy link
Owner

weppos commented Feb 6, 2016

This is tricky, as the list is not stored in a simple Hash (to increase the lookup performance). However, the request makes sense.

I'll see what I can do.

@gam3
Copy link
Contributor

gam3 commented Aug 16, 2016

It is not quite clear to me what the API you are looking for, but this is close. Maybe you could tell me more.

#!/usr/bin/env ruby

require 'public_suffix'

module PublicSuffix
  class List
    def [](x)
      myhash = Hash.new
      x.split(DOT).reverse[0..0].each do |p|
        if @indexes.include?(p)
          @indexes[p].each do |r|
            if @rules[r].wildcard?
              myhash['*.' + @rules[r].value] = @rules[r]
            else
              myhash['' + @rules[r].value] = @rules[r]
            end
          end
        end
      end
      myhash[x] || nil
    end
  end
end


list = PublicSuffix::List.default

%w(as com wired.com *.api.githubcloud.com compute.amazonaws.com).each do |name|
  puts "#{name} #{list[name].inspect}"
end

@weppos weppos self-assigned this Jun 25, 2019
@weppos
Copy link
Owner

weppos commented Jun 25, 2019

Closing, as you can currently take advantage of the Rule#each returning an Enumerator.

PublicSuffix::List.default.each.find { |r| r.rule == "*.wired.com" }
# => nil
PublicSuffix::List.default.each.find { |r| r.rule == "com" }
# => #<PublicSuffix::Rule::Normal:0x00007fe049034040 @value="com", @length=1, @private=false>

@weppos weppos closed this as completed Jun 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants