emojidex 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/emojidex.gemspec +7 -6
  3. data/lib/emojidex.rb +9 -1
  4. data/lib/emojidex/client.rb +53 -0
  5. data/lib/emojidex/data/categories.rb +42 -42
  6. data/lib/emojidex/data/category.rb +10 -10
  7. data/lib/emojidex/data/collection.rb +39 -21
  8. data/lib/emojidex/data/collection/asset_information.rb +19 -3
  9. data/lib/emojidex/data/collection/cache.rb +28 -33
  10. data/lib/emojidex/data/collection/moji_data.rb +13 -11
  11. data/lib/emojidex/data/collection/static_collection.rb +3 -3
  12. data/lib/emojidex/data/emoji.rb +54 -37
  13. data/lib/emojidex/data/emoji/asset_information.rb +86 -3
  14. data/lib/emojidex/data/extended.rb +2 -2
  15. data/lib/emojidex/data/utf.rb +2 -2
  16. data/lib/emojidex/defaults.rb +9 -1
  17. data/lib/emojidex/env_helper.rb +1 -2
  18. data/lib/emojidex/service/collection.rb +70 -31
  19. data/lib/emojidex/service/error.rb +1 -0
  20. data/lib/emojidex/service/indexes.rb +31 -10
  21. data/lib/emojidex/service/search.rb +5 -4
  22. data/lib/emojidex/service/transactor.rb +35 -19
  23. data/lib/emojidex/service/user.rb +80 -37
  24. data/lib/emojidex/service/user/history_item.rb +23 -0
  25. data/lib/emojidex/{data → util}/collection_checker.rb +2 -3
  26. metadata +10 -79
  27. data/.coveralls.yml +0 -1
  28. data/.gitignore +0 -12
  29. data/.rspec +0 -2
  30. data/.rubocop.yml +0 -18
  31. data/.travis.yml +0 -18
  32. data/Gemfile +0 -18
  33. data/Guardfile +0 -19
  34. data/README.md +0 -73
  35. data/spec/emojidex/data/categories_spec.rb +0 -19
  36. data/spec/emojidex/data/collection_checker_spec.rb +0 -72
  37. data/spec/emojidex/data/collection_spec.rb +0 -136
  38. data/spec/emojidex/data/emoji_spec.rb +0 -47
  39. data/spec/emojidex/data/extended_spec.rb +0 -118
  40. data/spec/emojidex/data/utf_spec.rb +0 -91
  41. data/spec/emojidex/service/collection_spec.rb +0 -20
  42. data/spec/emojidex/service/error_spec.rb +0 -17
  43. data/spec/emojidex/service/indexes_spec.rb +0 -62
  44. data/spec/emojidex/service/search_spec.rb +0 -87
  45. data/spec/emojidex/service/transactor_spec.rb +0 -11
  46. data/spec/emojidex/service/user_spec.rb +0 -128
  47. data/spec/spec_helper.rb +0 -18
  48. data/spec/support/Genshin.svg +0 -126
  49. data/spec/support/couple_kiss/0.svg +0 -177
  50. data/spec/support/couple_kiss/1.svg +0 -177
  51. data/spec/support/couple_kiss/10.svg +0 -293
  52. data/spec/support/couple_kiss/11.svg +0 -235
  53. data/spec/support/couple_kiss/12.svg +0 -235
  54. data/spec/support/couple_kiss/13.svg +0 -235
  55. data/spec/support/couple_kiss/14.svg +0 -177
  56. data/spec/support/couple_kiss/15.svg +0 -177
  57. data/spec/support/couple_kiss/2.svg +0 -177
  58. data/spec/support/couple_kiss/3.svg +0 -235
  59. data/spec/support/couple_kiss/4.svg +0 -235
  60. data/spec/support/couple_kiss/5.svg +0 -235
  61. data/spec/support/couple_kiss/6.svg +0 -293
  62. data/spec/support/couple_kiss/7.svg +0 -235
  63. data/spec/support/couple_kiss/8.svg +0 -235
  64. data/spec/support/couple_kiss/9.svg +0 -235
  65. data/spec/support/couple_kiss/animation.json +0 -14
  66. data/spec/support/fixtures/categories.json +0 -14
  67. data/spec/support/fixtures/category.json +0 -5
  68. data/spec/support/fixtures/emoji.json +0 -28
  69. data/spec/support/fixtures/emoji_detailed.json +0 -41
  70. data/spec/support/fixtures/search_emoji.json +0 -21
  71. data/spec/support/fixtures/single_emoji.json +0 -19
  72. data/spec/support/fixtures/single_emoji_detailed.json +0 -30
  73. data/spec/support/sample_collections/good/emoji.json +0 -30
  74. data/spec/support/sample_collections/good/mouth.svg +0 -49
  75. data/spec/support/sample_collections/good/nut_and_bolt.svg +0 -372
  76. data/spec/support/sample_collections/good/purple_heart.svg +0 -24
  77. data/spec/support/sample_collections/good/px32/mouth.png +0 -0
  78. data/spec/support/sample_collections/good/px32/nut_and_bolt.png +0 -0
  79. data/spec/support/sample_collections/good/px32/purple_heart.png +0 -0
  80. data/spec/support/sample_collections/good/px32/woman_with_bunny_ears.png +0 -0
  81. data/spec/support/sample_collections/good/woman_with_bunny_ears.svg +0 -90
  82. data/spec/support/sample_collections/missing_assets/emoji.json +0 -30
  83. data/spec/support/sample_collections/missing_assets/mouth.svg +0 -49
  84. data/spec/support/sample_collections/missing_assets/purple_heart.svg +0 -24
  85. data/spec/support/sample_collections/missing_assets/px32/mouth.png +0 -0
  86. data/spec/support/sample_collections/missing_assets/px32/nut_and_bolt.png +0 -0
  87. data/spec/support/sample_collections/missing_assets/px32/woman_with_bunny_ears.png +0 -0
  88. data/spec/support/sample_collections/missing_assets/woman_with_bunny_ears.svg +0 -90
  89. data/spec/support/sample_collections/missing_index/emoji.json +0 -23
  90. data/spec/support/sample_collections/missing_index/mouth.svg +0 -49
  91. data/spec/support/sample_collections/missing_index/nut_and_bolt.svg +0 -372
  92. data/spec/support/sample_collections/missing_index/purple_heart.svg +0 -24
  93. data/spec/support/sample_collections/missing_index/px32/mouth.png +0 -0
  94. data/spec/support/sample_collections/missing_index/px32/nut_and_bolt.png +0 -0
  95. data/spec/support/sample_collections/missing_index/px32/purple_heart.png +0 -0
  96. data/spec/support/sample_collections/missing_index/px32/woman_with_bunny_ears.png +0 -0
  97. data/spec/support/sample_collections/missing_index/woman_with_bunny_ears.svg +0 -90
@@ -1,16 +1,18 @@
1
- module Emojidex::Data
2
- # collects and condenses UTF moji codes within a collection
3
- module CollectionMojiData
4
- attr_reader :moji_code_string, :moji_code_index
1
+ module Emojidex
2
+ module Data
3
+ # collects and condenses UTF moji codes within a collection
4
+ module CollectionMojiData
5
+ attr_reader :moji_code_string, :moji_code_index
5
6
 
6
- def condense_moji_code_data
7
- @moji_code_string = ""
8
- @moji_code_index = {}
7
+ def condense_moji_code_data
8
+ @moji_code_string = ''
9
+ @moji_code_index = {}
9
10
 
10
- @emoji.values.each do |moji|
11
- if moji.moji != nil
12
- @moji_code_string << moji.moji
13
- @moji_code_index[moji.moji] = moji.code
11
+ @emoji.values.each do |moji|
12
+ unless moji.moji.nil?
13
+ @moji_code_string << moji.moji
14
+ @moji_code_index[moji.moji] = moji.code
15
+ end
14
16
  end
15
17
  end
16
18
  end
@@ -8,12 +8,12 @@ module Emojidex
8
8
  def load_from_server(detailed = true, locale = '??')
9
9
  locale = Emojidex::EnvHelper.lang? if locale == '??'
10
10
  begin
11
- res = Emojidex::Service::Transactor.get(@endpoint, {detailed: detailed, locale: locale})
11
+ res = Emojidex::Service::Transactor.get(@endpoint, detailed: detailed, locale: locale)
12
12
  rescue
13
- return false
13
+ false
14
14
  end
15
15
  add_emoji(res)
16
- return true
16
+ true
17
17
  end
18
18
 
19
19
  def check_and_load_static(collection)
@@ -1,48 +1,65 @@
1
+ require_relative '../../emojidex'
1
2
  require_relative 'emoji/asset_information'
2
3
 
3
- module Emojidex::Data
4
- # emoji base class
5
- class Emoji
6
- attr_accessor :moji, :category, :code, :code_ja,
7
- :unicode, :tags, :emoticon, :variants, :base
8
-
9
- include Emojidex::Data::EmojiAssetInformation
10
-
11
- def initialize(details = {})
12
- init_asset_info(details)
13
- @moji = details[:moji]
14
- @code, @code_ja = details[:code], details[:code_ja]
15
- @unicode, @full_name = details[:unicode], details[:full_name]
16
- @emoticon = details[:emoticon]
17
- @category = details[:category] ? details[:category].to_sym : :other
18
- @tags = details[:tags].map { |tag| tag.to_sym } unless details[:tags].nil?
19
- @link = details[:link]
20
- @variants, @base = details[:variants] || [], details[:base]
21
- @is_wide = details[:is_wide]
22
- end
4
+ module Emojidex
5
+ module Data
6
+ # emoji base class
7
+ class Emoji
8
+ attr_accessor :moji, :category, :code, :code_ja,
9
+ :unicode, :tags, :emoticon, :variants, :base
23
10
 
24
- def to_s
25
- @moji || ":#{@code}:"
26
- end
11
+ include Emojidex::Data::EmojiAssetInformation
27
12
 
28
- def to_json(*args)
29
- to_hash.to_json(*args)
30
- end
13
+ def initialize(details = {})
14
+ _init_identifier_info(details)
15
+ _init_descriptor_info(details)
16
+ init_asset_info(details)
17
+ end
31
18
 
32
- def to_hash
33
- hash = {}
34
- instance_variables.each do |key|
35
- hash[key.to_s.delete('@')] = instance_variable_get(key)
19
+ def to_s
20
+ @moji || Emojidex.encapsulate_code(@code)
36
21
  end
37
- hash
38
- end
39
22
 
40
- def [](key)
41
- instance_variable_get(key.to_s.delete(':').insert(0, '@'))
42
- end
23
+ def to_json(*args)
24
+ to_hash.to_json(*args)
25
+ end
26
+
27
+ def to_hash
28
+ hash = {}
29
+ instance_variables.each do |key|
30
+ hash[key.to_s.delete('@')] = instance_variable_get(key)
31
+ end
32
+ hash
33
+ end
43
34
 
44
- def []=(key, val)
45
- instance_variable_set(key.to_s.delete(':').insert(0, '@'), val)
35
+ def [](key)
36
+ instance_variable_get(key.to_s.delete(':').insert(0, '@'))
37
+ end
38
+
39
+ def []=(key, val)
40
+ instance_variable_set(key.to_s.delete(':').insert(0, '@'), val)
41
+ end
42
+
43
+ private
44
+
45
+ def _init_identifier_info(details)
46
+ @moji = details[:moji].to_s
47
+ @code = Emojidex.escape_code(details[:code].to_s)
48
+ @code_ja = Emojidex.escape_code(details[:code_ja].to_s)
49
+ @unicode = details[:unicode].to_s
50
+ @full_name = details[:full_name].to_s
51
+ @emoticon = details[:emoticon].to_s
52
+ end
53
+
54
+ def _init_descriptor_info(details)
55
+ @category = details[:category] ? details[:category].to_sym : :other
56
+ @tags = details[:tags].map(&:to_sym) unless details[:tags].nil?
57
+ @link = details[:link].to_s
58
+ @variants = details[:variants] || []
59
+ @variants.uniq!
60
+ @base = details[:base]
61
+ @is_wide = details[:is_wide]
62
+ end
46
63
  end
47
64
  end
48
65
  end
@@ -1,10 +1,11 @@
1
+ require 'digest/md5'
1
2
  require_relative '../../defaults'
2
3
 
3
4
  module Emojidex
4
5
  module Data
5
6
  # Asset information for emoji
6
7
  module EmojiAssetInformation
7
- attr_accessor :checksums, :paths
8
+ attr_accessor :checksums, :paths, :local_checksums
8
9
 
9
10
  def init_asset_info(details)
10
11
  blank_checksums
@@ -25,6 +26,7 @@ module Emojidex
25
26
  Emojidex::Defaults.sizes.keys.each do |size|
26
27
  @checksums[:png][size] = nil
27
28
  end
29
+ @local_checksums = @checksums.dup
28
30
  end
29
31
 
30
32
  def fill_checksums(checksums)
@@ -35,10 +37,91 @@ module Emojidex
35
37
  end
36
38
  end
37
39
 
40
+ def fill_paths(paths)
41
+ @paths[:svg] = paths[:svg] if paths.include? :svg
42
+ return unless paths.include? :png
43
+ Emojidex::Defaults.sizes.keys.each do |size|
44
+ @paths[:png][size] = paths[:png][size] if paths[:png].include? size
45
+ end
46
+ end
47
+
48
+ # Acquires path and caches the target file if not found or out of date
49
+ def path(format, size = nil)
50
+ fp = path?(format, size)
51
+ cache(format, [size]) unless !fp.nil? && File.exist?(fp)
52
+ fp
53
+ end
54
+
38
55
  # returns asset path
39
56
  def path?(format, size = nil)
40
- return @paths[format][size] unless size.nil?
41
- @paths[format]
57
+ case format
58
+ when :svg
59
+ return @paths[format] if File.exist?(@paths[format])
60
+ when :png
61
+ return nil if size.nil?
62
+ return @paths[format][size] if File.exist?(@paths[format][size])
63
+ end
64
+ nil
65
+ end
66
+
67
+ # Caches a file of the specified format in the specified sizes
68
+ def cache(format, sizes = nil)
69
+ generate_local_checksums
70
+ case format
71
+ when :png
72
+ _cache_png(sizes) unless sizes.nil?
73
+ when :svg
74
+ _cache_svg
75
+ end
76
+ end
77
+
78
+ # Generates a checksum for each locally cached file
79
+ def generate_local_checksum(format, size = nil)
80
+ case format
81
+ when :png
82
+ return @local_checksums[:png][size] = _checksum_for_file(@paths[:png][size])
83
+ when :svg
84
+ return @local_checksum[:svg] = _checksum_for_file(@paths[:svg])
85
+ end
86
+ nil
87
+ end
88
+
89
+ def generate_local_checksums
90
+ @local_checksums[:svg] = _checksum_for_file(@paths[:svg])
91
+ @paths[:png].keys.each do |size|
92
+ @local_checksums[:png][size] = _checksum_for_file(@paths[:png][size])
93
+ end
94
+ @local_checksums
95
+ end
96
+
97
+ private
98
+
99
+ def _cache_svg
100
+ @paths[:svg] = Dir.pwd unless (@paths.include? :svg) && !@paths[:svg].nil?
101
+ return if File.exist?(@paths[:svg]) && !@checksums[:svg].nil? &&
102
+ @checksums[:svg] == generate_local_checksum(:svg)
103
+ response = Emojidex::Service::Transactor.download("#{@code}.svg")
104
+ File.open(@paths[:svg], 'wb') { |fp| fp.write(response.body) }
105
+ generate_local_checksum(:svg)
106
+ end
107
+
108
+ def _cache_png(sizes)
109
+ sizes.each do |size|
110
+ unless @paths.include?(:png) &&
111
+ @paths[:png].include?(size) && @paths[:png][size].nil? == false
112
+ @paths[:png][size] = "#{Dir.pwd}/#{size}/#{@code}.png"
113
+ end
114
+ next if File.exist?(@paths[:png][size]) &&
115
+ !@checksums[:png][size].nil? &&
116
+ @checksums[:png][size] == generate_local_checksum(:png, size)
117
+ response = Emojidex::Service::Transactor.download("#{size}/#{@code}.png")
118
+ File.open(@paths[:png][size], 'wb') { |fp| fp.write(response.body) }
119
+ generate_local_checksum(:png, size)
120
+ end
121
+ end
122
+
123
+ def _checksum_for_file(path)
124
+ (File.exist? path) ? Digest::MD5.file(path).hexdigest : nil
42
125
  end
43
126
  end
44
127
  end
@@ -7,8 +7,8 @@ module Emojidex
7
7
  class Extended < Collection
8
8
  include Emojidex::Data::StaticCollection
9
9
 
10
- def initialize
11
- super
10
+ def initialize(opts = {})
11
+ super(opts)
12
12
  @endpoint = 'extended_emoji'
13
13
  load_from_server unless check_and_load_static('extended')
14
14
  @emoji
@@ -7,8 +7,8 @@ module Emojidex
7
7
  class UTF < Collection
8
8
  include Emojidex::Data::StaticCollection
9
9
 
10
- def initialize
11
- super
10
+ def initialize(opts = {})
11
+ super(opts)
12
12
  @endpoint = 'utf_emoji'
13
13
  load_from_server unless check_and_load_static('utf')
14
14
  @emoji
@@ -1,7 +1,7 @@
1
1
  module Emojidex
2
2
  # Global defines for emojidex
3
3
  class Defaults
4
- @@selected_sizes = [:mdpi, :px32]
4
+ @@selected_sizes = [:hdpi, :px32]
5
5
  @@selected_formats = [:png]
6
6
 
7
7
  def self.sizes
@@ -31,5 +31,13 @@ module Emojidex
31
31
  def self.lang
32
32
  'en'
33
33
  end
34
+
35
+ def self.encapsulator
36
+ ':'
37
+ end
38
+
39
+ def self.system_cache_path
40
+ ENV['EMOJI_CACHE'] || "#{ENV['HOME']}/.emojidex/"
41
+ end
34
42
  end
35
43
  end
@@ -1,9 +1,8 @@
1
1
  module Emojidex
2
2
  # Obtains and parses local environment variables
3
3
  module EnvHelper
4
-
5
4
  def self.lang?
6
- lang = ENV['LANG'].match('^..')
5
+ lang = ENV['LANG'].match('^..') if ENV.include? 'LANG'
7
6
  lang = 'en' unless lang.to_s == 'ja'
8
7
  lang
9
8
  end
@@ -6,61 +6,100 @@ module Emojidex
6
6
  module Service
7
7
  # A modified collection class for collections tied to the emojidex service
8
8
  class Collection < Emojidex::Data::Collection
9
- attr_reader :endpoint, :page, :limit, :detailed, :auto_cache
9
+ attr_reader :endpoint, :page, :limit, :detailed, :auto_cache, :status
10
10
 
11
11
  def initialize(opts = {})
12
- @emoji = {}
13
- add_emoji(opts[:emoji]) && opts.delete(:emoji) if opts.include? :emoji
12
+ @opts = opts
14
13
 
15
- @username = opts[:username] || nil
16
- opts.delete(:username)
17
- @auth_token = opts[:auth_token] || nil
18
- opts.delete(:auth_token)
14
+ @status = ''
19
15
 
20
- @endpoint = opts[:endpoint] || 'emoji'
21
- opts.delete(:endpoint)
22
- @page = opts[:page] || 0
23
- opts.delete(:page)
24
- @limit = opts[:limit] || Emojidex::Defaults.limit
25
- opts.delete(:limit)
26
- @detailed = opts[:detailed] || false
27
- opts.delete(:detailed)
16
+ _init_emoji
17
+ _init_user_info
18
+ _init_endpoint
19
+ _init_cache
28
20
 
29
- @auto_cache = opts[:auto_cache] || true
30
- opts.delete(:auto_cache)
21
+ @auto_cache = @opts[:auto_cache] || true
22
+ @opts.delete(:auto_cache)
31
23
 
32
- auto_init = opts[:auto_init] || true
33
- opts.delete(:auto_init)
34
-
35
- @opts = opts
24
+ auto_init = @opts[:auto_init] || true
25
+ @opts.delete(:auto_init)
36
26
 
37
27
  more if auto_init
28
+
38
29
  @emoji
39
30
  end
40
31
 
41
32
  # Get the next page worth of emoji and add them to the collection
42
- def more()
33
+ def more
43
34
  @page += 1
44
35
 
45
36
  opts = { page: @page, limit: @limit, detailed: @detailed }
46
- opts[:username] = @username unless @username.nil?
47
- opts[:auth_token] = @auth_token unless @auth_token.nil?
37
+ opts[:username] = @username unless @username.nil? || @username == ''
38
+ opts[:auth_token] = @auth_token unless @auth_token.nil? || @auth_token == ''
48
39
  opts.merge! @opts
49
40
 
50
- page_moji = Emojidex::Service::Transactor.get(@endpoint, opts)
41
+ begin
42
+ moji_page = Emojidex::Service::Transactor.get(@endpoint, opts)
43
+ rescue Error::Unauthorized => e
44
+ @status = e.message
45
+ return {}
46
+ rescue Error::PaymentRequired => e
47
+ @status = e.message
48
+ return {}
49
+ end
50
+
51
+ _process_moji_page(moji_page)
52
+ end
53
+
54
+ private
55
+
56
+ def _init_emoji
57
+ @emoji = {}
58
+ return unless @opts.include? :emoji
59
+ add_emoji(@opts[:emoji])
60
+ @opts.delete(:emoji)
61
+ end
62
+
63
+ def _init_user_info
64
+ @username = @opts[:username] || nil
65
+ @opts.delete(:username)
66
+ @auth_token = @opts[:auth_token] || nil
67
+ @opts.delete(:auth_token)
68
+ end
69
+
70
+ def _init_endpoint
71
+ @endpoint = @opts[:endpoint] || 'emoji'
72
+ @opts.delete(:endpoint)
73
+ @page = @opts[:page] || 0
74
+ @opts.delete(:page)
75
+ @limit = @opts[:limit] || Emojidex::Defaults.limit
76
+ @opts.delete(:limit)
77
+ @detailed = @opts[:detailed] || false
78
+ @opts.delete(:detailed)
79
+ end
51
80
 
52
- if page_moji.is_a? Hash
53
- unless page_moji.key? :emoji
81
+ def _process_moji_page(moji_page)
82
+ if moji_page.is_a? Hash
83
+ unless moji_page.key? :emoji
54
84
  @page -= 1 # reset page beacuse we failed
55
85
  return {}
56
86
  end
57
87
 
58
- add_emoji(page_moji[:emoji])
59
- return page_moji[:emoji]
88
+ return add_emoji(moji_page[:emoji])
60
89
  end
61
- add_emoji(page_moji)
90
+
91
+ add_emoji(moji_page)
62
92
  cache! if @auto_cache
63
- page_moji
93
+ moji_page
94
+ end
95
+
96
+ def _init_cache
97
+ if @opts.include? :cache_path
98
+ setup_cache(@opts[:cache_path])
99
+ @opts.delete :cache_path
100
+ else
101
+ setup_cache
102
+ end
64
103
  end
65
104
  end
66
105
  end