Module: NexosisApi::Client::Datasets

Included in:
NexosisApi::Client
Defined in:
lib/nexosis_api/client/datasets.rb

Overview

Dataset-based API operations

Instance Method Summary collapse

Instance Method Details

#create_dataset_csv(dataset_name, csv) ⇒ NexosisApi::DatasetSummary

save data in a named dataset from csv content

Parameters:

  • dataset_name (String)

    name to save the dataset

  • csv (CSV)

    csv content ready for reading

Returns:


24
25
26
27
# File 'lib/nexosis_api/client/datasets.rb', line 24

def create_dataset_csv(dataset_name, csv)
    content = process_csv_to_s csv
    create_dataset dataset_name, content, 'text/csv'
end

#create_dataset_json(dataset_name, json_data) ⇒ NexosisApi::DatasetSummary

save data in a named dataset

Parameters:

  • dataset_name (String)

    name to save the dataset

  • json_data (Hash)

    parsed json data

Returns:


15
16
17
# File 'lib/nexosis_api/client/datasets.rb', line 15

def create_dataset_json(dataset_name, json_data)
    create_dataset dataset_name, json_data.to_json, 'application/json'
end

#get_dataset(dataset_name, page_number = 0, page_size = 50, query_options = {}) ⇒ Object

Note:

Query Options includes start_date as a DateTime or ISO 8601 compliant string, end_date, also as a DateTime or string, and :include as an Array of strings indicating the columns to return. The dates can be used independently and are inclusive. Lack of options returns all values within the given page.

Get the data in the set, with paging, and optional projection.

Parameters:

  • dataset_name (String)

    name of the dataset for which to retrieve data.

  • page_number (Integer) (defaults to: 0)

    zero-based page number of results to retrieve

  • page_size (Integer) (defaults to: 50)

    Count of results to retrieve in each page (max 1000).

  • query_options (Hash) (defaults to: {})

    options hash for limiting and projecting returned results


56
57
58
59
60
61
62
63
# File 'lib/nexosis_api/client/datasets.rb', line 56

def get_dataset(dataset_name, page_number = 0, page_size = 50, query_options = {}) 
    response = get_dataset_internal(dataset_name,page_number,page_size,query_options)
    if(response.success?)
        NexosisApi::DatasetData.new(response.parsed_response)
    else
        raise HttpException.new("There was a problem getting the dataset: #{response.code}.", "getting dataset #{dataset_name}" ,response)
    end
end

#get_dataset_csv(dataset_name, page_number = 0, page_size = 50, query_options = {}) ⇒ Object

Note:

Query Options includes start_date as a DateTime or ISO 8601 compliant string, end_date, also as a DateTime or string, and :include as an Array of strings indicating the columns to return. The dates can be used independently and are inclusive. Lack of options returns all values within the given page.

Get the data in the set, written to a CSV file, optionally filtering it.

Examples:

get page 1 with 20 results each page

NexosisApi.client.get_dataset_csv('MyDataset', 1, 20, {:include => 'sales'})

Parameters:

  • dataset_name (String)

    name of the dataset for which to retrieve data.

  • page_number (Integer) (defaults to: 0)

    zero-based page number of results to retrieve

  • page_size (Integer) (defaults to: 50)

    Count of results to retrieve in each page (max 1000).

  • query_options (Hash) (defaults to: {})

    options hash for limiting and projecting returned results


76
77
78
79
80
81
82
83
# File 'lib/nexosis_api/client/datasets.rb', line 76

def get_dataset_csv(dataset_name, page_number = 0, page_size = 50, query_options = {})
    response = get_dataset_internal(dataset_name,page_number,page_size,query_options,"text/csv")
    if(response.success?)
       response.body
    else
        raise HttpException.new("There was a problem getting the dataset: #{response.code}.", "getting dataset #{dataset_name}" ,response)
    end
end

#list_datasets(partial_name = '') ⇒ Array of NexosisApi::DatasetSummary

Gets the list of data sets that have been saved to the system, optionally filtering by partial name match.

Parameters:

  • partial_name (String) (defaults to: '')

    if provided, all datasets returned will contain this string

Returns:


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nexosis_api/client/datasets.rb', line 33

def list_datasets(partial_name = '')
    list_dataset_url = "/data?partialName=#{partial_name.to_s}"
    response = self.class.get(list_dataset_url,:headers => @headers)
    if(response.success?)
        results = []
        response.parsed_response["items"].each do |dr|
            results << NexosisApi::DatasetSummary.new(dr)
        end
        results
    else
        raise HttpException.new("There was a problem listing datasets: #{response.code}.", "listing datasets with partial name #{partial_name}" ,response)
    end
end

#remove_dataset(dataset_name, filter_options = {}) ⇒ Object

Note:

Options: start_date, end_date, cascade_forecast, cascade_sessions, cascade

  • start_date - the first date on which to start removing data

  • end_date - the last date on which to finish removing data

  • cascade_forecast - will cascade deletes to all related forecasts

  • cascade_sessions - will cascade deletes to all related sessions

  • cascade - will cascade deletes to all related forecasts and sessions

Remove data from a data set or the entire set.

Examples:

  • request delete with cascade forecast

NexosisApi.client.remove_dataset('mydataset', {:cascade_forecast => true})

Parameters:

  • dataset_name (String)

    the name of the dataset from which to remove data

  • filter_options (Hash) (defaults to: {})

    filtering which data to remove


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/nexosis_api/client/datasets.rb', line 97

def remove_dataset(dataset_name, filter_options = {})
 raise ArgumentError "dataset_name was not provided and is not optional " unless dataset_name.to_s.empty? == false
    dataset_remove_url = "/data/#{dataset_name}"
    query = {}
    cascade_options = filter_options.each {|k,v| 
        if(k.to_s.include?('cascade') && v)
            if(k.to_s == 'cascade')
                #hack here to handle two-keyed api query param
                query["cascade"] = "forecast"
                query[:cascade] = "session"
            elsif(k.to_s == 'cascade_forecast')
                 query["cascade"] = "forecast"
            else
                 query["cascade"] = "session"
            end
        end
    }
    
    if(filter_options.empty? == false)
        query["startDate"] = filter_options[:start_date].to_s #unless filter_options[:start_date].nil?
        query["endDate"] = filter_options[:end_date].to_s #unless filter_options[:end_date].nil?
    end
    response  = self.class.delete(dataset_remove_url, :headers => @headers, :query => query)
    if(response.success?)
        return
    else
        raise HttpException.new("There was a problem removing the dataset: #{response.code}.", "removing dataset #{dataset_name}", response)
    end
end