class QuickChart
Attributes
background_color[RW]
base_url[RW]
config[RW]
device_pixel_ratio[RW]
format[RW]
height[RW]
key[RW]
width[RW]
Public Class Methods
new( config, width: 500, height: 300, background_color: '
click to toggle source
# File lib/quickchart.rb, line 15 def initialize( config, width: 500, height: 300, background_color: '#ffffff', device_pixel_ratio: 1.0, format: 'png', key: nil, base_url: 'https://quickchart.io' ) @config = config @width = width @height = height @background_color = background_color @device_pixel_ratio = device_pixel_ratio @format = format @key = key @base_url = base_url end
Public Instance Methods
_http_post(path)
click to toggle source
# File lib/quickchart.rb, line 51 def _http_post(path) spec = Gem.loaded_specs['quickchart'] version = spec ? spec.version.to_s : 'unknown' request_headers = { 'user-agent' => "quickchart-ruby/#{version}" } params = { c: @config.is_a?(String) ? @config : @config.to_json, w: @width, h: @height, bkg: @background_color, devicePixelRatio: @device_pixel_ratio, f: @format } params['key'] = @key if @key uri = URI("#{@base_url}#{path}") https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json') req.body = params.to_json https.request(req) end
get_short_url()
click to toggle source
# File lib/quickchart.rb, line 75 def get_short_url res = _http_post('/chart/create') raise "Request error: #{res.body}" unless (200..300).cover? res.code.to_i JSON.parse(res.body)['url'] end
get_url()
click to toggle source
# File lib/quickchart.rb, line 35 def get_url params = { c: @config.is_a?(String) ? @config : @config.to_json, w: @width, h: @height, bkg: @background_color, devicePixelRatio: @device_pixel_ratio, f: @format } params['key'] = @key if @key encoded = params.to_a.map { |x| "#{x[0]}=#{CGI.escape(x[1].to_s)}" }.join('&') "#{@base_url}/chart?#{encoded}" end
to_blob()
click to toggle source
# File lib/quickchart.rb, line 82 def to_blob res = _http_post('/chart') res.body end
to_file(path)
click to toggle source
# File lib/quickchart.rb, line 87 def to_file(path) data = to_blob File.open(path, 'wb') { |path| path.puts data } end