idna 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a86fa25e9289266e5d9957a883e65b360843ab50
4
+ data.tar.gz: 1c009cdcadd22ce01e267090607daba2d6f63f10
5
+ SHA512:
6
+ metadata.gz: '07318ee9ac99a91d5b7a115f3277dab82f1b9afcd573cf7bc2c6ec27ef8813339c64d306663b25e119b82f16684285ca0ef61f64b971105efde2479998219b4e'
7
+ data.tar.gz: '0785722a65c129de6dfebd412259ad18d428e92f317a33f93eae7e35177a0da845574d779fefd7de01ae7f3805af58a9083a1bec6289321d5fb9f905fb44a60a'
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .DS_Store
12
+ .rspec_status
13
+ .rubocop_todo.yml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in idna.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 ogom
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,61 @@
1
+ # Idna
2
+
3
+ The [IDN Library](http://www.gnu.org/software/libidn/manual/libidn.html) to use by the [Ruby-FFI](https://github.com/ffi/ffi).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'idna'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```
22
+ $ gem install idna
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Transform
28
+
29
+ * Attach function to `idna_to_ascii_8z`:
30
+
31
+ ```
32
+ Idna.to_ascii('あいうえお') #xn--l8jegik
33
+ ```
34
+
35
+ * Attach function to `idna_to_unicode_8z8z`:
36
+
37
+ ```
38
+ Idna.to_unicode('xn--l8jegik') #あいうえお
39
+ ```
40
+
41
+ ### Configure
42
+
43
+ * Custome libidn name:
44
+
45
+ ```
46
+ Idna.configure do |config|
47
+ config.ffi_lib = 'idn.so.11'
48
+ end
49
+
50
+ Idna.reload!
51
+ ```
52
+
53
+ ## Development
54
+
55
+ ```
56
+ $ bundle exec rspec
57
+ ```
58
+
59
+ ## License
60
+
61
+ * MIT
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "idna"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "idna/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "idna"
8
+ spec.version = Idna::VERSION
9
+ spec.authors = ["ogom"]
10
+ spec.email = ["ogom@outlook.com"]
11
+
12
+ spec.summary = %q{The IDN Library to use by the Ruby-FFI.}
13
+ spec.description = %q{The IDN Library to use by the Ruby-FFI.}
14
+ spec.homepage = "https://github.com/ogom/ruby-idna"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "ffi"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.15"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
@@ -0,0 +1,36 @@
1
+ require 'idna/version'
2
+ require 'idna/default'
3
+ require 'idna/configure'
4
+ require 'idna/error'
5
+ require 'idna/client'
6
+
7
+ # Internationalizing Domain Names in Applications
8
+ module Idna
9
+ class << self
10
+ def configure
11
+ yield Idna::Configure
12
+ end
13
+
14
+ def config
15
+ Idna::Configure
16
+ end
17
+
18
+ def client
19
+ Idna::Client.lib_load! unless @client
20
+ @client ||= Idna::Client.new
21
+ end
22
+
23
+ def reload!
24
+ @client = nil
25
+ Idna::Client.lib_load!
26
+ true
27
+ end
28
+
29
+ def method_missing(method, *args, &block)
30
+ return super unless client.respond_to?(method)
31
+ client.send(method, *args, &block)
32
+ end
33
+ end
34
+ end
35
+
36
+ Idna::Configure.setup
@@ -0,0 +1,41 @@
1
+ require 'ffi'
2
+
3
+ module Idna
4
+ class Client
5
+ extend FFI::Library
6
+ class << self
7
+ def lib_load!
8
+ ffi_lib Idna::Configure.ffi_lib
9
+
10
+ attach_function :idna_to_ascii_8z, %i[string pointer int], :int
11
+ attach_function :idna_to_unicode_8z8z, %i[string pointer int], :int
12
+ end
13
+ end
14
+
15
+ def to_ascii(string, skip_useless: false)
16
+ return unless string
17
+ return string if skip_useless && string.ascii_only?
18
+
19
+ pointer = FFI::MemoryPointer.new :pointer
20
+ int = idna_to_ascii_8z(string, pointer, 0x0001)
21
+ Idna::Error.handling(int) unless int == 0
22
+ result = pointer.read_pointer.read_string
23
+ pointer.free
24
+
25
+ result
26
+ end
27
+
28
+ def to_unicode(string, skip_useless: false)
29
+ return unless string
30
+ return string if skip_useless && !string.ascii_only?
31
+
32
+ pointer = FFI::MemoryPointer.new :pointer
33
+ int = idna_to_unicode_8z8z(string, pointer, 0x0001)
34
+ Idna::Error.handling(int) unless int == 0
35
+ result = pointer.read_pointer.read_string
36
+ pointer.free
37
+
38
+ result.force_encoding('UTF-8')
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,27 @@
1
+ module Idna
2
+ module Configure
3
+ class << self
4
+ def attributes
5
+ @attributes ||= %i[ffi_lib]
6
+ end
7
+
8
+ def setup
9
+ attributes.each do |attribute|
10
+ if Idna::Default.respond_to?(attribute)
11
+ instance_variable_set(:"@#{attribute}", Idna::Default.send(attribute))
12
+ end
13
+ end
14
+ end
15
+
16
+ def options
17
+ Hash[
18
+ Idna::Configure.attributes.map do |attribute|
19
+ [attribute, instance_variable_get(:"@#{attribute}")]
20
+ end
21
+ ]
22
+ end
23
+
24
+ attr_accessor *Idna::Configure.attributes
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ module Idna
2
+ module Default
3
+ FFI_LIB = 'idn'.freeze
4
+
5
+ class << self
6
+ def ffi_lib
7
+ FFI_LIB
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,52 @@
1
+ module Idna
2
+ class Error < StandardError
3
+ class << self
4
+ def handling(int)
5
+ klass = case int
6
+ when 1 then Idna::Error::IDNA_STRINGPREP_ERROR
7
+ when 2 then Idna::Error::IDNA_PUNYCODE_ERROR
8
+ when 3 then Idna::Error::IDNA_CONTAINS_NON_LDH
9
+ when 4 then Idna::Error::IDNA_CONTAINS_MINUS
10
+ when 5 then Idna::Error::IDNA_INVALID_LENGTH
11
+ when 6 then Idna::Error::IDNA_NO_ACE_PREFIX
12
+ when 7 then Idna::Error::IDNA_ROUNDTRIP_VERIFY_ERROR
13
+ when 8 then Idna::Error::IDNA_CONTAINS_ACE_PREFIX
14
+ when 9 then Idna::Error::IDNA_ICONV_ERROR
15
+ when 201 then Idna::Error::IDNA_MALLOC_ERROR
16
+ when 202 then Idna::Error::IDNA_DLOPEN_ERROR
17
+ end
18
+ raise klass.new message(int)
19
+ end
20
+
21
+ private
22
+
23
+ def message(int)
24
+ case int
25
+ when 1 then 'Error during string preparation.'
26
+ when 2 then 'Error during punycode operation.'
27
+ when 3 then 'For IDNA_USE_STD3_ASCII_RULES, indicate that the string contains non-LDH ASCII characters.'
28
+ when 4 then 'For IDNA_USE_STD3_ASCII_RULES, indicate that the string contains a leading or trailing hyphen-minus (U+002D).'
29
+ when 5 then 'The final output string is not within the (inclusive) range 1 to 63 characters.'
30
+ when 6 then 'The string does not contain the ACE prefix (for ToUnicode).'
31
+ when 7 then 'The ToASCII operation on output string does not equal the input.'
32
+ when 8 then 'The input contains the ACE prefix (for ToASCII).'
33
+ when 9 then 'Could not convert string in locale encoding.'
34
+ when 201 then 'Could not allocate buffer (this is typically a fatal error).'
35
+ when 202 then 'Could not dlopen the libcidn DSO (only used internally in libc).'
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ class Error::IDNA_STRINGPREP_ERROR < Idna::Error; end
42
+ class Error::IDNA_PUNYCODE_ERROR < Idna::Error; end
43
+ class Error::IDNA_CONTAINS_NON_LDH < Idna::Error; end
44
+ class Error::IDNA_CONTAINS_MINUS < Idna::Error; end
45
+ class Error::IDNA_INVALID_LENGTH < Idna::Error; end
46
+ class Error::IDNA_NO_ACE_PREFIX < Idna::Error; end
47
+ class Error::IDNA_ROUNDTRIP_VERIFY_ERROR < Idna::Error; end
48
+ class Error::IDNA_CONTAINS_ACE_PREFIX < Idna::Error; end
49
+ class Error::IDNA_ICONV_ERROR < Idna::Error; end
50
+ class Error::IDNA_MALLOC_ERROR < Idna::Error; end
51
+ class Error::IDNA_DLOPEN_ERROR < Idna::Error; end
52
+ end
@@ -0,0 +1,3 @@
1
+ module Idna
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: idna
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ogom
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: The IDN Library to use by the Ruby-FFI.
70
+ email:
71
+ - ogom@outlook.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - idna.gemspec
86
+ - lib/idna.rb
87
+ - lib/idna/client.rb
88
+ - lib/idna/configure.rb
89
+ - lib/idna/default.rb
90
+ - lib/idna/error.rb
91
+ - lib/idna/version.rb
92
+ homepage: https://github.com/ogom/ruby-idna
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.13
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: The IDN Library to use by the Ruby-FFI.
116
+ test_files: []