Class: GoodData::Label
- Inherits:
-
MdObject
- Object
- Rest::Object
- Rest::Resource
- MdObject
- GoodData::Label
- 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
Instance Method Summary collapse
-
#attribute ⇒ GoodData::Attibute
Gives an attribute of current label.
-
#attribute_uri ⇒ GoodData::Attibute
Gives an attribute url of current label.
-
#find_element_value(element_id) ⇒ String
For an element id find values (titles) for this label.
-
#find_value_uri(value) ⇒ String
Finds an attribute element URI for given value.
-
#get_valid_elements(url_or_params = {}, request_payload = {}) ⇒ Array
Gets valid elements using /validElements? API.
-
#value?(value) ⇒ Boolean
Finds if a label has an attribute element for given value.
-
#values(options = {}) ⇒ Array
Returns all values for this label.
- #values_count ⇒ Object
Methods included from Mixin::IsLabel
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
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
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
Methods included from Mixin::Links
Methods inherited from Rest::Resource
Methods inherited from Rest::Object
client, default_client, #initialize, #saved?
Methods included from Mixin::DataPropertyReader
Methods included from Mixin::DataPropertyWriter
Methods included from Mixin::MetaPropertyReader
Methods included from Mixin::MetaPropertyWriter
Methods included from Mixin::MetaGetter
Methods included from Mixin::DataGetter
Methods included from Mixin::RootKeyGetter
Methods included from Mixin::ContentGetter
Constructor Details
This class inherits a constructor from GoodData::MdObject
Instance Method Details
#attribute ⇒ GoodData::Attibute
Gives an attribute of current label
116 117 118 |
# File 'lib/gooddata/models/metadata/label.rb', line 116 def attribute project.attributes(content['formOf']) end |
#attribute_uri ⇒ GoodData::Attibute
Gives an attribute url of current label. Useful for mass actions when it does not introduce HTTP call.
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 /
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
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
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.
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.
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( = {}) Enumerator.new do |y| offset = [:offset] || 0 page_limit = [: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 |