piotr_majewski_magic 0.0.6 → 0.0.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 501de2d0b9fd09066c8e4adc35a4fd690ff74e8e
4
- data.tar.gz: 32250955f24b4476b04aad15e63c690ccad9cf2c
2
+ SHA256:
3
+ metadata.gz: 625277973f304598447198105e250e744f87280393b445b47314e1bf40be8684
4
+ data.tar.gz: f5371be4205526ac1e12cacf8022e9cd7971c6dab715c4175696b388fa5a98b5
5
5
  SHA512:
6
- metadata.gz: b6e77141adff2298a2788db314a88f38a9c90319777ef90ab270ad3d5e904970103c4dc89098aafec31147350998b0fdb3d9b5efe40b1819a45d6d2236b4afe5
7
- data.tar.gz: a0ee9174bff5d8ae56709ef7ebd4145b9789fe84be2a91141466cb792c16d6362d83472f889df420cc7af75f6791614a853ea4d897555222571ac5a5c8e5e326
6
+ metadata.gz: 306ae43b9ff7ffa373865a013e78f6a8fdbcca3a90bc24ad298d5547b5029a227560de4ffc6f7b910ab8ed4b9eeeb8d177310c4fe7b1f1adb4d10d67df87132a
7
+ data.tar.gz: 2b3b58bcaa0d1dd094fb7db68d43bb3dbfc60a171af26026bfeb670e82d188e60a96bf3ef2e9686b7b7ee105a23245ecac5fc69bcdb5e901b62647b99aaa4cda
data/cache/cards.json CHANGED
@@ -0,0 +1 @@
1
+ {}
@@ -3,13 +3,13 @@
3
3
  require 'json'
4
4
  require_relative 'mtg_api_response'
5
5
 
6
- class CardsSource
6
+ class Cards
7
7
  def self.get(no_cache = nil)
8
8
  new(no_cache).get
9
9
  end
10
10
 
11
11
  def get
12
- JSON.parse(cards_source)
12
+ JSON.parse(cards)
13
13
  end
14
14
 
15
15
  private
@@ -20,8 +20,8 @@ class CardsSource
20
20
  @no_cache = no_cache
21
21
  end
22
22
 
23
- def cards_source
24
- fetch_cards_from_api if no_cache || cache_empty?
23
+ def cards
24
+ fetch_and_save_cards_from_api if no_cache || cache_empty?
25
25
 
26
26
  read_cache
27
27
  end
@@ -31,10 +31,10 @@ class CardsSource
31
31
  end
32
32
 
33
33
  def cache_empty?
34
- File.size(cache_file_path).zero?
34
+ File.size(cache_file_path).zero?
35
35
  end
36
36
 
37
- def fetch_cards_from_api
37
+ def fetch_and_save_cards_from_api
38
38
  File.open(cache_file_path, 'w') do |file|
39
39
  file.write(MtgApiResponse.results)
40
40
  end
data/lib/cards_manager.rb CHANGED
@@ -22,15 +22,16 @@ class CardsManager
22
22
  private
23
23
 
24
24
  def group_and_select(query, cards)
25
- raise UnknownAttribute unless cards.first.key?(query[0][:attribute])
25
+ attribute = query[0][:attribute]
26
+ unknown_attribute(attribute) unless cards.first.key?(attribute)
26
27
 
27
28
  selected_cards = matching_cards(query[0], cards)
28
29
  new_query = query[1..]
29
30
 
30
31
  return selected_cards if new_query.empty?
31
32
 
32
- selected_cards.map.with_object({}) do |(attribute_value, cards), hash|
33
- hash[attribute_value] = group_and_select(new_query, cards)
33
+ selected_cards.map.with_object({}) do |(attribute_value, card_set), hash|
34
+ hash[attribute_value] = group_and_select(new_query, card_set)
34
35
  end
35
36
  end
36
37
 
@@ -46,6 +47,14 @@ class CardsManager
46
47
 
47
48
  cli_value.tally == source_value&.tally
48
49
  rescue NoMethodError
50
+ invalid_value(cli_value)
51
+ end
52
+
53
+ def unknown_attribute(attribute)
54
+ raise UnknownAttribute, "`#{attribute}`: Is not an MTG card attribute"
55
+ end
56
+
57
+ def invalid_value(cli_value)
49
58
  message = "`#{cli_value}`: Type of provided value doesn't match type of card attribute"
50
59
  raise InvalidValue, message
51
60
  end
@@ -30,10 +30,10 @@ class MtgApiRequest
30
30
  private
31
31
 
32
32
  def unfetched_pages
33
- @unfetched_pages ||= (1..total_count).to_a
33
+ @unfetched_pages ||= (1..total_page_count).to_a
34
34
  end
35
35
 
36
- def total_count
36
+ def total_page_count
37
37
  response = Faraday.get(URL)
38
38
  (response.headers['total-count'].to_f / 100).ceil
39
39
  end
data/lib/query.rb CHANGED
@@ -5,6 +5,9 @@ require 'forwardable'
5
5
  class Query
6
6
  extend Forwardable
7
7
 
8
+ ATTRIBUTE_AND_VALUE_DIVIDERS = /(?<!~)=/.freeze
9
+ VALUE_TO_LIST_DIVIDERS = /(?<!~)&/.freeze
10
+
8
11
  RepeatedFilter = Class.new(StandardError)
9
12
  Group = Class.new
10
13
 
@@ -16,11 +19,10 @@ class Query
16
19
 
17
20
  def build
18
21
  @build ||= argv.map.with_object([]) do |argument, query_options|
19
- query_element = parse_argument(argument)
22
+ query_element = query_element(argument)
20
23
 
21
24
  if query_options.find { |el| el[:attribute] == query_element[:attribute] }
22
- message = "#{query_element[:attribute]}: Every filter can be passed only once"
23
- raise RepeatedFilter, message
25
+ repeated_filter(query_element[:attribute])
24
26
  end
25
27
 
26
28
  query_options << query_element
@@ -31,15 +33,35 @@ class Query
31
33
 
32
34
  private
33
35
 
34
- # TODO: pick a divider (something that is unlikely to show up)
35
- # potential problem with imageUrl values
36
- # NOTE: should allow escaping `#`
37
- def parse_argument(argument)
38
- return { attribute: argument, value: Group } unless argument.match?('=')
36
+ def query_element(argument)
37
+ return { attribute: argument, value: Group } unless argument.match?(ATTRIBUTE_AND_VALUE_DIVIDERS)
38
+
39
+ attribute, value = argument.split(ATTRIBUTE_AND_VALUE_DIVIDERS)
40
+
41
+ {
42
+ attribute: sanitize(attribute),
43
+ value: sanitize(parse_value(value))
44
+ }
45
+ end
46
+
47
+ def parse_value(value)
48
+ return value unless value&.match?(VALUE_TO_LIST_DIVIDERS)
39
49
 
40
- attribute, value = argument.split('=')
41
- value = value&.match?('#') ? value.split('#') : value
50
+ value.split(VALUE_TO_LIST_DIVIDERS)
51
+ end
52
+
53
+ def sanitize(query_part)
54
+ return unless query_part
55
+
56
+ if query_part.is_a?(String)
57
+ query_part.gsub(/~=/, '=').gsub(/~&/, '&')
58
+ else
59
+ query_part.map { |part| sanitize(part) }
60
+ end
61
+ end
42
62
 
43
- { attribute: attribute, value: value }
63
+ def repeated_filter(attribute)
64
+ message = "#{attribute}: Every filter can be passed only once"
65
+ raise RepeatedFilter, message
44
66
  end
45
67
  end
data/lib/query_mtg.rb CHANGED
@@ -3,14 +3,13 @@
3
3
  require 'json'
4
4
  require_relative 'cards_manager'
5
5
  require_relative 'query'
6
- require_relative 'cards_source'
6
+ require_relative 'cards'
7
7
 
8
8
  class QueryMtg
9
9
  def self.call(argv)
10
10
  new(argv).call
11
11
  end
12
12
 
13
- # NOTE: puts to output to CLI
14
13
  def call
15
14
  puts requested_cards
16
15
  rescue Query::RepeatedFilter, CardsManager::InvalidValue, CardsManager::UnknownAttribute => e
@@ -35,6 +34,6 @@ class QueryMtg
35
34
  end
36
35
 
37
36
  def cards_source
38
- CardsSource.get(no_cache)
37
+ Cards.get(no_cache)
39
38
  end
40
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: piotr_majewski_magic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Majewski
@@ -48,8 +48,8 @@ extra_rdoc_files: []
48
48
  files:
49
49
  - bin/query_mtg
50
50
  - cache/cards.json
51
+ - lib/cards.rb
51
52
  - lib/cards_manager.rb
52
- - lib/cards_source.rb
53
53
  - lib/mtg_api_request.rb
54
54
  - lib/mtg_api_response.rb
55
55
  - lib/query.rb
@@ -73,8 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
73
  - !ruby/object:Gem::Version
74
74
  version: '0'
75
75
  requirements: []
76
- rubyforge_project:
77
- rubygems_version: 2.6.12
76
+ rubygems_version: 3.1.4
78
77
  signing_key:
79
78
  specification_version: 4
80
79
  summary: CLI for querying MTG.io