Module: NexosisApi::Client::Views

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

Overview

Views-based API operations

See Also:

Since:

  • 1.2.0

Instance Method Summary collapse

Instance Method Details

#create_view(view_name, dataset_name, right_datasource_name) ⇒ NexosisApi::ViewDefinition

Note:

@view_name Must be unique within your organization

Create a new view or update an existing one by name

Parameters:

  • view_name (String)

    the name of the view to create or update.

  • dataset_name (String)

    the unique name of a data that is one source of this view.

  • right_datasource_name (String)

    the additional data source to join in this view.

Returns:

Raises:

Since:

  • 1.2.0



37
38
39
40
41
42
43
44
45
46
# File 'lib/nexosis_api/client/views.rb', line 37

def create_view(view_name, dataset_name, right_datasource_name)
  raise ArgumentError, 'view_name was not provided and is not optional' unless view_name.to_s.empty? == false
  raise ArgumentError, 'dataset_name was not provided and is not optional' unless dataset_name.to_s.empty? == false
  raise ArgumentError, 'right_datasource_name was not provided and is not optional' unless right_datasource_name.to_s.empty? == false
  view_definition = NexosisApi::ViewDefinition.new('viewName' => view_name)
  view_definition.dataset_name = dataset_name
  join = NexosisApi::Join.new('dataSet' => { 'name' => right_datasource_name })
  view_definition.joins = [join]
  create_view_by_def(view_definition)
end

#create_view_by_def(view_definition) ⇒ NexosisApi::ViewDefinition

Create or update a view based on a view definition object

Parameters:

Returns:

Raises:

Since:

  • 1.2.0



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nexosis_api/client/views.rb', line 52

def create_view_by_def(view_definition)
  view_name = view_definition.view_name
  raise ArgumentError, 'view_name was not provided and is not optional' unless view_name.to_s.empty? == false
  url = "/views/#{view_name}"
  response = self.class.put(url, headers: @headers, body: view_definition.to_json)
  if response.success?
    return NexosisApi::ViewDefinition.new(response.parsed_response)
  else
    raise NexosisApi::HttpException.new('Could not create the requested view',
                                        "Attempting to create view named #{view_name}",
                                        response)
  end
end

#get_view(view_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.

Note:
  • the results include any transformations or imputations required to prepare the data for a session

Get the processed data from the view definition

Parameters:

  • view_name (String)

    the unique name of the view 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

Raises:

Since:

  • 1.2.0



94
95
96
97
98
99
100
101
102
# File 'lib/nexosis_api/client/views.rb', line 94

def get_view(view_name, page_number = 0, page_size = 50, query_options = {})
  raise ArgumentError, 'view_name was not provided and is not optional' unless view_name.to_s.empty? == false
  url = "/views/#{view_name}"
  response = self.class.get(url, headers: @headers,
                                 query: create_query(page_number, page_size, query_options),
                                 query_string_normalizer: ->(query_map) { array_query_normalizer(query_map) } )
  raise NexosisApi::HttpException.new('Could not retrieve data for the given view', "Requesting data for view #{view_name}", response) unless response.success?
  NexosisApi::ViewData.new(response.parsed_response)
end

#list_views(partial_name = '', dataset_name = '', page = 0, page_size = 50) ⇒ NexosisApi::PagedArray of NexosisApi::ViewDefinition

List all existing view defintions, optionally limited by partial name or participating data sources

Parameters:

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

    optionally limit results by view name

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

    optionally limit results by dataset used in definition

  • page (Integer) (defaults to: 0)

    optionally get results by non-zero page - defaults to 0

  • page_size (Integer) (defaults to: 50)

    optionally limit page size - defaults to 50 (max: 1000)

Returns:

Raises:

Since:

  • 1.2.0



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/nexosis_api/client/views.rb', line 16

def list_views(partial_name = '', dataset_name = '', page = 0, page_size = 50)
  url = '/views'
  query = {
    'page': page,
    'pageSize': page_size
  }
  query.store 'partialName', partial_name if partial_name.empty? == false
  query.store 'dataSetName', dataset_name if dataset_name.empty? == false
  response = self.class.get(url, headers: @headers, query: query)
  raise NexosisApi::HttpException('Could not retrieve list of views.', 'attempting list of views', response) unless response.success?
  NexosisApi::PagedArray.new(response.parsed_response, response.parsed_response['items']
                         .map { |definition| NexosisApi::ViewDefinition.new(definition) })
end

#remove_view(view_name, cascade = nil) ⇒ void

This method returns an undefined value.

Deletes the named view and optionally all sessions created from it

Parameters:

  • view_name (String)

    the view to remove

  • cascade (String) (defaults to: nil)

    indicate any non-nil to remove associated sessions

Raises:

Since:

  • 1.2.0



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nexosis_api/client/views.rb', line 71

def remove_view(view_name, cascade = nil)
  raise ArgumentError, 'view_name was not provided and is not optional' unless view_name.to_s.empty? == false
  url = "/views/#{view_name}"
  query = 'cascade=sessions' unless cascade.nil?
  response = self.class.delete(url, headers: @headers, query: query)
  unless response.success?
    raise NexosisApi::HttpException.new('Could not delete view',
                                        "Attempting to delete view #{view_name}",
                                        response)
  end
end