我如何使用正则表达式在我的 Jekyll 插件中构造一个 API 调用?
How can I use regex to construct an API call in my Jekyll plugin?
我正在尝试编写自己的 Jekyll 插件来从自定义标签构建 api 查询。我已经创建了基本的插件和标签,但是我 运行 已经达到了我的编程技能的极限,所以向您寻求帮助。
这是我的自定义标签供参考:
{% card "Arbor Elf | M13" %}
这是我的插件的进度:
module Jekyll
class Scryfall < Liquid::Tag
def initialize(tag_name, text, tokens)
super
@text = text
end
def render(context)
# Store the name of the card, ie "Arbor Elf"
@card_name =
# Store the name of the set, ie "M13"
@card_set =
# Build the query
@query = "https://api.scryfall.com/cards/named?exact=#{@card_name}&set=#{@card_set}"
# Store a specific JSON property
@card_art =
# Finally we render out the result
"<img src='#{@card_art}' title='#{@card_name}' />"
end
end
end
Liquid::Template.register_tag('cards', Jekyll::Scryfall)
作为参考,这里有一个使用上述详细信息的示例查询(将其粘贴到浏览器中以查看返回的响应)
https://api.scryfall.com/cards/named?exact=arbor+elf&set=m13
我在谷歌搜索后的最初尝试是使用正则表达式在 |
处拆分 @text
,如下所示:
@card_name = "#{@text}".split(/| */)
这不太奏效,而是输出了这个:
[“A”, “r”, “b”, “o”, “r”, “ “, “E”, “l”, “f”, “ “, “|”, “ “, “M”, “1”, “3”, “ “]
我也不确定如何在 JSON 响应中访问和存储特定属性。理想情况下,我可以这样做:
@card_art = JSONRESPONSE.image_uri.large
我很清楚我在这里问了很多,但我很乐意尝试让它发挥作用并从中学习。
感谢阅读。
实际上,您的 split
应该可以工作——您只需为其提供正确的正则表达式(您可以直接在 @text
上调用它)。您还需要转义正则表达式中的管道字符,因为管道可能具有特殊含义。您可以使用 rubular.com 来试验正则表达式。
parts = @text.split(/\|/)
# => => ["Arbor Elf ", " M13"]
请注意,它们还包含一些额外的空格,您可以使用 strip
将其删除。
@card_name = parts.first.strip
@card_set = parts.last.strip
这也可能是回答以下问题的好时机:如果用户插入多个管道会发生什么情况?如果他们插入 none 怎么办?您的代码会为此向他们提供有用的错误消息吗?
您还需要在 URL 中转义这些值。如果您的一位用户添加 a card containing a &
character 怎么办?您的 URL 会中断:
https://api.scryfall.com/cards/named?exact=Sword of Dungeons & Dragons&set=und
这看起来像一个带有三个参数的 URL,exact
、set
和 Dragons
。您需要对要包含在 URL:
中的用户输入进行编码
require 'cgi'
query = "https://api.scryfall.com/cards/named?exact=#{CGI.escape(@card_name)}&set=#{CGI.escape(@card_set)}"
# => "https://api.scryfall.com/cards/named?exact=Sword+of+Dungeons+%26+Dragons&set=und"
之后的内容不太清楚,因为您还没有编写代码。尝试使用 Net::HTTP
模块进行调用,然后使用 JSON
模块解析响应。如果您遇到问题,请回到这里提出新问题。
我正在尝试编写自己的 Jekyll 插件来从自定义标签构建 api 查询。我已经创建了基本的插件和标签,但是我 运行 已经达到了我的编程技能的极限,所以向您寻求帮助。
这是我的自定义标签供参考:
{% card "Arbor Elf | M13" %}
这是我的插件的进度:
module Jekyll
class Scryfall < Liquid::Tag
def initialize(tag_name, text, tokens)
super
@text = text
end
def render(context)
# Store the name of the card, ie "Arbor Elf"
@card_name =
# Store the name of the set, ie "M13"
@card_set =
# Build the query
@query = "https://api.scryfall.com/cards/named?exact=#{@card_name}&set=#{@card_set}"
# Store a specific JSON property
@card_art =
# Finally we render out the result
"<img src='#{@card_art}' title='#{@card_name}' />"
end
end
end
Liquid::Template.register_tag('cards', Jekyll::Scryfall)
作为参考,这里有一个使用上述详细信息的示例查询(将其粘贴到浏览器中以查看返回的响应)
https://api.scryfall.com/cards/named?exact=arbor+elf&set=m13
我在谷歌搜索后的最初尝试是使用正则表达式在 |
处拆分 @text
,如下所示:
@card_name = "#{@text}".split(/| */)
这不太奏效,而是输出了这个:
[“A”, “r”, “b”, “o”, “r”, “ “, “E”, “l”, “f”, “ “, “|”, “ “, “M”, “1”, “3”, “ “]
我也不确定如何在 JSON 响应中访问和存储特定属性。理想情况下,我可以这样做:
@card_art = JSONRESPONSE.image_uri.large
我很清楚我在这里问了很多,但我很乐意尝试让它发挥作用并从中学习。
感谢阅读。
实际上,您的 split
应该可以工作——您只需为其提供正确的正则表达式(您可以直接在 @text
上调用它)。您还需要转义正则表达式中的管道字符,因为管道可能具有特殊含义。您可以使用 rubular.com 来试验正则表达式。
parts = @text.split(/\|/)
# => => ["Arbor Elf ", " M13"]
请注意,它们还包含一些额外的空格,您可以使用 strip
将其删除。
@card_name = parts.first.strip
@card_set = parts.last.strip
这也可能是回答以下问题的好时机:如果用户插入多个管道会发生什么情况?如果他们插入 none 怎么办?您的代码会为此向他们提供有用的错误消息吗?
您还需要在 URL 中转义这些值。如果您的一位用户添加 a card containing a &
character 怎么办?您的 URL 会中断:
https://api.scryfall.com/cards/named?exact=Sword of Dungeons & Dragons&set=und
这看起来像一个带有三个参数的 URL,exact
、set
和 Dragons
。您需要对要包含在 URL:
require 'cgi'
query = "https://api.scryfall.com/cards/named?exact=#{CGI.escape(@card_name)}&set=#{CGI.escape(@card_set)}"
# => "https://api.scryfall.com/cards/named?exact=Sword+of+Dungeons+%26+Dragons&set=und"
之后的内容不太清楚,因为您还没有编写代码。尝试使用 Net::HTTP
模块进行调用,然后使用 JSON
模块解析响应。如果您遇到问题,请回到这里提出新问题。