Skip to content

Instantly share code, notes, and snippets.

@eloyesp
Created September 6, 2021 14:56
Show Gist options
  • Save eloyesp/ba7ba24442a00d45d317b33e4f9a7490 to your computer and use it in GitHub Desktop.
Save eloyesp/ba7ba24442a00d45d317b33e4f9a7490 to your computer and use it in GitHub Desktop.
Fetch and process shopify articles
#!/usr/bin/env ruby
require 'httparty'
class Blog
include HTTParty
base_uri "https://#{ ENV['SHOPIFY_STORE'] }.myshopify.com"
headers 'Content-Type' => 'application/json',
'X-Shopify-Access-Token' => ENV['SHOPIFY_TOKEN']
def initialize id
@id = id
end
def articles
return to_enum(__method__) unless block_given?
res = self.class.get "/admin/api/2021-07/blogs/#{ @id }/articles.json"
res['articles'].each { |article| yield article }
while res.headers['link']
next_url = res.headers['link']
.split(', ')
.find { |link| link.match(/rel="next"/) }
.slice(/<(.*)>; /, 1)
res = self.class.get next_url
res['articles'].each { |article| yield article }
end
end
end
# Setup instance
blog = Blog.new $1 # i.e 50212175932
# Iterate
blog.articles.each_with_index do |article, i|
p article['title'], i
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment