PageRenderTime 125ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/Namecheap.pm

http://github.com/duckduckgo/zeroclickinfo-spice
Perl | 66 lines | 39 code | 15 blank | 12 comment | 0 complexity | 553ff5d4a8268ae124ac212403583128 MD5 | raw file
Possible License(s): Apache-2.0
  1. package DDG::Spice::Namecheap;
  2. # ABSTRACT: Queries Namecheap for available domain names.
  3. use DDG::Spice;
  4. use Data::Validate::Domain qw(is_domain);
  5. use List::Util qw(pairmap);
  6. use URI::Escape;
  7. use DDG::Util::SpiceConstants;
  8. spice proxy_cache_valid => '418 1d';
  9. my $namecheap_endpoint = 'http://api.namecheap.com/xml.response';
  10. # environment variables:
  11. # DDG_SPICE_NAMECHEAP_USERNAME : Namecheap API username
  12. # DDG_SPICE_NAMECHEAP_APIKEY : Namecheap API key
  13. # DDG_SPICE_NAMECHEAP_IP_ADDR : Allowed end-User IP address
  14. my @query_list = (
  15. ApiUser => '{{ENV{DDG_SPICE_NAMECHEAP_USERNAME}}}', # Username required to access the API
  16. ApiKey => '{{ENV{DDG_SPICE_NAMECHEAP_APIKEY}}}', # Password required used to access the API
  17. UserName => '{{ENV{DDG_SPICE_NAMECHEAP_USERNAME}}}', # same as ApiUser
  18. Command => 'namecheap.domains.check', # API method
  19. ClientIp => '0.0.0.0', # End-user IP address
  20. DomainList => '$1', # argument to method
  21. );
  22. my $query_url = uri_escape("$namecheap_endpoint?") . join( uri_escape('&'), pairmap {
  23. # the param must always be escaped
  24. my $param = uri_escape("$a=");
  25. # the value must not be escaped if it is part of the environment or argument
  26. my $val = $b;
  27. "$param$val";
  28. } @query_list );
  29. spice to => "https://duckduckgo.com/x.js?u=" . $query_url;
  30. spice wrap_jsonp_callback => 1;
  31. # Triggers
  32. triggers startend => "namecheap";
  33. my $domain_part_regex = qr|
  34. (?:http://)? # HTTP protocol scheme part [optional]
  35. (?<domain> [^/]* ) # domain part
  36. (?:[^\s]*) # any path part (e.g., /path/to/file)
  37. |x;
  38. # Handle statement
  39. handle remainder => sub {
  40. my ($remainder) = @_;
  41. return unless $remainder; # Guard against "no answer"
  42. # get the domain part out
  43. $remainder =~ $domain_part_regex;
  44. my $domain = $+{domain};
  45. # Namecheap API does not allow for subdomains
  46. return unless $domain =~ /^[^.]+\.@{[ DDG::Util::SpiceConstants::TLD_REGEX ]}$/;
  47. # must be a valid domain
  48. return unless is_domain($domain);
  49. return $domain;
  50. };
  51. 1;