Class: Goldencobra::Redirector

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/goldencobra/redirector.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#import_csv_dataObject

Returns the value of attribute import_csv_data.



9
10
11
# File 'app/models/goldencobra/redirector.rb', line 9

def import_csv_data
  @import_csv_data
end

Class Method Details

.add_param_to_url(url, uri_params) ⇒ string

Add a url-params tu an url

Parameters:

Returns:



119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/goldencobra/redirector.rb', line 119

def self.add_param_to_url(url, uri_params)
  target_uri = Addressable::URI.parse(url)
  target_params = Rack::Utils.parse_nested_query(target_uri.query.to_s)
  request_params = Rack::Utils.parse_nested_query(uri_params)
  merged_params = target_params.merge(request_params)
  if merged_params.present?
    return "#{target_uri.scheme}://#{target_uri.host}#{target_uri.path}?#{merged_params.to_param}"
  else
    return "#{target_uri.scheme}://#{target_uri.host}#{target_uri.path}"
  end
end

.get_by_request(request_original_url) ⇒ Array

Returns a target url where to redirect of a given url

Parameters:

  • request_original_url (String)

    SourceURl of Request

Returns:

  • (Array)

    target url to rewrite to | status code for redirection



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/goldencobra/redirector.rb', line 70

def self.get_by_request(request_original_url)
  begin
    uri = Addressable::URI.parse(request_original_url)
  rescue
    uri = nil
  end
  if uri.present?
    uri_params = Rack::Utils.parse_nested_query(uri.query.to_s)
    request_path = "#{uri.scheme}://#{uri.host}#{uri.path}"
    redirects = Goldencobra::Redirector.active.where(source_url: request_path)
    if redirects.any?
      #if multiple redirectors found, select the first
      redirect = redirects.first
      redirecter_source_uri = Addressable::URI.parse(redirect.source_url)
      if redirecter_source_uri.path == uri.path
        #Wenn die url parameter egal sind
        if redirect.ignore_url_params
          return [redirect.rewrite_target_url(uri.query), redirect.redirection_code]
        else
          #wenn die urlparameter nicht egal sind und identisch sind
          source_params = Rack::Utils.parse_nested_query(redirecter_source_uri.query.to_s)
          if !source_params.map{|k,v| uri_params[k] == v}.include?(false)
            return [redirect.rewrite_target_url(uri.query), redirect.redirection_code]
          else
            return nil
          end
        end
      end
    else
      return nil
    end
  else
    return nil
  end
end

Instance Method Details

#check_csv_dataBoolean

Validator wenn es csv Daten zum importierne gibt

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/goldencobra/redirector.rb', line 42

def check_csv_data
  if import_csv_data.present?
    begin
      all_source_urls = Goldencobra::Redirector.where(active: true).pluck(:source_url)
      data = CSV.parse(import_csv_data, { col_sep: "," })
      source_urls_from_data = data.map{|a| a[0].strip}
      if source_urls_from_data.length != source_urls_from_data.uniq.length
        errors.add(:import_csv_data, "Die CSV Daten enthalten mehrere Zeilen mit gleichen Source URLs<br>" )
      end
      data.each_with_index do |row, index|
        if row.length != 2
          errors.add(:import_csv_data, "Format der CSV Datei hat nicht 2 Spalten in Zeile #{index + 1}<br>" )
        end
        if all_source_urls.include?(row[0].strip)
          errors.add(:import_csv_data, "Source URL in Zeile #{index + 1} existiert bereits<br>" )
        end
      end
    rescue
      errors.add(:import_csv_data, "Die CSV Daten sind leider ungültig")
    end
  end
end

#create_multiples_from_csv_dataGoldencobra::Redirector

Creates multiple new Redirections based on ‘import_csv_data’ and deletes this single, new, invalid record afterwards



27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/goldencobra/redirector.rb', line 27

def create_multiples_from_csv_data
  if import_csv_data.present?
      data = CSV.parse(import_csv_data, { col_sep: "," })
      Goldencobra::Redirector.transaction do
        data.each do |row|
          Goldencobra::Redirector.create(source_url: row[0].strip, target_url: row[1].strip, redirection_code: redirection_code, active: active, ignore_url_params: ignore_url_params )
        end
      end
      self.destroy
  end
end

#rewrite_target_url(uri_params) ⇒ String

Helper Method for rewriting urls

Parameters:

  • uri_params (String)

    foo=bar&test=12

Returns:

  • (String)

    TargetURl of given Redirector merged with source params



110
111
112
# File 'app/models/goldencobra/redirector.rb', line 110

def rewrite_target_url(uri_params)
  Goldencobra::Redirector.add_param_to_url(self.target_url, uri_params)
end