Class: Aniks::AniksSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/aniks_search.rb

Class Method Summary collapse

Class Method Details

.content_searchObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/aniks_search.rb', line 4

def self.content_search
  query_string = params[:search_form][:search]
  routing_path = get_routing_path
  @api_response = HTTParty.get("https://cdn.contentful.com/spaces/jrlorqxrzseg/entries?access_token=8926a86933b23d4eebd718b4b7b988b661ececd448753a245b449f99440be972&query=#{query_string}")
  search_pair = {}
  JSON.parse(@api_response)['items'].each do |content|
    search_pair[content['sys']['id']] = content['fields']
  end
  @path = []
  search_pair.each do |id, fields|
    formats = []
    fields.each do |title, value|
      if value.to_s.downcase.include? (query_string.downcase)
        formats << "fields[:#{title}]"
      end
    end
    formats.each do |format|
      controller_array = controller_search(format, id)
      view_array = views_search(format, controller_array)
      routing_name = []
      view_array.each do |a|
        routing_method = ""
        routing_method << a.split("/").last(2).first
        routing_method << ","
        routing_method << a.split("/").last.split(".").first
        routing_name << routing_method
      end
      routing_name.each do |b|
        if routing_path[b].present?
          @path << routing_path[b]
        end
      end
    end
  end
end

.controller_search(format, id) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/aniks_search.rb', line 85

def self.controller_search(format, id)
  controller_array = []
  controller_files = Dir["#{Rails.root}/app/controllers/**/*.rb"]
  controller_files.each do |controllerfile|
    hole_file = File.foreach(controllerfile) do |line|
      if line.include?(id)
        controller_array << line.gsub(/\s+/, "").partition(" ").first.split("=").first
      end
    end
  end
  return controller_array
end

.get_routing_pathObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/aniks_search.rb', line 40

def self.get_routing_path
  routing_path = {}
  all_routes = Rails.application.routes.routes
  routes = all_routes.collect do |route|
    reqs = route.requirements.dup
    reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/
    reqs = reqs.empty? ? "" : reqs.inspect
    {:name => route.name.to_s, :verb => route.verb.to_s, :path => route.path.spec.to_s, :controller => route.requirements[:controller], :action => route.requirements[:action]}
  end
  routes.each do |r|
    if r[:path].exclude?("id")
      routing_path["#{r[:controller]},#{r[:action]}"] = r[:name]
    end
  end
  return routing_path
end

.static_searchObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/aniks_search.rb', line 57

def self.static_search
  view_array = []
  query_string = params[:static_search_form][:search]
  files = Dir["#{Rails.root}/app/views/**/*.html.erb"]
  files.each do |textfile|
    whole_file = File.foreach(textfile) do |line|
      if line.include?(query_string)
        view_array << textfile.split('/blog').last
      end
    end
  end
  routing_path = get_routing_path
  routing_name = []
  @path = []
  view_array.each do |a|
    routing_method = ""
    routing_method << a.split("/").last(2).first
    routing_method << ","
    routing_method << a.split("/").last.split(".").first
    routing_name << routing_method
  end
  routing_name.each do |b|
    if routing_path[b].present?
      @path << routing_path[b]
    end
  end
end

.views_search(format, controller_array) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/aniks_search.rb', line 98

def self.views_search(format, controller_array)
  view_array = []
  controller_array.each do |var|
    files = Dir["#{Rails.root}/app/views/**/*.html.erb"]
    files.each do |textfile|
      whole_file = File.foreach(textfile) do |line|
        if line.include?("#{var}.#{format}")
          view_array << textfile.split('/blog').last
        end
      end
    end
  end
  return view_array
end