class GoodData::Label

Public Instance Methods

attribute() click to toggle source

Gives an attribute of current label @return [GoodData::Attibute]

# File lib/gooddata/models/metadata/label.rb, line 104
def attribute
  project.attributes(content['formOf'])
end
attribute_uri() click to toggle source

Gives an attribute url of current label. Useful for mass actions when it does not introduce HTTP call. @return [GoodData::Attibute]

# File lib/gooddata/models/metadata/label.rb, line 110
def attribute_uri
  content['formOf']
end
find_element_value(element_id) click to toggle source

For an element id find values (titles) for this label. Element id can be given as both number id or URI as a string beginning with / @param [Object] element_id Element identifier either Number or a uri as a String @return [String] value of the element if found

# File lib/gooddata/models/metadata/label.rb, line 31
def find_element_value(element_id)
  element_id = element_id.is_a?(String) ? element_id.match(/\?id=(\d+)/)[1] : element_id
  uri = links['elements']
  result = client.get(uri + "/?id=#{element_id}")
  items = result['attributeElements']['elements']
  if items.empty?
    fail(AttributeElementNotFound, element_id)
  else
    items.first['title']
  end
end
find_value_uri(value) click to toggle source

Finds an attribute element URI for given value. This URI can be used by find_element_value to find the original value again @param [String] value value of an label you are looking for @return [String]

# File lib/gooddata/models/metadata/label.rb, line 18
def find_value_uri(value)
  results = get_valid_elements(filter: value)
  items = results['validElements']['items']
  if items.empty?
    fail(AttributeElementNotFound, value)
  else
    items.first['element']['uri']
  end
end
get_valid_elements(*args) click to toggle source

Gets valid elements of a label for a specific paging (:offset and :limit) or get validElements of a specific value (:filter). In the case filter a specific value, because the API /validElements only filter by partial match, we need to filter again at client side for exact match. @return [Array] Results

# File lib/gooddata/models/metadata/label.rb, line 46
def get_valid_elements(*args)
  if args && !args.empty? && args.first[:filter]
    params = args.first
    params[:limit] = 100_000
    results, = valid_elements params
    results['validElements']['items'] = results['validElements']['items'].select do |i|
      i['element']['title'] == params[:filter]
    end
  else
    results, = valid_elements(*args)
  end
  results
end
value?(value) click to toggle source

Finds if a label has an attribute element for given value. @param [String] value value of an label you are looking for @return [Boolean]

# File lib/gooddata/models/metadata/label.rb, line 63
def value?(value)
  find_value_uri(value)
  true
rescue AttributeElementNotFound
  false
end
values(options = {}) click to toggle source

Returns all values for this label. This is for inspection purposes only since obviously there can be huge number of elements. @param [Hash] options the options to pass to the value list @option options [Number] :limit limits the number of values to certain number. Default is 100 @return [Array]

# File lib/gooddata/models/metadata/label.rb, line 74
def values(options = {})
  all_values = []
  offset = options[:offset] || 0
  page_limit = options[:limit] || 100
  loop do
    results = get_valid_elements(limit: page_limit, offset: offset)

    elements = results['validElements']
    elements['items'].map do |el|
      v = el['element']
      all_values << {
        :value => v['title'],
        :uri => v['uri']
      }
    end
    break if elements['items'].count < page_limit

    offset += page_limit
  end
  all_values
end
values_count() click to toggle source
# File lib/gooddata/models/metadata/label.rb, line 96
def values_count
  results, = valid_elements
  count = GoodData::Helpers.get_path(results, %w(validElements paging total))
  count && count.to_i
end

Private Instance Methods

valid_elements(url_or_params = {}, request_payload = {}) click to toggle source
# File lib/gooddata/models/metadata/label.rb, line 116
def valid_elements(url_or_params = {}, request_payload = {})
  final_url = url_or_params

  if url_or_params.is_a?(Hash)
    default_params = {
      limit: 1,
      offset: 0,
      order: 'asc'
    }
    params = default_params.merge(url_or_params)
    query_params = params.map { |x, v| "#{x}=#{CGI.escape(v.to_s)}" }.reduce { |acc, elem| "#{acc}&#{elem}" }
    final_url = "#{uri}/validElements?#{query_params}"
  end

  results = client.post(final_url, 'validElementsRequest' => request_payload)

  # Implementation of polling is based on
  # https://opengrok.intgdc.com/source/xref/gdc-backend/src/test/java/com/gooddata/service/dao/ValidElementsDaoTest.java
  status_url = results['uri']
  if status_url
    results = client.poll_on_response(status_url) do |body|
      status = body['taskState'] && body['taskState']['status']
      status == 'RUNNING' || status == 'PREPARED' || body['uri']
    end
  end

  [results, params]
end