Skip to content

Instantly share code, notes, and snippets.

@arikfr
Last active March 15, 2019 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arikfr/57a3c719182d53422320860d183acb8e to your computer and use it in GitHub Desktop.
Save arikfr/57a3c719182d53422320860d183acb8e to your computer and use it in GitHub Desktop.
Example of for using Redash API in Ruby to update the schedule
require 'httparty'
class Redash
include HTTParty
def initialize(slug, api_key)
@base_uri = "https://app.redash.io/#{slug}"
@headers = {
'Authorization': "Key #{api_key}"
}
end
def get(url, options={})
full_uri = "#{@base_uri}/#{url}"
options.merge!({headers: @headers, 'Content-Type': 'application/json'})
JSON.parse(self.class.get(full_uri, options).body)
end
def post(url, options={})
full_uri = "#{@base_uri}/#{url}"
options.merge!({headers: @headers, 'Content-Type': 'application/json'})
JSON.parse(self.class.post(full_uri, options).body)
end
def paginate(request)
page = 1
page_size = 100
items = []
loop do
response = get(request, :query => {page: page, page_size: page_size})
items.concat(response['results'])
page += 1
break if response['page'] * response['page_size'] >= response['count']
end
items
end
def queries(page=1, page_size=25)
get('api/queries', :query => {page: page, page_size: page_size})
end
def query_by_id(id)
get("api/queries/#{id}")
end
def update_query(id, body)
post("api/queries/#{id}", :body => JSON.dump(body))
end
end
redash = Redash.new('your-slug', 'your-api-key')
query_id = 123456
query = redash.query_by_id(query_id)
schedule = query['schedule']
schedule.merge!({"until": "2019-03-17"})
puts redash.update_query(query_id, :schedule => schedule)['schedule']
# get all queries:
redash.paginate('api/queries')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment