piotr_majewski_magic 0.0.4 → 0.1
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 +4 -4
- data/lib/{cards_source.rb → cards.rb} +6 -7
- data/lib/cards_manager.rb +12 -3
- data/lib/mtg_api_request.rb +2 -2
- data/lib/query.rb +33 -11
- data/lib/query_mtg.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6341b23896c241c1b9d67ef5e9c43753f32e0504
|
4
|
+
data.tar.gz: a2214c807495e4f63e77e253663fb4ef4f66defd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2bbebe1218d2f3a13ed3a953eb9bdc68465ebb49acaf37fa94f5f401f486080ce3ec71a05ab9f3a71c551a40f597f1c557b9eea8396f6a4d864faed0079263f
|
7
|
+
data.tar.gz: bc546c26f24e818ef56812c9fee1a1e732abeada4af0e7507920394de1aaaf38d121254ae3550b412a39a0633744da5bf4e974774148f12fd1db33e15794a198
|
@@ -1,16 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'json'
|
4
|
-
require 'pry'
|
5
4
|
require_relative 'mtg_api_response'
|
6
5
|
|
7
|
-
class
|
6
|
+
class Cards
|
8
7
|
def self.get(no_cache = nil)
|
9
8
|
new(no_cache).get
|
10
9
|
end
|
11
10
|
|
12
11
|
def get
|
13
|
-
JSON.parse(
|
12
|
+
JSON.parse(cards)
|
14
13
|
end
|
15
14
|
|
16
15
|
private
|
@@ -21,8 +20,8 @@ class CardsSource
|
|
21
20
|
@no_cache = no_cache
|
22
21
|
end
|
23
22
|
|
24
|
-
def
|
25
|
-
|
23
|
+
def cards
|
24
|
+
fetch_and_save_cards_from_api if no_cache || cache_empty?
|
26
25
|
|
27
26
|
read_cache
|
28
27
|
end
|
@@ -32,10 +31,10 @@ class CardsSource
|
|
32
31
|
end
|
33
32
|
|
34
33
|
def cache_empty?
|
35
|
-
|
34
|
+
File.size(cache_file_path).zero?
|
36
35
|
end
|
37
36
|
|
38
|
-
def
|
37
|
+
def fetch_and_save_cards_from_api
|
39
38
|
File.open(cache_file_path, 'w') do |file|
|
40
39
|
file.write(MtgApiResponse.results)
|
41
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
|
-
|
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,
|
33
|
-
hash[attribute_value] = group_and_select(new_query,
|
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
|
data/lib/mtg_api_request.rb
CHANGED
@@ -30,10 +30,10 @@ class MtgApiRequest
|
|
30
30
|
private
|
31
31
|
|
32
32
|
def unfetched_pages
|
33
|
-
@unfetched_pages ||= (1..
|
33
|
+
@unfetched_pages ||= (1..total_page_count).to_a
|
34
34
|
end
|
35
35
|
|
36
|
-
def
|
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 =
|
22
|
+
query_element = query_element(argument)
|
20
23
|
|
21
24
|
if query_options.find { |el| el[:attribute] == query_element[:attribute] }
|
22
|
-
|
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
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
41
|
-
|
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
|
-
|
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,7 +3,7 @@
|
|
3
3
|
require 'json'
|
4
4
|
require_relative 'cards_manager'
|
5
5
|
require_relative 'query'
|
6
|
-
require_relative '
|
6
|
+
require_relative 'cards'
|
7
7
|
|
8
8
|
class QueryMtg
|
9
9
|
def self.call(argv)
|
@@ -34,6 +34,6 @@ class QueryMtg
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def cards_source
|
37
|
-
|
37
|
+
Cards.get(no_cache)
|
38
38
|
end
|
39
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.
|
4
|
+
version: '0.1'
|
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
|