Class: ForemanDocker::ImageSearch

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_docker/image_search.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ImageSearch

Returns a new instance of ImageSearch.



3
4
5
6
7
8
# File 'app/services/foreman_docker/image_search.rb', line 3

def initialize(*args)
  @sources = {}
  args.each do |source|
    add_source(source)
  end
end

Instance Method Details

#add_source(source) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'app/services/foreman_docker/image_search.rb', line 10

def add_source(source)
  case source
  when ForemanDocker::Docker
    @sources[:compute_resource] ||= []
    @sources[:compute_resource] << source
  when Service::RegistryApi
    @sources[:registry] ||= []
    @sources[:registry] << source
  end
end

#available?(query) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/services/foreman_docker/image_search.rb', line 47

def available?(query)
  tags(query).present?
end

#images(query) ⇒ Object



37
38
39
# File 'app/services/foreman_docker/image_search.rb', line 37

def images(query)
  sources_results_for(:search, query)
end

#remove_source(source) ⇒ Object



21
22
23
24
25
# File 'app/services/foreman_docker/image_search.rb', line 21

def remove_source(source)
  @sources.each do |_, sources|
    sources.delete(source)
  end
end

#search(query) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'app/services/foreman_docker/image_search.rb', line 27

def search(query)
  return [] if query[:term].blank? || query[:term] == ':'

  unless query[:tags] == 'true'
    images(query[:term])
  else
    tags(query[:term])
  end
end

#tags(query) ⇒ Object



41
42
43
44
45
# File 'app/services/foreman_docker/image_search.rb', line 41

def tags(query)
  image_name, tag = query.split(':')
  sources_results_for(:tags, image_name, tag)
    .map { |tag_name| { 'name' => tag_name } }
end