Module: JekyllMTG::Utils

Included in:
LinkCard
Defined in:
lib/utils.rb

Overview

Provides shared methods

Constant Summary collapse

DEFAULT_REQUEST_FORMAT =
'json'
SCRYFALL_BASE_URI =
'https://api.scryfall.com/'
SCRYFALL_FUZZY_PATH =
'/cards/named'

Instance Method Summary collapse

Instance Method Details

#fetch_card(parsed_card_info) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/utils.rb', line 22

def fetch_card(parsed_card_info)
  uri = URI(SCRYFALL_BASE_URI)
  uri.path = SCRYFALL_FUZZY_PATH
  params = { fuzzy: parsed_card_info[:card_name], set: parsed_card_info[:set_code], format: DEFAULT_REQUEST_FORMAT }
  uri.query = URI.encode_www_form(params)
  headers = { 'Accept' => 'application/json', 'User-Agent' => "JekyllMTG/#{JekyllMTG::VERSION}" }
  response = Net::HTTP.get(uri, headers)
  JSON.parse(response)
end

#parse_card_info(card_info) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/utils.rb', line 12

def parse_card_info(card_info)
  args = {}

  card_info.scan(/(\w+)="([^\"]*)"/) do |key, value|
    args[key.to_sym] = value # Store as symbols for convenience
  end

  { card_name: args[:name], set_code: args[:set], contents: args[:contents] }
end