Class: PushRoutes::PushRouteUrl
- Inherits:
-
Object
- Object
- PushRoutes::PushRouteUrl
- Defined in:
- lib/push_routes/push_route_url.rb
Instance Attribute Summary collapse
-
#isNew ⇒ Object
if this push_route_url is a new route that needs to be registered can be determined by if the input passed in was a string or not.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
-
#initialize(input) ⇒ PushRouteUrl
constructor
A new instance of PushRouteUrl.
-
#matches(url) ⇒ bool
returns true of url is a valid expression of the route represented TODO: testing on this function.
-
#notification_string(params) ⇒ String
Returns a true url from the original route with parameters filled in If the route has no parameters this function simply returns the route, ignoring input If the parameters are insufficient an ArgumentError is raised.
-
#param_associations ⇒ Hash
Gets the parameter associations from a url if they are well formed, meaning, each parameter is preceded by another token.
- #to_s ⇒ Object
Constructor Details
#initialize(input) ⇒ PushRouteUrl
Returns a new instance of PushRouteUrl.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/push_routes/push_route_url.rb', line 10 def initialize(input) if (input.is_a?(ActionDispatch::Journey::Route)) #Remove traiing format string @source = input.path.spec.to_s.gsub(/\(\.\:format\)\Z/, "") @isNew = false elsif input.is_a? String @source = input @isNew = true else raise ArgumentError.new("Unexpected input: #{input}") end #Parses out the required parameters for the route as a list of symbols @url_args = @source.scan(/\/\:([^\/]+)/).flatten.map { |e| e.to_sym } end |
Instance Attribute Details
#isNew ⇒ Object
if this push_route_url is a new route that needs to be registered can be determined by if the input passed in was a string or not
8 9 10 |
# File 'lib/push_routes/push_route_url.rb', line 8 def isNew @isNew end |
#source ⇒ Object
Returns the value of attribute source.
3 4 5 |
# File 'lib/push_routes/push_route_url.rb', line 3 def source @source end |
Instance Method Details
#matches(url) ⇒ bool
returns true of url is a valid expression of the route represented TODO: testing on this function
63 64 65 66 67 |
# File 'lib/push_routes/push_route_url.rb', line 63 def matches(url) str = @source.gsub(/(?!\/)\:.*?(?=\/|$)/, "([^\/]+?)") str = "^" + str + "$" Regexp.new(str).match(url) end |
#notification_string(params) ⇒ String
Returns a true url from the original route with parameters filled in If the route has no parameters this function simply returns the route, ignoring input If the parameters are insufficient an ArgumentError is raised
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/push_routes/push_route_url.rb', line 34 def notification_string(params) if @url_args.count == 0 @source else unless (params.is_a?(Hash)) raise ArgumentError.new("Params for #{@source} must be a hash with: #{@url_args}") end built_string = @source @url_args.each do |e| if (params.include?(e)) built_string = built_string.gsub(Regexp.new("\\:#{e}"), params[e].to_s) else raise ArgumentError.new("Missing param #{e}") end end built_string end end |
#param_associations ⇒ Hash
Gets the parameter associations from a url if they are well formed, meaning, each parameter is preceded by another token
72 73 74 |
# File 'lib/push_routes/push_route_url.rb', line 72 def param_associations @source.scan(/\/([^\/]*?)\/\:([^\/]+)/).map {|k,v| [v.to_sym,k]}.to_h end |
#to_s ⇒ Object
53 54 55 |
# File 'lib/push_routes/push_route_url.rb', line 53 def to_s @source end |