Class: GoodData::Label

Inherits:
MdObject show all
Includes:
Mixin::IsLabel
Defined in:
lib/gooddata/models/metadata/label.rb

Constant Summary

Constants inherited from MdObject

MdObject::IDENTIFIERS_CFG, MdObject::MD_OBJ_CTG

Constants included from Mixin::MdIdToUri

Mixin::MdIdToUri::IDENTIFIERS_CFG

Constants included from Mixin::MdObjectIndexer

Mixin::MdObjectIndexer::MD_OBJ_CTG

Instance Attribute Summary

Attributes inherited from Rest::Object

#client, #json, #project

Instance Method Summary collapse

Methods included from Mixin::IsLabel

#label?

Methods inherited from MdObject

#==, #add_tag, #browser_uri, #delete, #deprecated, #deprecated=, find_replaceable_values, #initialize, #listed?, #project, #reload!, #remove_tag, replace, #replace, #replace!, replace_bracketed, replace_quoted, #save, #save_as, #tag_set, #unlisted, #unlisted=, #validate

Methods included from Mixin::MdIdToUri

#identifier_to_uri

Methods included from Mixin::MdObjectIndexer

#[]

Methods included from Mixin::MdObjectQuery

#all, #dependency, #dependency?, #query, #usedby, #usedby?, #using, #using?

Methods included from Mixin::MdFinders

#find_by_identifier, #find_by_tag, #find_by_title, #find_first_by_identifier, #find_first_by_title

Methods included from Mixin::MdObjId

#uri_obj_id

Methods included from Mixin::MdGrantees

#change_permission, #grant, #grantees, #revoke

Methods included from Mixin::MdRelations

#dependency, #dependency?, #usedby, #usedby?, #using, #using?

Methods included from Mixin::ObjId

#obj_id

Methods included from Mixin::Links

#links

Methods inherited from Rest::Resource

#initialize

Methods inherited from Rest::Object

client, default_client, #initialize, #saved?

Methods included from Mixin::DataPropertyReader

#data_property_reader

Methods included from Mixin::DataPropertyWriter

#data_property_writer

Methods included from Mixin::MetaPropertyReader

#metadata_property_reader

Methods included from Mixin::MetaPropertyWriter

#metadata_property_writer

Methods included from Mixin::MetaGetter

#meta

Methods included from Mixin::DataGetter

#data

Methods included from Mixin::RootKeyGetter

#root_key

Methods included from Mixin::ContentGetter

#content

Constructor Details

This class inherits a constructor from GoodData::MdObject

Instance Method Details

#attributeGoodData::Attibute

Gives an attribute of current label

Returns:

  • (GoodData::Attibute)


116
117
118
# File 'lib/gooddata/models/metadata/label.rb', line 116

def attribute
  project.attributes(content['formOf'])
end

#attribute_uriGoodData::Attibute

Gives an attribute url of current label. Useful for mass actions when it does not introduce HTTP call.

Returns:

  • (GoodData::Attibute)


122
123
124
# File 'lib/gooddata/models/metadata/label.rb', line 122

def attribute_uri
  content['formOf']
end

#find_element_value(element_id) ⇒ String

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 /

Parameters:

  • element_id (Object)

    Element identifier either Number or a uri as a String

Returns:

  • (String)

    value of the element if found



31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ String

Finds an attribute element URI for given value. This URI can be used by find_element_value to find the original value again

Parameters:

  • value (String)

    value of an label you are looking for

Returns:



18
19
20
21
22
23
24
25
26
# 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(url_or_params = {}, request_payload = {}) ⇒ Array

Gets valid elements using /validElements? API

Returns:

  • (Array)

    Results



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gooddata/models/metadata/label.rb', line 45

def get_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).map { |x, v| "#{x}=#{CGI.escape(v.to_s)}" }.reduce { |acc, elem| "#{acc}&#{elem}" }
    final_url = "#{uri}/validElements?#{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'
    end
  end

  results
end

#value?(value) ⇒ Boolean

Finds if a label has an attribute element for given value.

Parameters:

  • value (String)

    value of an label you are looking for

Returns:

  • (Boolean)


76
77
78
79
80
81
# File 'lib/gooddata/models/metadata/label.rb', line 76

def value?(value)
  find_value_uri(value)
  true
rescue AttributeElementNotFound
  false
end

#values(options = {}) ⇒ Array

Returns all values for this label. This is for inspection purposes only since obviously there can be huge number of elements.

Parameters:

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

    the options to pass to the value list

Options Hash (options):

  • :limit (Number)

    limits the number of values to certain number. Default is 100

Returns:

  • (Array)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gooddata/models/metadata/label.rb', line 87

def values(options = {})
  Enumerator.new do |y|
    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']
        y << {
          :value => v['title'],
          :uri => v['uri']
        }
      end
      break if elements['items'].count < page_limit
      offset += page_limit
    end
  end
end

#values_countObject



108
109
110
111
112
# File 'lib/gooddata/models/metadata/label.rb', line 108

def values_count
  results = get_valid_elements
  count = GoodData::Helpers.get_path(results, %w(validElements paging total))
  count && count.to_i
end