Class: PCO::URL

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

Direct Known Subclasses

ChurchCenter, Get

Defined Under Namespace

Classes: ChurchCenter, Get

Constant Summary collapse

VERSION =
"1.7.1"

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.



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

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=#{URLcrypt.encrypt(query)}" : query
  end
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



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

def app_name
  @app_name
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#queryObject (readonly)

Returns the value of attribute query.



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

def query
  @query
end

Class Method Details

.decrypt_query_params(string) ⇒ Object



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

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

.method_missing(method_name, *args) ⇒ Object



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

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



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

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



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

def domain
  return @domain if @domain

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

#hostnameObject



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

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



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

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



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

def to_s
  uri.to_s
end

#uriObject



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

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