首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何验证Ruby中对应用程序块的支持?

如何验证Ruby中对应用程序块的支持?
EN

Stack Overflow用户
提问于 2021-08-11 12:51:18
回答 1查看 101关注 0票数 0

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

EN

回答 1

Stack Overflow用户

发布于 2021-08-12 08:46:29

好吧,我自己做的:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68742401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档