Class: PCO::URL

Inherits:
Object
  • Object
show all
Defined in:
lib/pco/url.rb,
lib/pco/url/get.rb,
lib/pco/url/engine.rb,
lib/pco/url/version.rb,
lib/pco/url/encryption.rb,
lib/pco/url/church_center.rb,
lib/pco/url/engine/domain_middleware.rb

Direct Known Subclasses

ChurchCenter, Get

Defined Under Namespace

Modules: Encryption Classes: ChurchCenter, Engine, Get

Constant Summary collapse

VERSION =
"2.1.3".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name:, path: nil, query: nil, encrypt_query_params: false, domain: nil) ⇒ URL

Returns a new instance of URL.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pco/url.rb', line 58

def initialize(app_name:, path: nil, query: nil, encrypt_query_params: false, domain: nil)
  @app_name = app_name.to_s.tr("_", "-")
  @path     = path
  @domain   = domain

  @path = @path[1..-1] if @path && @path[0] == "/"

  if query
    @query = encrypt_query_params ? "_e=#{Encryption.encrypt(query)}" : query
  end
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



54
55
56
# File 'lib/pco/url.rb', line 54

def app_name
  @app_name
end

#pathObject (readonly)

Returns the value of attribute path.



55
56
57
# File 'lib/pco/url.rb', line 55

def path
  @path
end

#queryObject (readonly)

Returns the value of attribute query.



56
57
58
# File 'lib/pco/url.rb', line 56

def query
  @query
end

Class Method Details

.decrypt_query_params(string) ⇒ Object



11
12
13
# File 'lib/pco/url.rb', line 11

def decrypt_query_params(string)
  Encryption.decrypt(string)
end

.method_missing(method_name, *args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pco/url.rb', line 31

def method_missing(method_name, *args)
  path = args.map { |p| p.sub(%r{\A/+}, "").sub(%r{/+\Z}, "") }.join("/")
  case method_name
  when :church_center
    PCO::URL::ChurchCenter.new(path: path).to_s
  when :get
    PCO::URL::Get.new(path: path).to_s
  else
    new(app_name: method_name, path: path).to_s
  end
end

.parse(string) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pco/url.rb', line 15

def parse(string)
  if (uri = URI.parse(string))
    app_name = uri.host.match(/(\w+)(-staging)?/)[1]

    if uri.query
      if (encrypted_part = encrypted_query_string(uri.query))
        uri.query.sub!("_e=#{encrypted_part}", decrypt_query_params(encrypted_part))
      end
    end

    new(app_name: app_name, path: uri.path, query: uri.query)
  else
    fail InvalidPCOURLString, "Unrecognized PCO::URL url string"
  end
end

Instance Method Details

#domainObject



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pco/url.rb', line 82

def domain
  return @domain if @domain

  case env
  when "production", "staging"
    "planningcenteronline.com"
  when "test"
    "pco.test"
  when "development"
    PCO::URL::Engine.domain || "pco.test"
  end
end

#hostnameObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pco/url.rb', line 95

def hostname
  # Try "CHECK_INS_URL" then url_for_app("check-ins")
  return env_overridden_hostname.split("://")[1] if env_overridden_hostname

  case env
  when "staging"
    "#{app_name}-staging.#{domain}"
  else
    "#{app_name}.#{domain}"
  end
end

#schemeObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pco/url.rb', line 70

def scheme
  # Try "CHECK_INS_URL" then url_for_app("check-ins")
  return env_overridden_hostname.split("://")[0] if env_overridden_hostname

  case env
  when "production", "staging"
    "https"
  else
    "http"
  end
end

#to_sObject



113
114
115
# File 'lib/pco/url.rb', line 113

def to_s
  uri.to_s
end

#uriObject



107
108
109
110
111
# File 'lib/pco/url.rb', line 107

def uri
  q = query ? "?#{query}" : nil
  url_string = "#{scheme}://#{hostname}/#{path}#{q}".sub(%r{(/)+$}, "")
  URI(url_string)
end