【问题标题】:How do I verify support for app blocks in Ruby?如何在 Ruby 中验证对应用程序块的支持?
【发布时间】:2021-08-11 12:51:18
【问题描述】:

在本指南中:https://shopify.dev/apps/online-store/verify-support 有一个 Node.js 示例。是否有任何 Ruby 示例可以做同样的事情?

【问题讨论】:

  • 不确定我是否理解这个问题,因为“应用程序块”似乎与前端主题有关,这更像是javascripthtmlcss 关注而不是@987654325 @关注。

标签: ruby shopify


【解决方案1】:

好的,我自己做的:

themes = ShopifyAPI::Theme.all
publishedTheme = themes.find {|t| t.role == 'main'}
assets = ShopifyAPI::Asset.find(:all, params: {theme_id: publishedTheme.id})
APP_BLOCK_TEMPLATES = ['product', 'collection', 'index']
templateJSONFiles = assets.filter {|file| APP_BLOCK_TEMPLATES.any? {|t| file.key == "templates/#{t}.json"}}
if templateJSONFiles.size === APP_BLOCK_TEMPLATES.size
  puts 'All desired templates support sections everywhere!'
elsif templateJSONFiles.size > 0 
  puts 'Only some of the desired templates support sections everywhere.'
end

templateMainSections = templateJSONFiles.map do |tmp|
    a = ShopifyAPI::Asset.find(tmp.key, params: {theme_id: publishedTheme.id})
    json = JSON.parse(a.value)
    
    main = json['sections'].find {|k, v| k == 'main' || v['type'].start_with?('main-')}
    if main
        assets.find {|file| file.key == "sections/#{main[1]['type']}.liquid"}
    else
        nil
    end
end.compact

sectionsWithAppBlock = templateMainSections.map do |file|
    acceptsAppBlock = false
    asset = ShopifyAPI::Asset.find(file.key, params: {theme_id: publishedTheme.id})
    match = asset.value.match(/\{\%\s+schema\s+\%\}([\s\S]*?)\{\%\s+endschema\s+\%\}/m)
    schema = JSON.parse(match[1]);
    if (schema && schema['blocks']) 
        acceptsAppBlock = schema['blocks'].any? {|b| b['type'] == '@app'};
    end

    acceptsAppBlock ? file : nil
end.compact

if sectionsWithAppBlock.size > 0 && templateJSONFiles.size == sectionsWithAppBlock.size
  puts 'All desired templates have main sections that support app blocks!'
elsif sectionsWithAppBlock.size > 0
  puts 'Only some of the desired templates support app blocks.'
else
  puts 'None of the desired templates support app blocks'
end

【讨论】:

  • 太棒了!!谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-17
  • 1970-01-01
相关资源
最近更新 更多