quandl_client 2.7.11 → 2.7.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/.gitignore +7 -7
  2. data/.rspec +1 -1
  3. data/.travis.yml +20 -20
  4. data/.yardopts +2 -2
  5. data/Gemfile +12 -12
  6. data/Guardfile +8 -8
  7. data/LICENSE +7 -7
  8. data/README.md +303 -303
  9. data/Rakefile +31 -31
  10. data/UPGRADE.md +220 -214
  11. data/VERSION +1 -1
  12. data/examples/create.rb +32 -32
  13. data/examples/find.rb +17 -17
  14. data/examples/login.rb +12 -12
  15. data/examples/search.rb +12 -12
  16. data/examples/trims.rb +15 -15
  17. data/lib/quandl/client/base/attributes.rb +15 -15
  18. data/lib/quandl/client/base/model.rb +40 -40
  19. data/lib/quandl/client/base/search.rb +74 -74
  20. data/lib/quandl/client/base/validation.rb +103 -101
  21. data/lib/quandl/client/base.rb +91 -91
  22. data/lib/quandl/client/middleware/parse_json.rb +87 -85
  23. data/lib/quandl/client/middleware.rb +9 -9
  24. data/lib/quandl/client/models/dataset/data.rb +57 -57
  25. data/lib/quandl/client/models/dataset.rb +269 -269
  26. data/lib/quandl/client/models/location.rb +10 -10
  27. data/lib/quandl/client/models/report.rb +14 -14
  28. data/lib/quandl/client/models/scraper.rb +16 -16
  29. data/lib/quandl/client/models/sheet.rb +50 -50
  30. data/lib/quandl/client/models/source.rb +48 -48
  31. data/lib/quandl/client/models/superset.rb +65 -65
  32. data/lib/quandl/client/models/user.rb +7 -7
  33. data/lib/quandl/client/version.rb +14 -14
  34. data/lib/quandl/client.rb +54 -53
  35. data/lib/quandl/her/collection.rb +18 -0
  36. data/lib/quandl/her/remove_method_data.rb +8 -8
  37. data/lib/quandl/pattern/client.rb +8 -8
  38. data/lib/quandl/pattern.rb +37 -37
  39. data/quandl_client.gemspec +33 -33
  40. data/spec/factories/dataset.rb +10 -10
  41. data/spec/factories/sheet.rb +7 -7
  42. data/spec/factories/source.rb +9 -9
  43. data/spec/fixtures/scraper.rb +5 -5
  44. data/spec/lib/quandl/client/dataset/attributes_spec.rb +63 -63
  45. data/spec/lib/quandl/client/dataset/data_spec.rb +92 -92
  46. data/spec/lib/quandl/client/dataset/location_spec.rb +65 -65
  47. data/spec/lib/quandl/client/dataset/persistence_spec.rb +104 -104
  48. data/spec/lib/quandl/client/dataset/search_spec.rb +19 -19
  49. data/spec/lib/quandl/client/dataset/source_spec.rb +47 -47
  50. data/spec/lib/quandl/client/dataset/trim_spec.rb +35 -35
  51. data/spec/lib/quandl/client/dataset/validation_spec.rb +68 -68
  52. data/spec/lib/quandl/client/dataset_spec.rb +57 -57
  53. data/spec/lib/quandl/client/scraper_spec.rb +71 -71
  54. data/spec/lib/quandl/client/sheet_spec.rb +37 -37
  55. data/spec/lib/quandl/client/source_spec.rb +51 -51
  56. data/spec/spec_helper.rb +30 -30
  57. metadata +28 -4
@@ -1,51 +1,51 @@
1
- module Quandl
2
- module Client
3
-
4
- class Sheet < Quandl::Client::Base
5
-
6
- ##########
7
- # SCOPES #
8
- ##########
9
-
10
- scope :query, :page, :parent_url_title
11
-
12
-
13
- ################
14
- # ASSOCIATIONS #
15
- ################
16
-
17
- def parent
18
- @parent ||= Quandl::Client::Sheet.find(parent_url_title)
19
- end
20
-
21
- def children
22
- Quandl::Client::Sheet.parent_url_title(self.full_url_title)
23
- end
24
-
25
-
26
- ###############
27
- # VALIDATIONS #
28
- ###############
29
-
30
- validates :title, presence: true
31
-
32
-
33
- ##############
34
- # PROPERTIES #
35
- ##############
36
-
37
- attributes :title, :content, :url_title, :full_url_title, :description, :skip_browse
38
-
39
- def html
40
- @html ||= self.attributes[:html] || Quandl::Client::Sheet.find(full_url_title).attributes[:html]
41
- end
42
-
43
- def parent_url_title
44
- @parent_url_title ||= self.full_url_title.split('/')[0..-2].join()
45
- end
46
-
47
-
48
- end
49
-
50
- end
1
+ module Quandl
2
+ module Client
3
+
4
+ class Sheet < Quandl::Client::Base
5
+
6
+ ##########
7
+ # SCOPES #
8
+ ##########
9
+
10
+ scope :query, :page, :parent_url_title
11
+
12
+
13
+ ################
14
+ # ASSOCIATIONS #
15
+ ################
16
+
17
+ def parent
18
+ @parent ||= Quandl::Client::Sheet.find(parent_url_title)
19
+ end
20
+
21
+ def children
22
+ Quandl::Client::Sheet.parent_url_title(self.full_url_title)
23
+ end
24
+
25
+
26
+ ###############
27
+ # VALIDATIONS #
28
+ ###############
29
+
30
+ validates :title, presence: true
31
+
32
+
33
+ ##############
34
+ # PROPERTIES #
35
+ ##############
36
+
37
+ attributes :title, :content, :url_title, :full_url_title, :description, :skip_browse
38
+
39
+ def html
40
+ @html ||= self.attributes[:html] || Quandl::Client::Sheet.find(full_url_title).attributes[:html]
41
+ end
42
+
43
+ def parent_url_title
44
+ @parent_url_title ||= self.full_url_title.split('/')[0..-2].join()
45
+ end
46
+
47
+
48
+ end
49
+
50
+ end
51
51
  end
@@ -1,49 +1,49 @@
1
- module Quandl
2
- module Client
3
-
4
- class Source < Quandl::Client::Base
5
-
6
- class << self
7
-
8
- def cached
9
- @@cached ||= {}
10
- end
11
-
12
- end
13
-
14
- ##########
15
- # SCOPES #
16
- ##########
17
-
18
- scope :query
19
- scope :page, ->(p){ where( page: p.to_i )}
20
- scope :code, ->(c){ where( code: c.to_s.upcase )}
21
-
22
-
23
- ###############
24
- # ASSOCIATIONS #
25
- ###############
26
-
27
- def datasets
28
- Quandl::Client::Dataset.source_code(code)
29
- end
30
-
31
-
32
- ###############
33
- # VALIDATIONS #
34
- ###############
35
-
36
- validates :code, presence: true, length: { minimum: 2 }, format: { with: Quandl::Pattern.code, message: "is invalid. Expected format: #{Quandl::Pattern.code.to_example}" }
37
- validates :host, :name, presence: true
38
-
39
-
40
- ##############
41
- # PROPERTIES #
42
- ##############
43
-
44
- attributes :code, :name, :host, :description, :datasets_count, :use_proxy, :type, :concurrency
45
-
46
- end
47
-
48
- end
1
+ module Quandl
2
+ module Client
3
+
4
+ class Source < Quandl::Client::Base
5
+
6
+ class << self
7
+
8
+ def cached
9
+ @@cached ||= {}
10
+ end
11
+
12
+ end
13
+
14
+ ##########
15
+ # SCOPES #
16
+ ##########
17
+
18
+ scope :query
19
+ scope :page, ->(p){ where( page: p.to_i )}
20
+ scope :code, ->(c){ where( code: c.to_s.upcase )}
21
+
22
+
23
+ ###############
24
+ # ASSOCIATIONS #
25
+ ###############
26
+
27
+ def datasets
28
+ Quandl::Client::Dataset.source_code(code)
29
+ end
30
+
31
+
32
+ ###############
33
+ # VALIDATIONS #
34
+ ###############
35
+
36
+ validates :code, presence: true, length: { minimum: 2 }, format: { with: Quandl::Pattern.code, message: "is invalid. Expected format: #{Quandl::Pattern.code.to_example}" }
37
+ validates :host, :name, presence: true
38
+
39
+
40
+ ##############
41
+ # PROPERTIES #
42
+ ##############
43
+
44
+ attributes :code, :name, :host, :description, :datasets_count, :use_proxy, :type, :concurrency
45
+
46
+ end
47
+
48
+ end
49
49
  end
@@ -1,66 +1,66 @@
1
- class Quandl::Client::Superset < Quandl::Client::Base
2
-
3
- scope :query, :page, :owner, :code, :source_code
4
-
5
- attributes :id, :source_code, :code, :name, :urlize_name, :description, :updated_at, :private
6
- attributes :column_codes, :column_names
7
- attributes :frequency, :from_date, :to_date
8
-
9
- validates :code, presence: true, format: { with: Quandl::Pattern.code, message: "is invalid. Expected format: #{Quandl::Pattern.code.to_example}" }
10
- validates :column_codes, presence: true
11
- validate :column_codes_should_be_valid!
12
-
13
- def self.find_or_build( attributes={} )
14
- attrs = attributes.symbolize_keys!
15
- # by id
16
- record = self.find(attrs[:id]) if attrs[:id].present?
17
- # by source_code/code
18
- record = self.find(File.join(attrs[:source_code], attrs[:code])) if !record.try(:exists?) && attrs[:source_code].present? && attrs[:code].present?
19
- # by code
20
- record = self.find(attrs[:code]) if !record.try(:exists?) && attrs[:code].present?
21
- # build
22
- record = self.new unless record.try(:exists?)
23
- record.assign_attributes(attrs)
24
- record
25
- end
26
-
27
- def self.example
28
- self.new( code: "SUPERSET_EXAMPLE", name: "Superset Name", description: "Superset description", column_codes: ['NSE.OIL.1'], column_names: ['Column Name'] )
29
- end
30
-
31
- def data
32
- @data ||= Quandl::Client::Dataset::Data.with_id(id)
33
- end
34
-
35
- def full_url
36
- File.join(Quandl::Client::Base.url.gsub(/api\/?/, ''), full_code)
37
- end
38
-
39
- def full_code
40
- File.join(self.source_code.to_s, self.code.to_s)
41
- end
42
-
43
- protected
44
-
45
- def column_codes_should_be_valid!
46
- # must be an array
47
- unless column_codes.respond_to?(:each)
48
- # failure
49
- self.errors.add( :column_codes, "expected an array, but got #{column_codes.class}.")
50
- # nothing more to do
51
- return false
52
- end
53
- # check each code
54
- column_codes.each do |code|
55
- # is the code valid?
56
- next if code =~ /#{Quandl::Pattern.code}\.#{Quandl::Pattern.code}\.[0-9]+/
57
- # otherwise report error
58
- self.errors.add( :column_codes, "Code '#{code}' is invalid. Expected: /#{Quandl::Pattern.code.to_example}.#{Quandl::Pattern.code.to_example}.INTEGER/" )
59
- # nothing more to do here
60
- return false
61
- end
62
- # success
63
- true
64
- end
65
-
1
+ class Quandl::Client::Superset < Quandl::Client::Base
2
+
3
+ scope :query, :page, :owner, :code, :source_code
4
+
5
+ attributes :id, :source_code, :code, :name, :urlize_name, :description, :updated_at, :private
6
+ attributes :column_codes, :column_names
7
+ attributes :frequency, :from_date, :to_date
8
+
9
+ validates :code, presence: true, format: { with: Quandl::Pattern.code, message: "is invalid. Expected format: #{Quandl::Pattern.code.to_example}" }
10
+ validates :column_codes, presence: true
11
+ validate :column_codes_should_be_valid!
12
+
13
+ def self.find_or_build( attributes={} )
14
+ attrs = attributes.symbolize_keys!
15
+ # by id
16
+ record = self.find(attrs[:id]) if attrs[:id].present?
17
+ # by source_code/code
18
+ record = self.find(File.join(attrs[:source_code], attrs[:code])) if !record.try(:exists?) && attrs[:source_code].present? && attrs[:code].present?
19
+ # by code
20
+ record = self.find(attrs[:code]) if !record.try(:exists?) && attrs[:code].present?
21
+ # build
22
+ record = self.new unless record.try(:exists?)
23
+ record.assign_attributes(attrs)
24
+ record
25
+ end
26
+
27
+ def self.example
28
+ self.new( code: "SUPERSET_EXAMPLE", name: "Superset Name", description: "Superset description", column_codes: ['NSE.OIL.1'], column_names: ['Column Name'] )
29
+ end
30
+
31
+ def data
32
+ @data ||= Quandl::Client::Dataset::Data.with_id(id)
33
+ end
34
+
35
+ def full_url
36
+ File.join(Quandl::Client::Base.url.gsub(/api\/?/, ''), full_code)
37
+ end
38
+
39
+ def full_code
40
+ File.join(self.source_code.to_s, self.code.to_s)
41
+ end
42
+
43
+ protected
44
+
45
+ def column_codes_should_be_valid!
46
+ # must be an array
47
+ unless column_codes.respond_to?(:each)
48
+ # failure
49
+ self.errors.add( :column_codes, "expected an array, but got #{column_codes.class}.")
50
+ # nothing more to do
51
+ return false
52
+ end
53
+ # check each code
54
+ column_codes.each do |code|
55
+ # is the code valid?
56
+ next if code =~ /#{Quandl::Pattern.code}\.#{Quandl::Pattern.code}\.[0-9]+/
57
+ # otherwise report error
58
+ self.errors.add( :column_codes, "Code '#{code}' is invalid. Expected: /#{Quandl::Pattern.code.to_example}.#{Quandl::Pattern.code.to_example}.INTEGER/" )
59
+ # nothing more to do here
60
+ return false
61
+ end
62
+ # success
63
+ true
64
+ end
65
+
66
66
  end
@@ -1,8 +1,8 @@
1
- class Quandl::Client::User < Quandl::Client::Base
2
-
3
- custom_get :info
4
- custom_post :login
5
-
6
- attributes :auth_token
7
-
1
+ class Quandl::Client::User < Quandl::Client::Base
2
+
3
+ custom_get :info
4
+ custom_post :login
5
+
6
+ attributes :auth_token
7
+
8
8
  end
@@ -1,15 +1,15 @@
1
- module Quandl
2
- module Client
3
- VERSION = File.read(File.expand_path(File.join(File.dirname(__FILE__), '../../../VERSION'))).strip.rstrip
4
- API_VERSION = 'v2'
5
-
6
- class << self
7
-
8
- def api_version
9
- API_VERSION
10
- end
11
-
12
- end
13
-
14
- end
1
+ module Quandl
2
+ module Client
3
+ VERSION = File.read(File.expand_path(File.join(File.dirname(__FILE__), '../../../VERSION'))).strip.rstrip
4
+ API_VERSION = 'v2'
5
+
6
+ class << self
7
+
8
+ def api_version
9
+ API_VERSION
10
+ end
11
+
12
+ end
13
+
14
+ end
15
15
  end
data/lib/quandl/client.rb CHANGED
@@ -1,54 +1,55 @@
1
- require "quandl/client/version"
2
-
3
- require 'scope_composer'
4
- require 'her'
5
- require 'quandl/her/remove_method_data'
6
- require 'quandl/logger'
7
- require "quandl/data"
8
-
9
- require 'quandl/pattern'
10
- require 'quandl/pattern/client'
11
-
12
- require 'quandl/client/middleware'
13
- require 'quandl/client/base'
14
- require 'quandl/client/models/dataset'
15
- require 'quandl/client/models/sheet'
16
- require 'quandl/client/models/source'
17
- require 'quandl/client/models/user'
18
- require 'quandl/client/models/location'
19
- require 'quandl/client/models/scraper'
20
- require 'quandl/client/models/report'
21
- require 'quandl/client/models/superset'
22
-
23
- module Quandl
24
- module Client
25
-
26
- class << self
27
-
28
- attr_accessor :request_source, :request_version, :request_platform
29
-
30
- def request_source
31
- @request_source ||= "quandl_client"
32
- end
33
-
34
- def request_version
35
- @request_version ||= Quandl::Client::VERSION
36
- end
37
-
38
- def request_platform
39
- @request_platform ||= RUBY_PLATFORM
40
- end
41
-
42
- end
43
-
44
- def self.use(url)
45
- Quandl::Client::Base.use(url)
46
- end
47
- def self.token=(value)
48
- Quandl::Client::Base.token = value
49
- end
50
-
51
- HTTP_STATUS_CODES = {100=>"Continue", 101=>"Switching Protocols", 102=>"Processing", 200=>"OK", 201=>"Created", 202=>"Accepted", 203=>"Non-Authoritative Information", 204=>"No Content", 205=>"Reset Content", 206=>"Partial Content", 207=>"Multi-Status", 208=>"Already Reported", 226=>"IM Used", 300=>"Multiple Choices", 301=>"Moved Permanently", 302=>"Found", 303=>"See Other", 304=>"Not Modified", 305=>"Use Proxy", 306=>"Reserved", 307=>"Temporary Redirect", 308=>"Permanent Redirect", 400=>"Bad Request", 401=>"Unauthorized", 402=>"Payment Required", 403=>"Forbidden", 404=>"Not Found", 405=>"Method Not Allowed", 406=>"Not Acceptable", 407=>"Proxy Authentication Required", 408=>"Request Timeout", 409=>"Conflict", 410=>"Gone", 411=>"Length Required", 412=>"Precondition Failed", 413=>"Request Entity Too Large", 414=>"Request-URI Too Long", 415=>"Unsupported Media Type", 416=>"Requested Range Not Satisfiable", 417=>"Expectation Failed", 422=>"Unprocessable Entity", 423=>"Locked", 424=>"Failed Dependency", 425=>"Reserved for WebDAV advanced collections expired proposal", 426=>"Upgrade Required", 427=>"Unassigned", 428=>"Precondition Required", 429=>"Too Many Requests", 430=>"Unassigned", 431=>"Request Header Fields Too Large", 500=>"Internal Server Error", 501=>"Not Implemented", 502=>"Bad Gateway", 503=>"Service Unavailable", 504=>"Gateway Timeout", 505=>"HTTP Version Not Supported", 506=>"Variant Also Negotiates (Experimental)", 507=>"Insufficient Storage", 508=>"Loop Detected", 509=>"Unassigned", 510=>"Not Extended", 511=>"Network Authentication Required"}
52
-
53
- end
1
+ require "quandl/client/version"
2
+
3
+ require 'scope_composer'
4
+ require 'her'
5
+ require 'quandl/her/remove_method_data'
6
+ require 'quandl/her/collection'
7
+ require 'quandl/logger'
8
+ require "quandl/data"
9
+
10
+ require 'quandl/pattern'
11
+ require 'quandl/pattern/client'
12
+
13
+ require 'quandl/client/middleware'
14
+ require 'quandl/client/base'
15
+ require 'quandl/client/models/dataset'
16
+ require 'quandl/client/models/sheet'
17
+ require 'quandl/client/models/source'
18
+ require 'quandl/client/models/user'
19
+ require 'quandl/client/models/location'
20
+ require 'quandl/client/models/scraper'
21
+ require 'quandl/client/models/report'
22
+ require 'quandl/client/models/superset'
23
+
24
+ module Quandl
25
+ module Client
26
+
27
+ class << self
28
+
29
+ attr_accessor :request_source, :request_version, :request_platform
30
+
31
+ def request_source
32
+ @request_source ||= "quandl_client"
33
+ end
34
+
35
+ def request_version
36
+ @request_version ||= Quandl::Client::VERSION
37
+ end
38
+
39
+ def request_platform
40
+ @request_platform ||= RUBY_PLATFORM
41
+ end
42
+
43
+ end
44
+
45
+ def self.use(url)
46
+ Quandl::Client::Base.use(url)
47
+ end
48
+ def self.token=(value)
49
+ Quandl::Client::Base.token = value
50
+ end
51
+
52
+ HTTP_STATUS_CODES = {100=>"Continue", 101=>"Switching Protocols", 102=>"Processing", 200=>"OK", 201=>"Created", 202=>"Accepted", 203=>"Non-Authoritative Information", 204=>"No Content", 205=>"Reset Content", 206=>"Partial Content", 207=>"Multi-Status", 208=>"Already Reported", 226=>"IM Used", 300=>"Multiple Choices", 301=>"Moved Permanently", 302=>"Found", 303=>"See Other", 304=>"Not Modified", 305=>"Use Proxy", 306=>"Reserved", 307=>"Temporary Redirect", 308=>"Permanent Redirect", 400=>"Bad Request", 401=>"Unauthorized", 402=>"Payment Required", 403=>"Forbidden", 404=>"Not Found", 405=>"Method Not Allowed", 406=>"Not Acceptable", 407=>"Proxy Authentication Required", 408=>"Request Timeout", 409=>"Conflict", 410=>"Gone", 411=>"Length Required", 412=>"Precondition Failed", 413=>"Request Entity Too Large", 414=>"Request-URI Too Long", 415=>"Unsupported Media Type", 416=>"Requested Range Not Satisfiable", 417=>"Expectation Failed", 422=>"Unprocessable Entity", 423=>"Locked", 424=>"Failed Dependency", 425=>"Reserved for WebDAV advanced collections expired proposal", 426=>"Upgrade Required", 427=>"Unassigned", 428=>"Precondition Required", 429=>"Too Many Requests", 430=>"Unassigned", 431=>"Request Header Fields Too Large", 500=>"Internal Server Error", 501=>"Not Implemented", 502=>"Bad Gateway", 503=>"Service Unavailable", 504=>"Gateway Timeout", 505=>"HTTP Version Not Supported", 506=>"Variant Also Negotiates (Experimental)", 507=>"Insufficient Storage", 508=>"Loop Detected", 509=>"Unassigned", 510=>"Not Extended", 511=>"Network Authentication Required"}
53
+
54
+ end
54
55
  end
@@ -0,0 +1,18 @@
1
+ module Her
2
+ module Model
3
+ module Attributes
4
+ def self.initialize_collection(klass, parsed_data={})
5
+ arr = klass.extract_array(parsed_data)
6
+ arr = [] unless arr.is_a?(Array)
7
+ collection_data = arr.map do |item_data|
8
+ resource = klass.new(klass.parse(item_data))
9
+ resource.run_callbacks :find
10
+ resource
11
+ end
12
+ Her::Collection.new(collection_data, parsed_data[:metadata], parsed_data[:errors])
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
@@ -1,9 +1,9 @@
1
- module Her
2
- module Model
3
- # remove deprecated data method since cassinatra returns data: []
4
- module DeprecatedMethods
5
- remove_method( :data ) if method_defined?( :data )
6
- remove_method( :data= ) if method_defined?( :data= )
7
- end
8
- end
1
+ module Her
2
+ module Model
3
+ # remove deprecated data method since cassinatra returns data: []
4
+ module DeprecatedMethods
5
+ remove_method( :data ) if method_defined?( :data )
6
+ remove_method( :data= ) if method_defined?( :data= )
7
+ end
8
+ end
9
9
  end
@@ -1,9 +1,9 @@
1
- module Quandl
2
- class Pattern
3
-
4
- define_pattern :dataset_date, /[0-9]{4}\-[0-9]{2}\-[0-9]{2}/, example: "yyyy-mm-dd"
5
- define_pattern :code, /[A-Z0-9_]+/, example: 'ALPHA_NUMERIC_ALL_CAPS'
6
- define_pattern :full_code, /(#{code}+)\/?(#{code})?/, example: "(SOURCE_CODE/)#{code.to_example}"
7
-
8
- end
1
+ module Quandl
2
+ class Pattern
3
+
4
+ define_pattern :dataset_date, /[0-9]{4}\-[0-9]{2}\-[0-9]{2}/, example: "yyyy-mm-dd"
5
+ define_pattern :code, /[A-Z0-9_]+/, example: 'ALPHA_NUMERIC_ALL_CAPS'
6
+ define_pattern :full_code, /(#{code}+)\/?(#{code})?/, example: "(SOURCE_CODE/)#{code.to_example}"
7
+
8
+ end
9
9
  end
@@ -1,38 +1,38 @@
1
- module Quandl
2
- class Pattern < Regexp
3
-
4
- class << self
5
-
6
- def define_pattern(name, pattern, options={})
7
- assert_unique_pattern!(name, pattern)
8
- patterns << name
9
- define_singleton_method(name){ Quandl::Pattern.new( pattern, options ) }
10
- end
11
-
12
- def assert_unique_pattern!(name, pattern)
13
- return false unless self.respond_to?(name)
14
- message = "Attempted to redefine previously defined pattern! '#{name}', /#{pattern}/"
15
- raise ArgumentError.new(name), message
16
- end
17
-
18
- def patterns
19
- @patterns ||= []
20
- end
21
-
22
- end
23
-
24
- def initialize(*args)
25
- @options = args.pop if args.last.is_a?(Hash)
26
- super(*args)
27
- end
28
-
29
- def to_example
30
- options[:example]
31
- end
32
-
33
- def options
34
- @options ||= {}
35
- end
36
-
37
- end
1
+ module Quandl
2
+ class Pattern < Regexp
3
+
4
+ class << self
5
+
6
+ def define_pattern(name, pattern, options={})
7
+ assert_unique_pattern!(name, pattern)
8
+ patterns << name
9
+ define_singleton_method(name){ Quandl::Pattern.new( pattern, options ) }
10
+ end
11
+
12
+ def assert_unique_pattern!(name, pattern)
13
+ return false unless self.respond_to?(name)
14
+ message = "Attempted to redefine previously defined pattern! '#{name}', /#{pattern}/"
15
+ raise ArgumentError.new(name), message
16
+ end
17
+
18
+ def patterns
19
+ @patterns ||= []
20
+ end
21
+
22
+ end
23
+
24
+ def initialize(*args)
25
+ @options = args.pop if args.last.is_a?(Hash)
26
+ super(*args)
27
+ end
28
+
29
+ def to_example
30
+ options[:example]
31
+ end
32
+
33
+ def options
34
+ @options ||= {}
35
+ end
36
+
37
+ end
38
38
  end