Module: LinkedData::Client::Collection::ClassMethods

Defined in:
lib/ontologies_api_client/collection.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Allows for arbitrary find_by methods. For example:

Ontology.find_by_acronym("BRO")
Ontology.find_by_group_and_category("UMLS", "Anatomy")


17
18
19
20
21
22
23
# File 'lib/ontologies_api_client/collection.rb', line 17

def method_missing(meth, *args, &block)
  if meth.to_s =~ /^find_by_(.+)$/
    find_by($1, *args, &block)
  else
    super
  end
end

Instance Method Details

#all(*args) ⇒ Object

Get all resources from the base collection for a resource



54
55
56
57
# File 'lib/ontologies_api_client/collection.rb', line 54

def all(*args)
  params = args.shift || {}
  entry_point(@media_type, params)
end

#all_to_hash(*args) ⇒ Object

Get all resources from the base collection for a resource as a hash with resource ids as the keys



61
62
63
64
# File 'lib/ontologies_api_client/collection.rb', line 61

def all_to_hash(*args)
  all = all(*args)
  Hash[all.map {|e| [e.id, e]}]
end

#collection_pathObject

For a type that is already defined, get the collection path



48
49
50
# File 'lib/ontologies_api_client/collection.rb', line 48

def collection_path
  uri_from_context(top_level_links, @media_type)
end

#entry_point(media_type, params = {}) ⇒ Object

Get the first collection of resources for a given type



41
42
43
44
# File 'lib/ontologies_api_client/collection.rb', line 41

def entry_point(media_type, params = {})
  params = {include: @include_attrs}.merge(params)
  HTTP.get(uri_from_context(top_level_links, media_type), params)
end

#find(id, params = {}) ⇒ Object

Find a resource by id



78
79
80
81
82
83
# File 'lib/ontologies_api_client/collection.rb', line 78

def find(id, params = {})
  found = where do |obj|
    obj.id.eql?(id)
  end
  found.first
end

#find_by(attrs, *args) ⇒ Object

Find a resource by a combination of attributes



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ontologies_api_client/collection.rb', line 93

def find_by(attrs, *args)
  attributes = attrs.split("_and_")
  values_to_find = args.slice!(0..attributes.length-1)
  params = args.shift
  unless params.is_a?(Hash)
    args.unshift(params)
    params = {}
  end
  where(params) do |obj|
    bools = []
    attributes.each_with_index do |attr, index|
      if obj.respond_to?(attr)
        value = obj.send(attr)
        if value.is_a?(Enumerable)
          bools << value.include?(values_to_find[index])
        else
          bools << (value == values_to_find[index])
        end
      end
    end
    bools.all?
  end
end

#get(id, params = {}) ⇒ Object

Get a resource by id (this will retrieve it from the REST service)



87
88
89
# File 'lib/ontologies_api_client/collection.rb', line 87

def get(id, params = {})
  HTTP.get(id, params)
end

Get all top-level links for the API



27
28
29
# File 'lib/ontologies_api_client/collection.rb', line 27

def top_level_links
  HTTP.get(LinkedData::Client.settings.rest_url)
end

#uri_from_context(object, media_type) ⇒ Object

Return a link given an object (with links) and a media type



33
34
35
36
37
# File 'lib/ontologies_api_client/collection.rb', line 33

def uri_from_context(object, media_type)
  object.links.each do |type, link|
    return link if link.media_type && link.media_type.downcase.eql?(media_type.downcase)
  end
end

#where(params = {}, &block) ⇒ Object

Find certain resources from the collection by passing a block that filters results



68
69
70
71
72
73
74
# File 'lib/ontologies_api_client/collection.rb', line 68

def where(params = {}, &block)
  if block_given?
    return all(params).select {|e| block.call(e)}
  else
    raise ArgumentException("Must provide a block to find items")
  end
end