insite 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 16f1ba3790818bbea311d4b4ae7dfafa48ce7e054e157797817ac6554efac22a
4
- data.tar.gz: fcb7131304d81c5556913a24f085eb1f7df2c62d8f0bdb97a410f15bd9c7bb03
3
+ metadata.gz: 5149a50ee698c0eeec91cb7127d89a2eb175f09d50967bc40fefa78137cee823
4
+ data.tar.gz: 4644600b968f6f8846a1a3f5c1e9fc02c89afd7d88c81f9e0f45bd873e5d7858
5
5
  SHA512:
6
- metadata.gz: ac5b61ba8ccde92a29dd8450fa6a8a079e201eec5571ab4fa5f0413d5f2de5f15b70d5624ee0f87cddd96a1dcaadf9f3393330f147ee93bdb3a8fb3ff48036b5
7
- data.tar.gz: 86d7e500ec84c9061f5a1d8a7078b945fe1a8fcd714d8e3f5f7ec2bc68a96247b877fcb88c2d4506e77c74e6cfafa0b071d882b1a048b974d3e642fb6f9249ec
6
+ metadata.gz: a40f146bd90b92f28939eb81917d33fb7d61ec82a3f943a5c2012a4aba12eb4bc273f0305435a68d3759ce87a5a5cba721a2d5b024a991f8f3ede5e23540988a
7
+ data.tar.gz: 269097cb25d93d68a441510daf7642660577aab7f63571ee8b1055af4922c4607ffd3c32e7724b84b12fa9ed70994552fceaf19bf1ec47fe101f75af1fa4e819
@@ -1,8 +1,9 @@
1
1
  # TODO: Title matcher?
2
2
  # TODO: Add page query methods.
3
+ require 'pry'
3
4
  module Insite
4
5
  class DefinedPage
5
- attr_reader :arguments, :browser, :has_fragment, :page_attributes, :page_elements, :page_features, :page_url, :query_arguments, :required_arguments, :site, :url_template, :url_matcher, :component_elements
6
+ attr_reader :arguments, :browser, :has_fragment, :page_attributes, :page_elements, :page_features, :page_url, :required_arguments, :site, :url_template, :url_matcher, :component_elements
6
7
 
7
8
  include Insite::CommonMethods
8
9
  extend Insite::ComponentMethods
@@ -12,7 +13,7 @@ module Insite
12
13
  alias_method :update_page, :update_object
13
14
 
14
15
  class << self
15
- attr_reader :has_fragment, :page_attributes, :page_elements, :page_features, :page_url, :url_matcher, :url_hash, :url_template
16
+ attr_reader :arguments, :has_fragment, :page_attributes, :page_elements, :page_features, :page_url, :required_arguments, :url_matcher, :url_hash, :url_template
16
17
  attr_accessor :component_elements
17
18
 
18
19
  # Allows you to set special page attributes that affect page behavior. The two page
@@ -68,15 +69,6 @@ module Insite
68
69
  @page_attributes.include? :page_template
69
70
  end
70
71
 
71
- # Returns an array of symbols representing the required arguments for the page's page URL.
72
- def required_arguments
73
- @arguments ||= @url_template.keys.map { |k| k.to_sym }
74
- end
75
-
76
- def query_arguments
77
- required_arguments.find { |x| @url_template.pattern =~ /\?.*#{x}=*/ }
78
- end
79
-
80
72
  # Used to define the full or relative URL to the page. Typically, you will *almost* *always* want to use
81
73
  # this method when defining a page object (but see notes below.) The URL can be defined in a number
82
74
  # of different ways. Here are some examples using Google News:
@@ -190,6 +182,13 @@ module Insite
190
182
  end
191
183
 
192
184
  @has_fragment = @url_template.pattern =~ /#/
185
+
186
+ @arguments ||= @url_template.keys.map(&:to_sym)
187
+
188
+ @required_arguments ||= @url_template.pattern
189
+ .scan(/\{(#{self.arguments.join('|')})/)
190
+ .flatten
191
+ .map(&:to_sym)
193
192
  end
194
193
 
195
194
  # Optional. Allows you to specify a fallback mechanism for checking to see if the correct page is
@@ -251,10 +250,10 @@ module Insite
251
250
  @page_elements = self.class.page_elements
252
251
  # TODO: Clean this up
253
252
  @page_features = self.class.instance_variable_get(:@page_features)
253
+ @all_arguments = self.class.arguments
254
254
  @required_arguments = self.class.required_arguments
255
255
  @url_matcher = self.class.url_matcher
256
256
  @url_template = self.class.url_template
257
- @query_arguments = self.class.query_arguments
258
257
  @has_fragment = self.class.has_fragment
259
258
 
260
259
  # Try to expand the URL template if the URL has parameters. Stores the
@@ -266,7 +265,7 @@ module Insite
266
265
  match = @url_template.match(@browser.url)
267
266
 
268
267
  # Raise if args are provided and the page doesn't take any args.
269
- if (@required_arguments - [:scheme]).empty? && args
268
+ if (@all_arguments - [:scheme]).empty? && args
270
269
  raise(
271
270
  Insite::Errors::PageInitError,
272
271
  "#{args.class} was provided as a #{self.class.name} initialization argument, " \
@@ -274,28 +273,30 @@ module Insite
274
273
  )
275
274
  end
276
275
 
277
- if @required_arguments.present? && !args
278
- @required_arguments.each do |arg|
276
+ if @all_arguments.present? && !args
277
+ @all_arguments.each do |arg|
279
278
  if match && match.keys.include?(arg.to_s)
280
279
  @arguments[arg] = match[arg.to_s]
281
280
  elsif @site.arguments[arg]
282
281
  @arguments[arg] = @site.arguments[arg]
283
282
  elsif @site.respond_to?(arg) && @site.public_send(arg)
284
283
  @arguments[arg] = @site.public_send(arg)
284
+ elsif !@required_arguments.include?(arg)
285
+ @arguments[arg] = nil
285
286
  else
286
287
  raise(
287
288
  Insite::Errors::PageInitError,
288
- "No arguments provided when attempting to initialize #{self.class.name}. " \
289
- "This page object requires the following arguments for initialization: "\
290
- ":#{@required_arguments.join(', :')}.\n\n#{caller.join("\n")}"
289
+ "An error occurred when attempting to build a URL for the #{self.class} page.\n" \
290
+ "URL template:\t#{@url_template.pattern}\n" \
291
+ "URL arguments:\t#{@arguments}\n" \
292
+ "Required args:\t#{@required_arguments.join(', :')}.\n" \
293
+ "Generated URL:\t#{@url_template.expand(@arguments)}\n"
291
294
  )
292
295
  end
293
296
  end
294
- elsif @required_arguments.present?
295
- @required_arguments.each do |arg| # Try to extract each URL argument from the hash or object provided, OR from the site object.
297
+ elsif @all_arguments.present?
298
+ @all_arguments.each do |arg| # Try to extract each URL argument from the hash or object provided, OR from the site object.
296
299
  if args.is_a?(Hash) && args.present?
297
- # args = args.with_indifferent_access
298
-
299
300
  if @arguments[arg] #The hash has the required argument.
300
301
  @arguments[arg]= args[arg]
301
302
  elsif match && match.keys.include?(arg.to_s)
@@ -304,41 +305,41 @@ module Insite
304
305
  @arguments[arg] = site.public_send(arg)
305
306
  elsif @site.arguments[arg]
306
307
  @arguments[arg] = @site.arguments[arg]
308
+ elsif !@required_arguments.include?(arg)
309
+ @arguments[arg] = nil
307
310
  else
308
311
  raise(
309
312
  Insite::Errors::PageInitError,
310
- "A required page argument is missing. #{args.class} was provided, " \
311
- "but this object did not respond to :#{arg}, which is necessary to " \
312
- "build a URL for the #{self.class.name} page.\n\n#{caller.join("\n")}"
313
+ "An error occurred when attempting to build a URL for the #{self.class} page.\n" \
314
+ "URL template:\t#{@url_template.pattern}\n" \
315
+ "URL arguments:\t#{@arguments}\n" \
316
+ "Required args:\t#{@required_arguments.join(', :')}.\n" \
317
+ "Generated URL:\t#{@url_template.expand(@arguments)}\n"
313
318
  )
314
319
  end
315
320
  elsif args # Some non-hash object was provided.
316
321
  if args.respond_to?(arg) #The hash has the required argument.
317
- # @arguments[arg]= args.public_send(arg)
318
322
  @arguments[arg]= args.public_send(arg)
319
323
  elsif @site.respond_to?(arg)
320
324
  @arguments[arg]= site.public_send(arg)
321
325
  elsif @site.arguments[arg]
322
326
  @arguments[arg] = @site.arguments[arg]
327
+ elsif !@required_arguments.include?(arg)
328
+ @arguments[arg] = nil
323
329
  else
324
330
  raise(
325
331
  Insite::Errors::PageInitError,
326
- "A required page argument is missing. #{args.class} was provided, but " \
327
- "this object did not respond to :#{arg}, which is necessary to build " \
328
- "a URL for the #{self.class.name} page.\n\n#{caller.join("\n")}"
332
+ "An error occurred when attempting to build a URL for the #{self.class} page.\n" \
333
+ "URL template:\t#{@url_template.pattern}\n" \
334
+ "URL arguments:\t#{@arguments}\n" \
335
+ "Required args:\t#{@required_arguments.join(', :')}.\n" \
336
+ "Generated URL:\t#{@url_template.expand(@arguments)}\n"
329
337
  )
330
338
  end
331
339
  else
332
340
  # Do nothing here yet.
333
341
  end
334
342
  end
335
- # If there are no required arguments then nothing should be provided.
336
- # elsif (@required_arguments - [:scheme]).empty? && args
337
- # raise(
338
- # Insite::Errors::PageInitError,
339
- # "#{args.class} was provided as a #{self.class.name} initialization argument, " \
340
- # "but the page URL doesn't require any arguments.\n\n#{caller.join("\n")}"
341
- # )
342
343
  else
343
344
  # Do nothing here yet.
344
345
  end
@@ -390,9 +391,7 @@ module Insite
390
391
  @page_attributes.include? :navigation_disabled
391
392
  end
392
393
 
393
- def on_page?
394
- url = @browser.url
395
-
394
+ def on_page?(url = @browser.url)
396
395
  if @url_matcher
397
396
  if @url_matcher =~ url
398
397
  return true
@@ -1,6 +1,6 @@
1
1
  module Insite
2
2
  class UndefinedPage
3
- attr_reader :arguments, :browser, :has_fragment, :page_attributes, :page_elements, :page_features, :page_url, :query_arguments, :required_arguments, :site, :url_template, :url_matcher
3
+ attr_reader :arguments, :browser, :has_fragment, :page_attributes, :page_elements, :page_features, :page_url, :required_arguments, :site, :url_template, :url_matcher
4
4
 
5
5
  include Insite::CommonMethods
6
6
  extend Insite::ComponentMethods
@@ -1,3 +1,3 @@
1
1
  module Insite
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: insite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Fitisoff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-04 00:00:00.000000000 Z
11
+ date: 2018-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport