plotlyrb 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 770ad612218a702db6da97964e0b08c5227291e5
4
- data.tar.gz: 571aa03563f0b459f969a9f20c5e189144894639
3
+ metadata.gz: a3294977f017a389f97c281ab13cc93b95878248
4
+ data.tar.gz: 60eee1cf956a7b94135cbcd5670dedb237239075
5
5
  SHA512:
6
- metadata.gz: 2016eea005ced2e9f7139ba2f5491d607e873a02332792a750a3f95da9c3aee8290cfaf967b396594444b67e6325461ccec9f5ea325b9d34134cf65fe6bff4e3
7
- data.tar.gz: e1ee27d011635f697977b7e6cfbc47e7ebedfe528530150eb7f224f553ee269845bd479503d5f67968374dde76a5ab3bb09c321707d84fc82c9594ed683d0847
6
+ metadata.gz: c259c1f081ecad6cd832f66bf73b3745bcaeea56bab362d4af2f9b4ab5cb17399682c21b180501bda4d391ad5d0a060f16d3d7b82836819037f65ad79dd6387f
7
+ data.tar.gz: b645541f67a799b2e8810a8934dc083aa352d58c50116a0a7a62854c811d8921015bbd5512cda32e4014e242b466b5fbe891de93ef49876da91749b9fe908431
data/README.md CHANGED
@@ -2,7 +2,13 @@
2
2
 
3
3
  Use [plotly](https://plot.ly) to graph your data.
4
4
 
5
- Currently only supports `plot_image`, which uses the REST API to generate an image for your plot. See [the python reference](https://plot.ly/python/reference/) for details on what you can plot.
5
+ Currently only supports `plot_image`, `create_grid`, and `create_plot_from_grid`.
6
+
7
+ - `plot_image` generates an image for your plot
8
+ - `create_grid` creates grid data accessible through the web interface
9
+ - `create_plot_from_grid` creates a plot based on the JSON representation of a grid
10
+
11
+ See [the python reference](https://plot.ly/python/reference/) for details on what you can plot.
6
12
 
7
13
  ## Installation
8
14
 
@@ -23,18 +29,24 @@ Or install it yourself as:
23
29
  ## Usage
24
30
 
25
31
  ```ruby
32
+ require 'plotlyrb'
33
+
26
34
  data = {
27
35
  :x => ['2013-10-04 22:23:00', '2013-11-04 22:23:00', '2013-12-04 22:23:00'],
28
36
  :y => [1, 3, 6],
29
- :type => 'scatter'
37
+ :type => 'scatter',
38
+ :mode => 'markers',
30
39
  }
31
40
 
32
- layout = { :xaxis => { :title => 'times' },
33
- :yaxis => { :title => 'bigfoot sightings', :range => [0, 7] },
41
+ layout = {
42
+ :xaxis => { :title => 'times' },
43
+ :yaxis => { :title => 'bigfoot sightings', :range => [0, 7] },
34
44
  }
35
45
 
36
- plotly = Plotlyrb::PlotImage.new('my-plotly-username', 'my-plotly-api-key')
37
- plotly.plot_image(data, 'plot.png', :png, layout)
46
+ plotly = Plotlyrb::ApiV2.auth_plotly('username', 'super secret API key')
47
+
48
+ # NOTE: data argument must be an array of traces
49
+ plotly.plot_image([data], '/path/to/plot.svg', :svg, layout)
38
50
  ```
39
51
 
40
52
  ## TODO
data/Rakefile CHANGED
@@ -3,6 +3,6 @@ require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new do |t|
5
5
  t.libs << "test"
6
- t.test_files = FileList['test/*test*.rb']
6
+ t.test_files = FileList['test/**/*test*.rb']
7
7
  t.verbose = true
8
8
  end
@@ -8,7 +8,14 @@ module Plotlyrb
8
8
  HOSTNAME = 'api.plot.ly'
9
9
  BASE_PATH = 'v2'
10
10
  BASE_URI = "#{PROTOCOL}://#{HOSTNAME}/#{BASE_PATH}"
11
- IMAGES = URI.parse("#{BASE_URI}/images")
11
+
12
+ def self.mk_endpoint(path)
13
+ URI.parse("#{BASE_URI}/#{path}")
14
+ end
15
+ IMAGES = mk_endpoint('images')
16
+ GRIDS = mk_endpoint('grids')
17
+ PLOTS = mk_endpoint('plots')
18
+
12
19
  COMMON_HEADERS = {
13
20
  'plotly-client-platform' => "Ruby #{Plotlyrb::VERSION}",
14
21
  'content-type' => 'application/json'
@@ -24,4 +31,4 @@ module Plotlyrb
24
31
  Plotly.new(headers_hash)
25
32
  end
26
33
  end
27
- end
34
+ end
@@ -0,0 +1,21 @@
1
+ require 'net/https'
2
+ require 'json'
3
+
4
+ module Plotlyrb
5
+ class Grid
6
+ def initialize(headers)
7
+ @headers = headers
8
+ @https = Net::HTTP.new(ApiV2::GRIDS.host, ApiV2::GRIDS.port)
9
+ @https.use_ssl = true
10
+ end
11
+
12
+ # data is a hash that mirrors the format of the data hash in the API
13
+ # https://api.plot.ly/v2/grids#create
14
+ def create(data)
15
+ payload = {:data => data}.to_json
16
+ request = Net::HTTP::Post.new(ApiV2::GRIDS.path, @headers)
17
+ request.body = payload
18
+ Response.from_http_response(@https.request(request))
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,48 @@
1
+ require 'net/https'
2
+ require 'json'
3
+
4
+ module Plotlyrb
5
+ class Plot
6
+
7
+ def initialize(headers)
8
+ @headers = headers
9
+ @https = Net::HTTP.new(ApiV2::PLOTS.host, ApiV2::PLOTS.port)
10
+ @https.use_ssl = true
11
+ end
12
+
13
+ # Takes a list of data hashes (each element is a trace?), response from grid creation, x and y
14
+ # column names (must appear in grid), and a layout. Extracts the column references from the
15
+ # grid response to populate the xsrc # and ysrc fields in data.
16
+ # See https://api.plot.ly/v2/plots#create
17
+ def create_from_grid(data, grid_json, layout = {})
18
+ grid_response_body = JSON.parse(grid_json)
19
+ begin
20
+ payload_data = data.map { |d| self.class.replace_column_names_with_uids(grid_response_body, d) }
21
+ rescue => e
22
+ return Response.fail(e.to_s)
23
+ else
24
+ payload = { :figure => { :data => payload_data, :layout => layout } }.to_json
25
+ request = Net::HTTP::Post.new(ApiV2::PLOTS.path, @headers)
26
+ request.body = payload
27
+ Response.from_http_response(@https.request(request))
28
+ end
29
+ end
30
+
31
+ def self.replace_column_names_with_uids(response_body, trace_data)
32
+ raise('No :xsrc key in trace data') unless trace_data.has_key?(:xsrc)
33
+ raise('No :ysrc key in trace data') unless trace_data.has_key?(:ysrc)
34
+ x_uid = column_uid_from_name(response_body, trace_data[:xsrc])
35
+ y_uid = column_uid_from_name(response_body, trace_data[:ysrc])
36
+ trace_data.merge({:xsrc => x_uid, :ysrc => y_uid})
37
+ end
38
+
39
+ def self.column_uid_from_name(response_body, name)
40
+ fid = response_body.fetch('file').fetch('fid')
41
+ cols = response_body.fetch('file').fetch('cols')
42
+ maybe_col = cols.select { |c| c.fetch('name') == name.to_s}
43
+ raise("Unable to find column named '#{name.to_s}' in response") if maybe_col.size != 1
44
+ uid = maybe_col.first.fetch('uid')
45
+ "#{fid}:#{uid}"
46
+ end
47
+ end
48
+ end
@@ -7,6 +7,14 @@ module Plotlyrb
7
7
  def plot_image(*args)
8
8
  PlotImage.new(@headers).plot_image(*args)
9
9
  end
10
+
11
+ def create_grid(*args)
12
+ Grid.new(@headers).create(*args)
13
+ end
14
+
15
+ def create_plot_from_grid(*args)
16
+ Plot.new(@headers).create_from_grid(*args)
17
+ end
10
18
  end
11
19
  end
12
20
 
@@ -0,0 +1,38 @@
1
+ module Plotlyrb
2
+ class Response < Struct.new(:success, :body, :msg, :errors)
3
+ def self.from_http_response(rsp)
4
+ case rsp.code
5
+ when '200'
6
+ new(true, rsp.body, '200 - OK', [])
7
+ when '201'
8
+ new(true, rsp.body, '201 - CREATED', [])
9
+ when '400'
10
+ new(false, rsp.body, '400 - BAD REQUEST', get_errors(rsp.body))
11
+ when '404'
12
+ new(false, rsp.body, '404 - NOT FOUND', ['Appears we got an endpoint wrong'])
13
+ else
14
+ new(false, rsp.body, "#{rsp.code} - UNHANDLED", ['Unhandled error'])
15
+ end
16
+ end
17
+
18
+ def self.fail(msg)
19
+ new(false, '', msg, [msg])
20
+ end
21
+
22
+ # s is a Net::HTTP::Response body we expect to contain JSON map with a list of errors
23
+ def self.get_errors(s)
24
+ msg_key = 'message'
25
+ h = JSON.parse(s)
26
+ es = h['errors']
27
+ if es.nil? || !es.is_a?(Array) || !all_have_key?(es, msg_key)
28
+ return ['Failed to parse plotly error response - check raw body']
29
+ end
30
+
31
+ es.map { |e| e.fetch(msg_key) }
32
+ end
33
+
34
+ def self.all_have_key?(hs, key)
35
+ hs.all? { |h| h.has_key?(key) }
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module Plotlyrb
2
- VERSION = '0.1.5'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/plotlyrb.rb CHANGED
@@ -2,5 +2,8 @@ require 'backports' if RUBY_VERSION < '1.9.3'
2
2
 
3
3
  require_relative 'plotlyrb/version'
4
4
  require_relative 'plotlyrb/api_v2'
5
+ require_relative 'plotlyrb/response'
5
6
  require_relative 'plotlyrb/plot_image'
7
+ require_relative 'plotlyrb/grid'
8
+ require_relative 'plotlyrb/plot'
6
9
  require_relative 'plotlyrb/plotly'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plotlyrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Veitch Lister Consulting
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
11
 
12
- date: 2016-01-25 00:00:00 Z
12
+ date: 2016-10-27 00:00:00 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -54,8 +54,11 @@ files:
54
54
  - ext/mkrf_conf.rb
55
55
  - lib/plotlyrb.rb
56
56
  - lib/plotlyrb/api_v2.rb
57
+ - lib/plotlyrb/grid.rb
58
+ - lib/plotlyrb/plot.rb
57
59
  - lib/plotlyrb/plot_image.rb
58
60
  - lib/plotlyrb/plotly.rb
61
+ - lib/plotlyrb/response.rb
59
62
  - lib/plotlyrb/version.rb
60
63
  - plotlyrb.gemspec
61
64
  homepage: https://github.com/vlc/plotlyrb