Class: QuickChart

Inherits:
Object
  • Object
show all
Defined in:
lib/quickchart.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, width: 500, height: 300, background_color: '#ffffff', device_pixel_ratio: 1.0, format: 'png', version: nil, key: nil, base_url: 'https://quickchart.io') ⇒ QuickChart

Returns a new instance of QuickChart.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/quickchart.rb', line 16

def initialize(
  config,
  width: 500,
  height: 300,
  background_color: '#ffffff',
  device_pixel_ratio: 1.0,
  format: 'png',
  version: nil,
  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
  @version = version
  @base_url = base_url
end

Instance Attribute Details

#background_colorObject

Returns the value of attribute background_color.



6
7
8
# File 'lib/quickchart.rb', line 6

def background_color
  @background_color
end

#base_urlObject

Returns the value of attribute base_url.



6
7
8
# File 'lib/quickchart.rb', line 6

def base_url
  @base_url
end

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/quickchart.rb', line 6

def config
  @config
end

#device_pixel_ratioObject

Returns the value of attribute device_pixel_ratio.



6
7
8
# File 'lib/quickchart.rb', line 6

def device_pixel_ratio
  @device_pixel_ratio
end

#formatObject

Returns the value of attribute format.



6
7
8
# File 'lib/quickchart.rb', line 6

def format
  @format
end

#heightObject

Returns the value of attribute height.



6
7
8
# File 'lib/quickchart.rb', line 6

def height
  @height
end

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/quickchart.rb', line 6

def key
  @key
end

#versionObject

Returns the value of attribute version.



6
7
8
# File 'lib/quickchart.rb', line 6

def version
  @version
end

#widthObject

Returns the value of attribute width.



6
7
8
# File 'lib/quickchart.rb', line 6

def width
  @width
end

Instance Method Details

#_http_post(path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/quickchart.rb', line 55

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
  params['v'] = @version if @version

  uri = URI("#{@base_url}#{path}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.instance_of? URI::HTTPS
  req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
  req.body = params.to_json

  http.request(req)
end

#get_short_urlObject



80
81
82
83
84
85
# File 'lib/quickchart.rb', line 80

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_urlObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/quickchart.rb', line 38

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
  params['v'] = @version if @version

  encoded = params.to_a.map { |x| "#{x[0]}=#{CGI.escape(x[1].to_s)}" }.join('&')

  "#{@base_url}/chart?#{encoded}"
end

#to_blobObject



87
88
89
90
# File 'lib/quickchart.rb', line 87

def to_blob
  res = _http_post('/chart')
  res.body
end

#to_file(path) ⇒ Object



92
93
94
95
# File 'lib/quickchart.rb', line 92

def to_file(path)
  data = to_blob
  File.open(path, 'wb') { |path| path.puts data }
end