Class: Plotlyrb::PlotImage

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

Constant Summary collapse

VALID_IMAGE_FORMATS =
[:png, :svg, :pdf, :eps]

Instance Method Summary collapse

Constructor Details

#initialize(headers) ⇒ PlotImage

Returns a new instance of PlotImage.



9
10
11
12
13
# File 'lib/plotlyrb/plot_image.rb', line 9

def initialize(headers)
  @headers = headers
  @https = Net::HTTP.new(ApiV2::IMAGES.host, ApiV2::IMAGES.port)
  @https.use_ssl = true
end

Instance Method Details

#plot_image(plot_image_spec, image_path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/plotlyrb/plot_image.rb', line 15

def plot_image(plot_image_spec, image_path)
  raise 'No :format key in spec' unless plot_image_spec.has_key?(:format)
  raise 'No :figure key in spec' unless plot_image_spec.has_key?(:figure)
  raise ':data key not found at {:figure => {:data => ...}}' unless plot_image_spec[:figure].has_key?(:data)

  image_format = plot_image_spec[:format]
  raise "Image format #{image_format} not supported" unless VALID_IMAGE_FORMATS.include?(image_format.to_sym)

  request = Net::HTTP::Post.new(ApiV2::IMAGES.path, @headers)
  request.body = plot_image_spec.to_json
  response = @https.request(request)
  IO.binwrite(image_path, response.body)
end