emojidex 0.0.23 → 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.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.coveralls.yml +1 -0
  3. data/.rubocop.yml +3 -0
  4. data/.travis.yml +7 -6
  5. data/Gemfile +0 -2
  6. data/Guardfile +17 -8
  7. data/README.md +54 -17
  8. data/emojidex.gemspec +4 -1
  9. data/lib/emojidex.rb +12 -10
  10. data/lib/emojidex/{categories.rb → data/categories.rb} +3 -4
  11. data/lib/emojidex/{category.rb → data/category.rb} +1 -1
  12. data/lib/emojidex/data/collection.rb +158 -0
  13. data/lib/emojidex/data/collection/asset_information.rb +52 -0
  14. data/lib/emojidex/data/collection/cache.rb +137 -0
  15. data/lib/emojidex/{collection → data/collection}/moji_data.rb +1 -1
  16. data/lib/emojidex/data/collection/static_collection.rb +35 -0
  17. data/lib/emojidex/data/collection_checker.rb +94 -0
  18. data/lib/emojidex/{emoji.rb → data/emoji.rb} +3 -2
  19. data/lib/emojidex/data/emoji/asset_information.rb +45 -0
  20. data/lib/emojidex/data/extended.rb +18 -0
  21. data/lib/emojidex/data/utf.rb +18 -0
  22. data/lib/emojidex/defaults.rb +21 -0
  23. data/lib/emojidex/env_helper.rb +11 -0
  24. data/lib/emojidex/service/collection.rb +67 -0
  25. data/lib/emojidex/service/error.rb +9 -0
  26. data/lib/emojidex/service/indexes.rb +43 -0
  27. data/lib/emojidex/service/search.rb +82 -0
  28. data/lib/emojidex/service/transactor.rb +100 -0
  29. data/lib/emojidex/service/user.rb +233 -0
  30. data/spec/{categories_spec.rb → emojidex/data/categories_spec.rb} +4 -3
  31. data/spec/{collection_checker_spec.rb → emojidex/data/collection_checker_spec.rb} +12 -15
  32. data/spec/{collection_spec.rb → emojidex/data/collection_spec.rb} +40 -23
  33. data/spec/{emoji_spec.rb → emojidex/data/emoji_spec.rb} +2 -2
  34. data/spec/{extended_spec.rb → emojidex/data/extended_spec.rb} +21 -10
  35. data/spec/{utf_spec.rb → emojidex/data/utf_spec.rb} +22 -17
  36. data/spec/emojidex/service/collection_spec.rb +20 -0
  37. data/spec/emojidex/service/error_spec.rb +17 -0
  38. data/spec/emojidex/service/indexes_spec.rb +62 -0
  39. data/spec/emojidex/service/search_spec.rb +87 -0
  40. data/spec/emojidex/service/transactor_spec.rb +11 -0
  41. data/spec/emojidex/service/user_spec.rb +128 -0
  42. data/spec/spec_helper.rb +9 -62
  43. metadata +36 -31
  44. data/lib/emojidex/api/categories.rb +0 -16
  45. data/lib/emojidex/api/emoji.rb +0 -26
  46. data/lib/emojidex/api/search/emoji.rb +0 -16
  47. data/lib/emojidex/client.rb +0 -60
  48. data/lib/emojidex/collection.rb +0 -156
  49. data/lib/emojidex/collection/asset_information.rb +0 -49
  50. data/lib/emojidex/collection/cache.rb +0 -78
  51. data/lib/emojidex/collection_checker.rb +0 -93
  52. data/lib/emojidex/emoji/asset_information.rb +0 -20
  53. data/lib/emojidex/error.rb +0 -15
  54. data/lib/emojidex/extended.rb +0 -19
  55. data/lib/emojidex/service.rb +0 -32
  56. data/lib/emojidex/utf.rb +0 -19
  57. data/spec/api/categories_spec.rb +0 -49
  58. data/spec/api/emoji_spec.rb +0 -89
  59. data/spec/api/search/emoji_spec.rb +0 -30
  60. data/spec/client_spec.rb +0 -24
@@ -1,16 +0,0 @@
1
- module Emojidex
2
- module API
3
- # Get cateegories from API
4
- module Categories
5
- def categories(*args)
6
- response = get('/api/v1/categories.json', args)
7
- response[:body]['categories']
8
- end
9
-
10
- def category(*args)
11
- response = get('/api/v1/categories/category.json', args)
12
- response[:body]
13
- end
14
- end
15
- end
16
- end
@@ -1,26 +0,0 @@
1
- module Emojidex
2
- module API
3
- # Get emoji from API
4
- module Emoji
5
- def emoji(*args)
6
- response = get('/api/v1/emoji.json', args)
7
- response[:body]['emoji']
8
- end
9
-
10
- def single_emoji(*args)
11
- response = get('/api/v1/emoji/emoji.json', args)
12
- response[:body]
13
- end
14
-
15
- def emoji_detailed(*args)
16
- response = get('/api/v1/emoji/detailed.json', args)
17
- response[:body]
18
- end
19
-
20
- def single_emoji_detailed(*args)
21
- response = get('/api/v1/emoji/1/detailed.json', args)
22
- response[:body]
23
- end
24
- end
25
- end
26
- end
@@ -1,16 +0,0 @@
1
- module Emojidex
2
- module API
3
- module Search
4
- # Search API for emoji
5
- module Emoji
6
- def emoji_code_cont(term, options = {})
7
- response = get(
8
- '/api/v1/search/emoji.json',
9
- options.merge('q[code_cont]' => term)
10
- )
11
- response[:body]
12
- end
13
- end
14
- end
15
- end
16
- end
@@ -1,60 +0,0 @@
1
- require 'faraday'
2
- require 'faraday_middleware'
3
-
4
- require 'emojidex/api/categories'
5
- require 'emojidex/api/emoji'
6
- require 'emojidex/api/search/emoji'
7
-
8
- module Emojidex
9
- # get the data from emojidex.com
10
- class Client
11
- attr_accessor :api_key, :api_username
12
- attr_reader :host
13
-
14
- include Emojidex::API::Categories
15
- include Emojidex::API::Emoji
16
- include Emojidex::API::Search::Emoji
17
-
18
- def initialize(opts = {})
19
- @api_key = opts[:api_key]
20
- @api_username = opts[:api_username]
21
- @host = opts[:host] || 'https://www.emojidex.com/'
22
- end
23
-
24
- def connection_options
25
- @connection_options ||= {
26
- url: @host,
27
- headers: {
28
- accept: 'application/json',
29
- user_agent: user_agent
30
- }
31
- }
32
- end
33
-
34
- def user_agent
35
- @user_agent ||= 'Emojidex Toolkit Client'
36
- end
37
-
38
- def get(path, params = {})
39
- request(:get, path, params)
40
- end
41
-
42
- private
43
-
44
- def connection
45
- @connection ||= Faraday.new connection_options do |conn|
46
- conn.request :url_encoded
47
- conn.response :json
48
- # conn.response :logger
49
- conn.adapter Faraday.default_adapter
50
- end
51
- end
52
-
53
- def request(method, path, params = {})
54
- response = connection.send(method.to_sym, path, params)
55
- response.env
56
- rescue Faraday::Error::ClientError, JSON::ParserError
57
- raise Emojidex::Error
58
- end
59
- end
60
- end
@@ -1,156 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'json'
4
- require_relative 'emoji'
5
- require_relative 'categories'
6
- require_relative 'collection/cache'
7
- require_relative 'collection/asset_information'
8
- require_relative 'collection/moji_data'
9
-
10
- module Emojidex
11
- # listing and search of standard UTF emoji
12
- class Collection
13
- include Emojidex::CollectionCache
14
- include Emojidex::CollectionAssetInformation
15
- include Emojidex::CollectionMojiData
16
- attr_accessor :emoji, :categories,
17
- :source_path, :vector_source_path, :raster_source_path
18
-
19
- # Initialize Collection. You can pass a list of emoji to seed the collection
20
- def initialize(emoji_list = nil, local_load_path = nil)
21
- @emoji = {}
22
- load_local_collection(local_load_path) unless local_load_path.nil?
23
- add_emoji(emoji_list) unless emoji_list.nil?
24
- end
25
-
26
- # Loads an emoji collection on local storage
27
- def load_local_collection(path)
28
- @source_path = File.expand_path(path)
29
- @vector_source_path = @source_path if @vector_source_path.nil?
30
- @raster_source_path = @source_path if @raster_source_path.nil?
31
- json = IO.read(@source_path + '/emoji.json')
32
- list = JSON.parse(json, symbolize_names: true)
33
- add_emoji(list)
34
- end
35
-
36
- # each override to map each functionality to the emoji hash values
37
- def each(&block)
38
- @emoji.values.each(&block)
39
- end
40
-
41
- # select override to map select functionality to the emoji hash values
42
- def select(&block)
43
- @emoji.values.select(&block)
44
- end
45
-
46
- def map(&block)
47
- @emoji.values.map(&block)
48
- end
49
-
50
- def collect(&block)
51
- @emoji.values.collect(&block)
52
- end
53
-
54
- # Retreives an Emoji object by the actual moji code/character code
55
- # Will likely only return moji from UTF collection
56
- def find_by_moji(moji)
57
- each do |m|
58
- return m if m[:moji] == moji
59
- end
60
- nil
61
- end
62
-
63
- alias_method :文字検索, :find_by_moji
64
-
65
- # Gets the emoji with the specified code
66
- # Returns the Emoji object or nil if no emoji with that code is found
67
- def find_by_code(code)
68
- @emoji[code.gsub(/\s/, '_').to_sym]
69
- end
70
-
71
- # Locates emoji by Japanese code (original Japanese emoji name [絵文字名])
72
- # Only applies to collections that contain JA codes, this function is mapped to
73
- # find_by_code for all other implementations (such as client)
74
- def find_by_code_ja(code_ja)
75
- each do |m|
76
- return m if m[:code_ja] == code_ja
77
- end
78
- nil
79
- end
80
-
81
- alias_method :コード検索, :find_by_code_ja
82
-
83
- def find_by_unicode(unicode)
84
- unicode = unicode.downcase
85
- each do |m|
86
- return m if m[:unicode] == unicode
87
- end
88
- nil
89
- end
90
-
91
- def search(criteria = {})
92
- Emojidex::Collection.new _sub_search(@emoji.values.dup, criteria)
93
- end
94
-
95
- # Get all emoji from this collection that are part of the specified category
96
- # Returns a new collection of only emoji in the specified category
97
- def category(category_code)
98
- categorized = @emoji.values.select { |moji| moji.category == category_code }
99
- Emojidex::Collection.new categorized
100
- end
101
-
102
- # Check to see if there are emoji in this collection which have the specified categories
103
- # Returns true if there are emoji for all secified categories within this collection
104
- def category?(*category_codes)
105
- (category_codes.uniq - @categories).empty?
106
- end
107
-
108
- # Adds emojis to the collection
109
- # After add categories are updated
110
- def add_emoji(list)
111
- list.each do |moji_info|
112
- if moji_info.instance_of? Emojidex::Emoji
113
- @emoji[moji_info.code.to_sym] = moji_info.dup
114
- else
115
- emoji = Emojidex::Emoji.new moji_info
116
- @emoji[emoji.code.to_sym] = emoji
117
- end
118
- end
119
- categorize
120
- associate_variants
121
- condense_moji_code_data
122
- @emoji
123
- end
124
-
125
- alias_method :<<, :add_emoji
126
-
127
- private
128
-
129
- # Makes a list of all categories which contain emoji in this collection
130
- def categorize
131
- @categories = @emoji.values.map { |moji| moji.category }
132
- @categories.uniq!
133
- end
134
-
135
- def associate_variants
136
- @emoji.values.each do |emoji_obj|
137
- if emoji_obj.code.match(/\(.*\)$/) # this emoji is a variant
138
- # check for base
139
- base_code = emoji_obj.code.sub(/\(.*\)$/, '').to_sym
140
- if @emoji.has_key? base_code
141
- @emoji[base_code].variants << emoji_obj.code.to_sym
142
- emoji_obj.base = base_code
143
- end
144
- end
145
- end
146
- end
147
-
148
- def _sub_search(list, criteria = {})
149
- cr = criteria.shift
150
- return list if cr.nil?
151
-
152
- list = list.select { |moji| moji if moji[cr[0]] =~ /#{cr[1]}/ }
153
- _sub_search(list, criteria)
154
- end
155
- end
156
- end
@@ -1,49 +0,0 @@
1
- require 'digest/md5'
2
-
3
- module Emojidex
4
- # Asset Information for Collections
5
- module CollectionAssetInformation
6
- def generate_checksums(formats = Emojidex::Defaults.formats, sizes = Emojidex::Defaults.sizes)
7
- @emoji.values.each do |moji|
8
- moji.checksums = get_checksums(moji, formats, sizes)
9
- end
10
- end
11
-
12
- def get_checksums(moji, formats, sizes)
13
- sums = {}
14
- sums[:svg] = _checksum_for_file("#{@vector_source_path}/#{moji.code}.svg") if formats.include? :svg
15
- if formats.include? :png
16
- sums[:png] = {}
17
- sizes.keys.each do |size|
18
- sums[:png][size] = _checksum_for_file("#{@raster_source_path}/#{size}/#{moji.code}.png")
19
- end
20
- end
21
- sums
22
- end
23
-
24
- def generate_paths(formats = Emojidex::Defaults.formats, sizes = Emojidex::Defaults.sizes)
25
- @emoji.values.each do |moji|
26
- moji.paths = get_paths(moji, formats, sizes)
27
- end
28
- end
29
-
30
- def get_paths(moji, formats, sizes)
31
- paths = {}
32
- path = "#{@vector_source_path}/#{moji.code}.svg"
33
- paths[:svg] = path if File.exist? path
34
- if formats.include? :png
35
- paths[:png] = {}
36
- sizes.keys.each do |size|
37
- path = "#{@raster_source_path}/#{size}/#{moji.code}.png"
38
- paths[:png][size] = path if File.exist? path
39
- end
40
- end
41
- paths
42
- end
43
-
44
- private
45
- def _checksum_for_file(path)
46
- (File.exist? path)? Digest::MD5.file(path).hexdigest : nil
47
- end
48
- end
49
- end
@@ -1,78 +0,0 @@
1
- require 'json'
2
- require 'fileutils'
3
- require_relative '../defaults'
4
-
5
- module Emojidex
6
- # local caching functionality for collections
7
- module CollectionCache
8
- attr_reader :cache_path
9
-
10
- def setup_cache(path = nil)
11
- # check if cache dir is already set
12
- return @cache_path if @cache_path && path.nil?
13
- # setup cache
14
- @cache_path = File.expand_path(path || ENV['EMOJI_CACHE'] || "#{ENV['HOME']}/.emojidex/cache")
15
- ENV['EMOJI_CACHE'] = @cache_path
16
- FileUtils.mkdir_p(@cache_path)
17
- Emojidex::Defaults.sizes.keys.each do |size|
18
- FileUtils.mkdir_p(@cache_path + "/#{size}")
19
- end
20
- @cache_path
21
- end
22
-
23
- # Caches emoji to local emoji storage cache
24
- # Options:
25
- # cache_path: manually specify cache location
26
- # (default is ENV['EMOJI_CACHE'] or '$HOME/.emoji_cache')
27
- # formats: formats to cache (default is SVG only)
28
- # sizes: sizes to cache (default is px32, but this is irrelivant for SVG)
29
- def cache!(options = {})
30
- setup_cache options[:cache_path]
31
- formats = options[:formats] || [:svg, :png]
32
- sizes = options[:sizes] || [:px32]
33
- @emoji.values.each do |moji|
34
- _svg_check_copy(moji) if formats.include? :svg
35
- _raster_check_copy(moji, :png, sizes) if formats.include? :png
36
- end
37
- cache_index
38
- end
39
-
40
- def cache_index(destination = nil)
41
- destination ||= @cache_path
42
- idx = Emojidex::Collection.new
43
- idx.load_local_collection(destination) if FileTest.exist? "#{destination}/emoji.json"
44
- idx.add_emoji @emoji.values
45
- File.open("#{destination}/emoji.json", 'w') do |f|
46
- f.write idx.emoji.values.to_json
47
- end
48
- end
49
-
50
- def write_index(destination)
51
- idx = @emoji.values.to_json
52
- idx = JSON.parse idx
53
- idx.each { |moji| moji.delete_if{ |k, v| v.nil? }}
54
- File.open("#{destination}/emoji.json", 'w') { |f| f.write idx.to_json }
55
- end
56
-
57
- private
58
-
59
- def _svg_check_copy(moji)
60
- src = @source_path + "/#{moji.code}"
61
- if File.exist? "#{src}.svg"
62
- unless File.exist?("#{@cache_path}/#{moji.code}.svg") &&
63
- FileUtils.compare_file("#{src}.svg", "#{@cache_path}/#{moji.code}.svg")
64
- FileUtils.cp("#{src}.svg", @cache_path)
65
- end
66
- end
67
- FileUtils.cp_r src, @cache_path if File.directory? src
68
- end
69
-
70
- def _raster_check_copy(moji, format, sizes)
71
- sizes.each do |size|
72
- src = @source_path + "/#{size}/#{moji.code}"
73
- FileUtils.cp "#{src}.#{format}", (@cache_path + "/#{size}") if FileTest.exist? "#{src}.#{format}"
74
- FileUtils.cp_r src, @cache_path if File.directory? src
75
- end
76
- end
77
- end
78
- end
@@ -1,93 +0,0 @@
1
-
2
- require 'find'
3
- require_relative 'defaults.rb'
4
-
5
- module Emojidex
6
- # Check collections for presence of image assets and discrepencies in emoji indexes.
7
- class CollectionChecker
8
- attr_reader :index_only, :asset_only
9
-
10
- def initialize(collections, options = {})
11
- collections = *collections
12
- @index_only = {}
13
- @asset_only = {}
14
- asset_path = options[:asset_path] || collections.first.source_path
15
- sizes = options[:sizes] || Defaults.sizes.keys
16
- formats = options[:formats] || Defaults.formats
17
-
18
- asset_files = create_asset_file_list(asset_path, sizes, formats)
19
- check_for_index_only(collections, asset_files, sizes, formats)
20
- check_for_asset_only(collections, asset_files)
21
- end
22
-
23
- private
24
-
25
- def create_asset_file_list(dir, sizes, formats)
26
- result = {}
27
- result.merge!(create_file_list(dir, '.svg', '')) if formats.include?(:svg)
28
- if formats.include?(:png)
29
- sizes.each do |size|
30
- result.merge!(create_file_list("#{dir}/#{size}", '.png', "#{size}/"))
31
- end
32
- end
33
- result
34
- end
35
-
36
- def create_file_list(dir, ext, prefix)
37
- result = {}
38
- return result unless Dir.exists?(dir)
39
- Dir.foreach(dir) do |file|
40
- result["#{prefix}#{File.basename(file, '.*')}".to_sym] =
41
- "#{prefix}#{file}" if File.extname(file) == ext
42
- end
43
- result
44
- end
45
-
46
- def check_for_index_only(collections, asset_files, sizes, formats)
47
- collections.each do |collection|
48
- collection.emoji.values.each do |emoji|
49
- tmp = []
50
- tmp += create_svg_array(emoji, asset_files) if formats.include?(:svg)
51
- tmp += create_png_array(emoji, asset_files, sizes) if formats.include?(:png)
52
- @index_only[emoji.code.to_sym] = tmp unless tmp.empty?
53
- end
54
- end
55
- end
56
-
57
- def create_svg_array(emoji, asset_files)
58
- result = []
59
- asset_file = asset_files[emoji.code.to_sym]
60
- result << "#{emoji.code}.svg" if asset_files[emoji.code.to_sym].nil?
61
- result
62
- end
63
-
64
- def create_png_array(emoji, asset_files, sizes)
65
- result = []
66
- sizes.each do |size|
67
- name = "#{size}/#{emoji.code}"
68
- result << "#{name}.png" if asset_files[name.to_sym].nil?
69
- end
70
- result
71
- end
72
-
73
- def check_for_asset_only(collections, asset_files)
74
- asset_files.each do |_key, value|
75
- code = File.basename(value, '.*')
76
-
77
- next if find_emoji_from_collections(collections, code)
78
-
79
- symbol = code.to_sym
80
- @asset_only[symbol] = [] if @asset_only[symbol].nil?
81
- @asset_only[symbol] << value
82
- end
83
- end
84
-
85
- def find_emoji_from_collections(collections, code)
86
- symbol = code.to_sym
87
- collections.each do |collection|
88
- return true unless collection.emoji[symbol].nil?
89
- end
90
- false
91
- end
92
- end # class CollectionChecker
93
- end # module Emojidex