Skip to content

Instantly share code, notes, and snippets.

@dpawluk
Created September 15, 2014 21:25
Show Gist options
  • Select an option

  • Save dpawluk/3fed970fb2d8981863fa to your computer and use it in GitHub Desktop.

Select an option

Save dpawluk/3fed970fb2d8981863fa to your computer and use it in GitHub Desktop.
ruby label to articles
require 'net/https'
require 'uri'
require 'json'
require 'pp'
subdomain = 'SUBDOMAIN'
username = 'EMAIL'
password = 'PASSWORD'
category = 'CATEGORY_ID'
label = {:label => {:name => "cake"}}.to_json
domain = 'https://' + subdomain + '.zendesk.com/api/v2'
begin
#get the article IDs
article_ids = Array.new
full_uri = "#{domain}/help_center/categories/#{category}/articles.json"
puts full_uri
uri = URI.parse(full_uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
timestamp = Time.now.strftime('%Y%m%d %H:%M:%S')
request.basic_auth(username, password)
resp = JSON.parse( http.request(request).body )
articles = resp['articles']
next_page = resp["next_page"]
count = resp["count"]
# shove the returned IDs into an array
articles.each do |s|
id = s.values_at("id")
article_ids << id[0]
end
#update those suckers by ID
article_ids.each do |id|
full_uri = "#{domain}/help_center/articles/#{id}/labels.json"
uri = URI.parse(full_uri)
puts uri
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request.add_field 'Content-Type','application/json'
request.body = label
timestamp = Time.now.strftime('%Y%m%d %H:%M:%S')
request.basic_auth(username, password)
puts request
response = http.request(request)
puts response
if response.is_a? Net::HTTPSuccess
puts "Successfully updated article number " + id.to_s + "."
else
puts "Aww shit, there was an error."
end
end
#do it until all articles are updated
#end when the count is 0
end until count == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment