nexosis_api 1.1.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: babec4e434789fc5e16d0aebaf206b09245d46a5
4
- data.tar.gz: 7f7c90dfea83768cc956929d650292fe14c7f63a
3
+ metadata.gz: 4c023f2428d99d8724c7f278b96e71b1faf560c1
4
+ data.tar.gz: 2baabb2e8f32f47f9ddb19f33059f5b6643b5ad9
5
5
  SHA512:
6
- metadata.gz: 01d35de5a51e9d863e909ec0973147a0870b37d62faa1e3fe6f6b956204591dd97f9fe752a561d176a135223d9908a67aafe651397cf3637db39abaa470750a1
7
- data.tar.gz: ab7f428b1ca8d396d79c5c449be6bba536999daac704ceedf3724c83a79e8ce3a21e867fdf1226c3e886a0fdf9c1ce1dc1365b41646fc675b6714b0611538a31
6
+ metadata.gz: 67fa1be6df48ed79569ad3cbc0f698bc381f2a61139f8c39ea7b51f5c27d637c2d59548362521d41fb783fa3e221464d9fccbdd940f889e4c65ac0b3447c6ea1
7
+ data.tar.gz: 92dd19462ab7bda527ff2bc6ea7ffbab4de7cec87cb0d748a828b195467393e2a19955993c47912a1ff133f505f3be7ee4b3ccfa163c088821e7a5de573f13cc
data/lib/nexosis_api.rb CHANGED
@@ -1,12 +1,12 @@
1
- require 'httparty'
2
- require 'nexosis_api/client'
3
-
4
- # The container for the Nexosis API Client
5
- module NexosisApi
6
- class << self
7
- def client options = {}
8
- return @client if defined?(@client)
9
- @client = NexosisApi::Client.new(options)
10
- end
11
- end
1
+ require 'httparty'
2
+ require 'nexosis_api/client'
3
+
4
+ # The container for the Nexosis API Client
5
+ module NexosisApi
6
+ class << self
7
+ def client options = {}
8
+ return @client if defined?(@client)
9
+ @client = NexosisApi::Client.new(options)
10
+ end
11
+ end
12
12
  end
@@ -1,18 +1,18 @@
1
- module NexosisApi
2
- #Class to parse an individual algorithm
3
- class Algorithm
4
- def initialize(algoHash)
5
- algoHash.each do |k,v|
6
- instance_variable_set("@#{k}", v) unless v.nil?
7
- end
8
- end
9
-
10
- # A friendly name for the algorithm
11
- # @return [String]
12
- attr_accessor :name
13
-
14
- # Descriptive explanation of the algorithm implementation
15
- # @return [String]
16
- attr_accessor :description
17
- end
18
- end
1
+ module NexosisApi
2
+ # Class to parse an individual algorithm
3
+ class Algorithm
4
+ def initialize(algo_hash)
5
+ algo_hash.each do |k, v|
6
+ instance_variable_set("@#{k}", v) unless v.nil?
7
+ end
8
+ end
9
+
10
+ # A friendly name for the algorithm
11
+ # @return [String]
12
+ attr_accessor :name
13
+
14
+ # Descriptive explanation of the algorithm implementation
15
+ # @return [String]
16
+ attr_accessor :description
17
+ end
18
+ end
@@ -1,32 +1,32 @@
1
- module NexosisApi
2
- # Class to parse results of an algorithm run
3
- class AlgorithmRun
4
- def initialize(runHash)
5
- runHash.each do |k,v|
6
- if k == "metrics"
7
- @metricSet = Array.new
8
- v.each{|m| @metricSet << NexosisApi::Metric.new(m) unless m.nil?}
9
- instance_variable_set("@#{k}", @metricSet)
10
- elsif k == "links"
11
- @linkSet = Array.new
12
- v.each{|l| @linkSet << NexosisApi::Link.new(l) unless l.nil?}
13
- instance_variable_set("@#{k}", @linkSet)
14
- else
15
- instance_variable_set("@#{k}", NexosisApi::Algorithm.new(v)) unless v.nil?
16
- end
17
- end
18
- end
19
-
20
- # Identifier of algorithm run
21
- # @return [NexosisApi::Algorithm]
22
- attr_accessor :algorithm
23
-
24
- # Set of {NexosisApi::Metric} on algorithm
25
- # @return [Array]
26
- attr_accessor :metrics
27
-
28
- # Relevant hypermedia as {NexosisApi::Link}
29
- # @return [Array]
30
- attr_accessor :links
31
- end
32
- end
1
+ module NexosisApi
2
+ # Class to parse results of an algorithm run
3
+ class AlgorithmRun
4
+ def initialize(run_hash)
5
+ run_hash.each do |k, v|
6
+ if k == 'metrics'
7
+ metric_set = []
8
+ v.each { |m| metric_set << NexosisApi::Metric.new(m) unless m.nil? }
9
+ instance_variable_set("@#{k}", metric_set)
10
+ elsif k == 'links'
11
+ link_set = []
12
+ v.each { |l| link_set << NexosisApi::Link.new(l) unless l.nil? }
13
+ instance_variable_set("@#{k}", link_set)
14
+ else
15
+ instance_variable_set("@#{k}", NexosisApi::Algorithm.new(v)) unless v.nil?
16
+ end
17
+ end
18
+ end
19
+
20
+ # Identifier of algorithm run
21
+ # @return [NexosisApi::Algorithm]
22
+ attr_accessor :algorithm
23
+
24
+ # Set of {NexosisApi::Metric} on algorithm
25
+ # @return [Array]
26
+ attr_accessor :metrics
27
+
28
+ # Relevant hypermedia as {NexosisApi::Link}
29
+ # @return [Array]
30
+ attr_accessor :links
31
+ end
32
+ end
@@ -1,34 +1,34 @@
1
- module NexosisApi
2
- #Class to parse the model data results from a session
3
- class AlgorithmSelection
4
- def initialize(dataHash)
5
- dataHash.each do |k,v|
6
- if k == "champion"
7
- instance_variable_set("@#{k}", NexosisApi::AlgorithmRun.new(v))
8
- elsif k == "contestants"
9
- @contestantArray = Array.new
10
- v.each {|c| @contestantArray << NexosisApi::AlgorithmRun.new(c)}
11
- instance_variable_set("@#{k}", @contestantArray)
12
- else
13
- instance_variable_set("@#{k}", v) unless v.nil?
14
- end
15
- end
16
- end
17
-
18
- # The date on which this algo was selected as champion
19
- # @return [DateTime]
20
- attr_accessor :date
21
-
22
- # The session which selected the algorithm
23
- # @return [String] session identifier
24
- attr_accessor :sessionId
25
-
26
- # The champion algorithm used
27
- # @return [NexosisApi::AlgorithmRun]
28
- attr_accessor :champion
29
-
30
- # All other algorithms which competed
31
- # @return [Array of NexosisApi::AlgorithmRun]
32
- attr_accessor :contestants
33
- end
34
- end
1
+ module NexosisApi
2
+ # Class to parse the model data results from a session
3
+ class AlgorithmSelection
4
+ def initialize(data_hash)
5
+ data_hash.each do |k, v|
6
+ if k == 'champion'
7
+ instance_variable_set("@#{k}", NexosisApi::AlgorithmRun.new(v))
8
+ elsif k == 'contestants'
9
+ contestant_array = []
10
+ v.each { |c| contestant_array << NexosisApi::AlgorithmRun.new(c) }
11
+ instance_variable_set("@#{k}", contestant_array)
12
+ else
13
+ instance_variable_set("@#{k}", v) unless v.nil?
14
+ end
15
+ end
16
+ end
17
+
18
+ # The date on which this algo was selected as champion
19
+ # @return [DateTime]
20
+ attr_accessor :date
21
+
22
+ # The session which selected the algorithm
23
+ # @return [String] session identifier
24
+ attr_accessor :sessionId
25
+
26
+ # The champion algorithm used
27
+ # @return [NexosisApi::AlgorithmRun]
28
+ attr_accessor :champion
29
+
30
+ # All other algorithms which competed
31
+ # @return [Array of NexosisApi::AlgorithmRun]
32
+ attr_accessor :contestants
33
+ end
34
+ end
@@ -1,72 +1,98 @@
1
- require 'nexosis_api/algorithm_run'
2
- require 'nexosis_api/algorithm_selection'
3
- require 'nexosis_api/algorithm'
4
- require 'nexosis_api/column_role'
5
- require 'nexosis_api/column_type'
6
- require 'nexosis_api/dataset_column'
7
- require 'nexosis_api/dataset_data'
8
- require 'nexosis_api/dataset_model'
9
- require 'nexosis_api/dataset_summary'
10
- require 'nexosis_api/http_exception'
11
- require 'nexosis_api/impact_metric'
12
- require 'nexosis_api/imports_response'
13
- require 'nexosis_api/link'
14
- require 'nexosis_api/metric'
15
- require 'nexosis_api/session_response'
16
- require 'nexosis_api/session_result'
17
- require 'nexosis_api/session'
18
- require 'nexosis_api/time_interval'
19
- require 'nexosis_api/client/sessions'
20
- require 'nexosis_api/client/datasets'
21
- require 'nexosis_api/client/imports'
22
-
23
- module NexosisApi
24
- # Primary entry point to working with Nexosis API
25
- class Client
26
- include HTTParty
27
- base_uri 'https://ml.nexosis.com/v1'
28
- include Client::Sessions
29
- include Client::Datasets
30
- include Client::Imports
31
-
32
- def initialize(options = {})
33
- raise ArgumentError, 'api_key was not defined' unless options[:api_key].nil? == false
34
- @api_key = options[:api_key]
35
- self.class.base_uri options[:base_uri] unless options[:base_uri].nil?
36
- @headers = {"api-key" => @api_key, "content-type" => "application/json"}
37
- @options = {:headers => @headers, :format => :json}
38
- end
39
-
40
- # Gets the current account balance.
41
- #
42
- # @return [String] a string with the numeric balance and currency identifier postfix - 10.0 USD
43
- # @example Get account balance
44
- # client.get_account_balance
45
- def get_account_balance()
46
- session_url = '/sessions'
47
- response = self.class.get(session_url,@options)
48
- response.headers["nexosis-account-balance"]
49
- end
50
-
51
- private
52
- def process_csv_to_s csv
53
- content = ""
54
- if(csv.is_a?(CSV))
55
- csv.each do |row|
56
- if(csv.headers.nil?)
57
- #not using row.to_csv because it uses non-compliant '\n' newline
58
- content.concat(row.join(',')).concat("\r\n")
59
- else
60
- content.concat(row.fields.join(',')).concat("\r\n")
61
- end
62
- end
63
- if(csv.headers.nil? == false)
64
- content.prepend(csv.headers.join(',').concat("\r\n")) unless csv.headers.nil?
65
- end
66
- else
67
- content = csv
68
- end
69
- content
70
- end
71
- end
72
- end
1
+ require 'nexosis_api/algorithm_run'
2
+ require 'nexosis_api/algorithm_selection'
3
+ require 'nexosis_api/algorithm'
4
+ require 'nexosis_api/column'
5
+ require 'nexosis_api/column_options'
6
+ require 'nexosis_api/column_role'
7
+ require 'nexosis_api/column_type'
8
+ require 'nexosis_api/dataset_data'
9
+ require 'nexosis_api/dataset_model'
10
+ require 'nexosis_api/dataset_summary'
11
+ require 'nexosis_api/http_exception'
12
+ require 'nexosis_api/impact_metric'
13
+ require 'nexosis_api/imports_response'
14
+ require 'nexosis_api/join'
15
+ require 'nexosis_api/link'
16
+ require 'nexosis_api/metric'
17
+ require 'nexosis_api/session_response'
18
+ require 'nexosis_api/session_result'
19
+ require 'nexosis_api/session'
20
+ require 'nexosis_api/time_interval'
21
+ require 'nexosis_api/view_definition'
22
+ require 'nexosis_api/view_data'
23
+ require 'nexosis_api/client/sessions'
24
+ require 'nexosis_api/client/datasets'
25
+ require 'nexosis_api/client/imports'
26
+ require 'nexosis_api/client/views'
27
+
28
+ module NexosisApi
29
+ # Primary entry point to working with Nexosis API
30
+ class Client
31
+ include HTTParty
32
+ base_uri 'https://ml.nexosis.com/v1'
33
+ include Client::Sessions
34
+ include Client::Datasets
35
+ include Client::Imports
36
+ include Client::Views
37
+
38
+ def initialize(options = {})
39
+ raise ArgumentError, 'api_key was not defined' unless options[:api_key].nil? == false
40
+ @api_key = options[:api_key]
41
+ self.class.base_uri options[:base_uri] unless options[:base_uri].nil?
42
+ @headers = {'api-key' => @api_key, 'Content-Type' => 'application/json'}
43
+ @options = {headers: @headers, format: :json}
44
+ end
45
+
46
+ # Gets the current account balance.
47
+ #
48
+ # @return [String] a string with the numeric balance and currency identifier postfix - 10.0 USD
49
+ # @example Get account balance
50
+ # client.get_account_balance
51
+ def get_account_balance()
52
+ session_url = '/sessions'
53
+ response = self.class.get(session_url,@options)
54
+ response.headers['nexosis-account-balance']
55
+ end
56
+
57
+ private
58
+
59
+ # @private
60
+ def process_csv_to_s csv
61
+ content = ''
62
+ if(csv.is_a?(CSV))
63
+ csv.each do |row|
64
+ if(csv.headers.nil?)
65
+ # not using row.to_csv because it uses non-compliant '\n' newline
66
+ content.concat(row.join(',')).concat("\r\n")
67
+ else
68
+ content.concat(row.fields.join(',')).concat("\r\n")
69
+ end
70
+ end
71
+ if(csv.headers.nil? == false)
72
+ content.prepend(csv.headers.join(',').concat("\r\n")) unless csv.headers.nil?
73
+ end
74
+ else
75
+ content = csv
76
+ end
77
+ content
78
+ end
79
+
80
+ # @private
81
+ def create_query(page_number, page_size, options = {})
82
+ options.store(:page_number, page_number)
83
+ options.store(:page_size, page_size)
84
+ query = {
85
+ 'page' => [page_number],
86
+ 'pageSize' => [page_size]
87
+ }
88
+ query['startDate'] = [options[:start_date].iso8601.gsub(/\+/,'%2B')] unless options[:start_date].nil?
89
+ query['endDate'] = [options[:end_date].iso8601.gsub(/\+/,'%2B')] unless options[:end_date].nil?
90
+ query['include'] = options[:include] unless options[:include].nil?
91
+ query
92
+ end
93
+
94
+ def array_query_normalizer(query_set)
95
+ query_set.map { |key, value| value.map { |v| "#{key}=#{v}" } }.join('&')
96
+ end
97
+ end
98
+ end
@@ -1,162 +1,157 @@
1
- require 'csv'
2
-
3
- module NexosisApi
4
- class Client
5
- # Dataset-based API operations
6
- #
7
- # @see http://docs.nexosis.com/
8
- module Datasets
9
-
10
- # save data in a named dataset
11
- #
12
- # @param dataset_name [String] name to save the dataset
13
- # @param json_data [Hash] parsed json data
14
- # @return [NexosisApi::DatasetSummary] information about your upload
15
- def create_dataset_json(dataset_name, json_data)
16
- create_dataset dataset_name, json_data.to_json, 'application/json'
17
- end
18
-
19
- # save data in a named dataset from csv content
20
- #
21
- # @param dataset_name [String] name to save the dataset
22
- # @param csv [CSV] csv content ready for reading
23
- # @return [NexosisApi::DatasetSummary] information about your upload
24
- def create_dataset_csv(dataset_name, csv)
25
- content = process_csv_to_s csv
26
- create_dataset dataset_name, content, 'text/csv'
27
- end
28
-
29
- # Gets the list of data sets that have been saved to the system, optionally filtering by partial name match.
30
- #
31
- # @param partial_name [String] if provided, all datasets returned will contain this string
32
- # @return [Array of NexosisApi::DatasetSummary] array of datasets found
33
- def list_datasets(partial_name = '')
34
- list_dataset_url = "/data?partialName=#{partial_name.to_s}"
35
- response = self.class.get(list_dataset_url,:headers => @headers)
36
- if(response.success?)
37
- results = []
38
- response.parsed_response["items"].each do |dr|
39
- results << NexosisApi::DatasetSummary.new(dr)
40
- end
41
- results
42
- else
43
- raise HttpException.new("There was a problem listing datasets: #{response.code}.", "listing datasets with partial name #{partial_name}" ,response)
44
- end
45
- end
46
-
47
- # Get the data in the set, with paging, and optional projection.
48
- #
49
- # @param dataset_name [String] name of the dataset for which to retrieve data.
50
- # @param page_number [Integer] zero-based page number of results to retrieve
51
- # @param page_size [Integer] Count of results to retrieve in each page (max 1000).
52
- # @param query_options [Hash] options hash for limiting and projecting returned results
53
- # @note Query Options includes start_date as a DateTime or ISO 8601 compliant string,
54
- # end_date, also as a DateTime or string, and :include as an Array of strings indicating the columns to return.
55
- # The dates can be used independently and are inclusive. Lack of options returns all values within the given page.
56
- def get_dataset(dataset_name, page_number = 0, page_size = 50, query_options = {})
57
- response = get_dataset_internal(dataset_name,page_number,page_size,query_options)
58
- if(response.success?)
59
- NexosisApi::DatasetData.new(response.parsed_response)
60
- else
61
- raise HttpException.new("There was a problem getting the dataset: #{response.code}.", "getting dataset #{dataset_name}" ,response)
62
- end
63
- end
64
-
65
- # Get the data in the set, written to a CSV file, optionally filtering it.
66
- #
67
- # @param dataset_name [String] name of the dataset for which to retrieve data.
68
- # @param page_number [Integer] zero-based page number of results to retrieve
69
- # @param page_size [Integer] Count of results to retrieve in each page (max 1000).
70
- # @param query_options [Hash] options hash for limiting and projecting returned results
71
- # @note Query Options includes start_date as a DateTime or ISO 8601 compliant string,
72
- # end_date, also as a DateTime or string, and :include as an Array of strings indicating the columns to return.
73
- # The dates can be used independently and are inclusive. Lack of options returns all values within the given page.
74
- # @example get page 1 with 20 results each page
75
- # NexosisApi.client.get_dataset_csv('MyDataset', 1, 20, {:include => 'sales'})
76
- def get_dataset_csv(dataset_name, page_number = 0, page_size = 50, query_options = {})
77
- response = get_dataset_internal(dataset_name,page_number,page_size,query_options,"text/csv")
78
- if(response.success?)
79
- response.body
80
- else
81
- raise HttpException.new("There was a problem getting the dataset: #{response.code}.", "getting dataset #{dataset_name}" ,response)
82
- end
83
- end
84
-
85
- # Remove data from a data set or the entire set.
86
- #
87
- # @param dataset_name [String] the name of the dataset from which to remove data
88
- # @param filter_options [Hash] filtering which data to remove
89
- # @note Options: start_date, end_date, cascade_forecast, cascade_sessions, cascade
90
- # - start_date - the first date on which to start removing data
91
- # - end_date - the last date on which to finish removing data
92
- # - cascade_forecast - will cascade deletes to all related forecasts
93
- # - cascade_sessions - will cascade deletes to all related sessions
94
- # - cascade - will cascade deletes to all related forecasts and sessions
95
- # @example - request delete with cascade forecast
96
- # NexosisApi.client.remove_dataset('mydataset', {:cascade_forecast => true})
97
- def remove_dataset(dataset_name, filter_options = {})
98
- raise ArgumentError "dataset_name was not provided and is not optional " unless dataset_name.to_s.empty? == false
99
- dataset_remove_url = "/data/#{dataset_name}"
100
- query = {}
101
- cascade_options = filter_options.each {|k,v|
102
- if(k.to_s.include?('cascade') && v)
103
- if(k.to_s == 'cascade')
104
- #hack here to handle two-keyed api query param
105
- query["cascade"] = "forecast"
106
- query[:cascade] = "session"
107
- elsif(k.to_s == 'cascade_forecast')
108
- query["cascade"] = "forecast"
109
- else
110
- query["cascade"] = "session"
111
- end
112
- end
113
- }
114
-
115
- if(filter_options.empty? == false)
116
- query["startDate"] = filter_options[:start_date].to_s #unless filter_options[:start_date].nil?
117
- query["endDate"] = filter_options[:end_date].to_s #unless filter_options[:end_date].nil?
118
- end
119
- response = self.class.delete(dataset_remove_url, :headers => @headers, :query => query)
120
- if(response.success?)
121
- return
122
- else
123
- raise HttpException.new("There was a problem removing the dataset: #{response.code}.", "removing dataset #{dataset_name}", response)
124
- end
125
- end
126
-
127
- private
128
- def create_dataset dataset_name, content, content_type
129
- raise ArgumentError "dataset_name was not provided and is not optional " unless dataset_name.to_s.empty? == false
130
- dataset_url = "/data/#{dataset_name}"
131
- headers = {"api-key" => @api_key, "content-type" => content_type}
132
- response = self.class.put(dataset_url, {:headers => headers, :body => content})
133
- if(response.success?)
134
- NexosisApi::DatasetSummary.new(response)
135
- else
136
- raise HttpException.new("There was a problem uploading the dataset: #{response.code}.", "uploading dataset #{dataset_name}" ,response)
137
- end
138
- end
139
-
140
- def get_dataset_internal(dataset_name, page_number = 0, page_size = 50, query_options = {}, content_type = "application/json")
141
- raise ArgumentError "page size must be <= 100 items per page" unless page_size <= 100
142
- raise ArgumentError "dataset_name was not provided and is not optional" unless dataset_name.to_s.empty? == false
143
- dataset_url = "/data/#{dataset_name.to_s}"
144
- headers = {"api-key" => @api_key, "Accept" => content_type}
145
- self.class.get(dataset_url, :headers => headers, :query => create_query(page_number, page_size, query_options))
146
- end
147
-
148
- def create_query page_number, page_size, options = {}
149
- options.store(:page_number,page_number)
150
- options.store(:page_size,page_size)
151
- query = {
152
- "startDate" => options[:start_date].to_s,
153
- "endDate" => options[:end_date].to_s,
154
- "page" => options[:page_number],
155
- "pageSize" => options[:page_size],
156
- "include" => options[:include]
157
- }
158
- query
159
- end
160
- end
161
- end
162
- end
1
+ require 'csv'
2
+
3
+ module NexosisApi
4
+ class Client
5
+ # Dataset-based API operations
6
+ #
7
+ # @see http://docs.nexosis.com/
8
+ module Datasets
9
+ # save data in a named dataset
10
+ #
11
+ # @param dataset_name [String] name to save the dataset
12
+ # @param json_data [Hash] parsed json data
13
+ # @return [NexosisApi::DatasetSummary] information about your upload
14
+ # @note input json is to be a hash, do not send a json string via to_json.
15
+ def create_dataset_json(dataset_name, json_data)
16
+ create_dataset dataset_name, json_data.to_json, 'application/json'
17
+ end
18
+
19
+ # save data in a named dataset from csv content
20
+ #
21
+ # @param dataset_name [String] name to save the dataset
22
+ # @param csv [CSV] csv content ready for reading
23
+ # @return [NexosisApi::DatasetSummary] information about your upload
24
+ def create_dataset_csv(dataset_name, csv)
25
+ content = process_csv_to_s csv
26
+ create_dataset dataset_name, content, 'text/csv'
27
+ end
28
+
29
+ # Gets the list of data sets that have been saved to the system, optionally filtering by partial name match.
30
+ #
31
+ # @param partial_name [String] if provided, all datasets returned will contain this string
32
+ # @return [Array of NexosisApi::DatasetSummary] array of datasets found
33
+ def list_datasets(partial_name = '')
34
+ list_dataset_url = "/data?partialName=#{partial_name}"
35
+ response = self.class.get(list_dataset_url, headers: @headers)
36
+ if response.success?
37
+ results = []
38
+ response.parsed_response['items'].each do |dr|
39
+ results << NexosisApi::DatasetSummary.new(dr)
40
+ end
41
+ results
42
+ else
43
+ raise HttpException.new("There was a problem listing datasets: #{response.code}.", "listing datasets with partial name #{partial_name}", response)
44
+ end
45
+ end
46
+
47
+ # Get the data in the set, with paging, and optional projection.
48
+ #
49
+ # @param dataset_name [String] name of the dataset for which to retrieve data.
50
+ # @param page_number [Integer] zero-based page number of results to retrieve
51
+ # @param page_size [Integer] Count of results to retrieve in each page (max 1000).
52
+ # @param query_options [Hash] options hash for limiting and projecting returned results
53
+ # @note Query Options includes start_date as a DateTime or ISO 8601 compliant string,
54
+ # end_date, also as a DateTime or string, and :include as an Array of strings indicating the columns to return.
55
+ # The dates can be used independently and are inclusive. Lack of options returns all values within the given page.
56
+ def get_dataset(dataset_name, page_number = 0, page_size = 50, query_options = {})
57
+ response = get_dataset_internal(dataset_name, page_number, page_size, query_options)
58
+ if response.success?
59
+ NexosisApi::DatasetData.new(response.parsed_response)
60
+ else
61
+ raise HttpException.new("There was a problem getting the dataset: #{response.code}.", "getting dataset #{dataset_name}", response)
62
+ end
63
+ end
64
+
65
+ # Get the data in the set, written to a CSV file, optionally filtering it.
66
+ #
67
+ # @param dataset_name [String] name of the dataset for which to retrieve data.
68
+ # @param page_number [Integer] zero-based page number of results to retrieve
69
+ # @param page_size [Integer] Count of results to retrieve in each page (max 1000).
70
+ # @param query_options [Hash] options hash for limiting and projecting returned results
71
+ # @note Query Options includes start_date as a DateTime or ISO 8601 compliant string,
72
+ # end_date, also as a DateTime or string, and :include as an Array of strings indicating the columns to return.
73
+ # The dates can be used independently and are inclusive. Lack of options returns all values within the given page.
74
+ # @example get page 1 with 20 results each page
75
+ # NexosisApi.client.get_dataset_csv('MyDataset', 1, 20, {:include => 'sales'})
76
+ def get_dataset_csv(dataset_name, page_number = 0, page_size = 50, query_options = {})
77
+ response = get_dataset_internal(dataset_name, page_number, page_size, query_options, 'text/csv')
78
+ if response.success?
79
+ response.body
80
+ else
81
+ raise HttpException.new("There was a problem getting the dataset: #{response.code}.", "getting dataset #{dataset_name}", response)
82
+ end
83
+ end
84
+
85
+ # Remove data from a data set or the entire set.
86
+ #
87
+ # @param dataset_name [String] the name of the dataset from which to remove data
88
+ # @param filter_options [Hash] filtering which data to remove
89
+ # @note Options: start_date, end_date, cascade_forecast, cascade_sessions, cascade
90
+ # - start_date - the first date on which to start removing data
91
+ # - end_date - the last date on which to finish removing data
92
+ # - cascade_forecast - will cascade deletes to all related forecasts
93
+ # - cascade_session - will cascade deletes to all related sessions
94
+ # - cascade_view - will cascade deletes to all related views (any part of join - think twice)
95
+ # - cascade - will cascade deletes to all related forecasts and sessions
96
+ # @example - request delete with cascade forecast
97
+ # NexosisApi.client.remove_dataset('mydataset', {:cascade_forecast => true})
98
+ def remove_dataset(dataset_name, filter_options = {})
99
+ raise ArgumentError 'dataset_name was not provided and is not optional ' if dataset_name.to_s.empty?
100
+ dataset_remove_url = "/data/#{dataset_name}"
101
+ query = {}
102
+ if filter_options.empty? == false
103
+ cascade_query = create_cascade_options(filter_options)
104
+ query['cascade'] = cascade_query unless cascade_query.nil?
105
+ query['startDate'] = [filter_options[:start_date].to_s] unless filter_options[:start_date].nil?
106
+ query['endDate'] = [filter_options[:end_date].to_s] unless filter_options[:end_date].nil?
107
+ end
108
+ #normalizer = proc { |query_set| query_set.map { |key, value| value.map { |v| "#{key}=#{v}" } }.join('&') }
109
+ response = self.class.delete(dataset_remove_url,
110
+ headers: @headers,
111
+ query: query,
112
+ query_string_normalizer: ->(query_map) {array_query_normalizer(query_map)})
113
+ if response.success?
114
+ return
115
+ else
116
+ raise HttpException.new("There was a problem removing the dataset: #{response.code}.", "removing dataset #{dataset_name}", response)
117
+ end
118
+ end
119
+
120
+ private
121
+
122
+ # @private
123
+ def create_dataset(dataset_name, content, content_type)
124
+ raise ArgumentError 'dataset_name was not provided and is not optional ' if dataset_name.to_s.empty?
125
+ dataset_url = "/data/#{dataset_name}"
126
+ headers = { 'api-key' => @api_key, 'Content-Type' => content_type }
127
+ response = self.class.put(dataset_url, headers: headers, body: content)
128
+ if response.success?
129
+ NexosisApi::DatasetSummary.new(response)
130
+ else
131
+ raise HttpException.new("There was a problem uploading the dataset: #{response.code}.", "uploading dataset #{dataset_name}", response)
132
+ end
133
+ end
134
+
135
+ # @private
136
+ def get_dataset_internal(dataset_name, page_number = 0, page_size = 50, query_options = {}, content_type = 'application/json')
137
+ raise ArgumentError 'page size must be <= 100 items per page' unless page_size <= 100
138
+ raise ArgumentError 'dataset_name was not provided and is not optional' unless dataset_name.to_s.empty? == false
139
+ dataset_url = "/data/#{dataset_name}"
140
+ headers = { 'api-key' => @api_key, 'Accept' => content_type }
141
+ self.class.get(dataset_url, headers: headers,
142
+ query: create_query(page_number, page_size, query_options),
143
+ query_string_normalizer: ->(query_map) { array_query_normalizer(query_map) })
144
+ end
145
+
146
+ # @private
147
+ def create_cascade_options(option_hash)
148
+ return nil if option_hash.nil?
149
+ return %w[session view forecast] if option_hash.key?(:cascade)
150
+ options_set = []
151
+ option_hash.each_key { |k| options_set << k.to_s.gsub(/cascade_/, '') if k.to_s.include? 'cascade_' }
152
+ # HACK: required to be backward compatible with incorrect key names
153
+ options_set.map { |s| s.chomp('s') }
154
+ end
155
+ end
156
+ end
157
+ end