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

DOMAINS =
{
  'development' => %w[pco.test pco.codes],
  'test' => %w[pco.test pco.codes],
  'staging' => %w[planningcenteronline.com planningcenter.com],
  'production' => %w[planningcenteronline.com planningcenter.com],
}.freeze
VERSION =
"3.0.0".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.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pco/url.rb', line 65

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.



61
62
63
# File 'lib/pco/url.rb', line 61

def app_name
  @app_name
end

#pathObject (readonly)

Returns the value of attribute path.



62
63
64
# File 'lib/pco/url.rb', line 62

def path
  @path
end

#queryObject (readonly)

Returns the value of attribute query.



63
64
65
# File 'lib/pco/url.rb', line 63

def query
  @query
end

Class Method Details

.decrypt_query_params(string) ⇒ Object



18
19
20
# File 'lib/pco/url.rb', line 18

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

.method_missing(method_name, *args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pco/url.rb', line 38

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pco/url.rb', line 22

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/pco/url.rb', line 89

def domain
  return @domain if @domain

  return PCO::URL::Engine.domain if PCO::URL::Engine.domain

  case env
  when "production", "staging"
    "planningcenteronline.com"
  when "test"
    "pco.test"
  when "development"
    "pco.test"
  end
end

#hostnameObject



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/pco/url.rb', line 104

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



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pco/url.rb', line 77

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



122
123
124
# File 'lib/pco/url.rb', line 122

def to_s
  uri.to_s
end

#uriObject



116
117
118
119
120
# File 'lib/pco/url.rb', line 116

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