Class: Trackerific::Services::Base
- Inherits:
-
Object
- Object
- Trackerific::Services::Base
- Defined in:
- lib/trackerific/services/base.rb
Direct Known Subclasses
FedEx, MockService, UPS, USPS
Class Attribute Summary collapse
-
.config ⇒ Object
Returns the value of attribute config.
-
.name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
-
.can_track?(id) ⇒ Boolean
Checks if the given package ID can be tracked by this service service in Trackerific.config.
-
.concerns(service_type) ⇒ Object
Includes the service concerns for the given service type.
-
.configure {|self.config = OpenStruct.new| ... } ⇒ Object
Configures the service.
-
.credentials ⇒ Hash
Reads the credentials from Trackerific.config.
-
.package_id_matchers ⇒ Object
An Array of Regexp that matches valid package ids for the service.
-
.register(name, options = {}) ⇒ Object
Registers the service with Trackerific.
-
.track(id) ⇒ Object
Creates a new instance and calls #track with the given id.
Instance Method Summary collapse
- #config ⇒ Object
-
#initialize(credentials = self.class.credentials) ⇒ Base
constructor
A new instance of Base.
- #track(id) ⇒ Object
Constructor Details
#initialize(credentials = self.class.credentials) ⇒ Base
Returns a new instance of Base.
59 60 61 62 63 64 65 66 |
# File 'lib/trackerific/services/base.rb', line 59 def initialize(credentials=self.class.credentials) @credentials = credentials if credentials.nil? raise Trackerific::Error, "Missing credentials for #{self.class.name}", caller end end |
Class Attribute Details
.config ⇒ Object
Returns the value of attribute config.
4 5 6 |
# File 'lib/trackerific/services/base.rb', line 4 def config @config end |
.name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/trackerific/services/base.rb', line 3 def name @name end |
Class Method Details
.can_track?(id) ⇒ Boolean
This will always be false if no credentials were found for the
Checks if the given package ID can be tracked by this service service in Trackerific.config
46 47 48 49 50 |
# File 'lib/trackerific/services/base.rb', line 46 def can_track?(id) return false if credentials.nil? package_id_matchers.each {|m| return true if id =~ m } false end |
.concerns(service_type) ⇒ Object
Includes the service concerns for the given service type
15 16 17 18 |
# File 'lib/trackerific/services/base.rb', line 15 def concerns(service_type) self.send :include, "Trackerific::Services::Concerns::#{service_type}".constantize end |
.configure {|self.config = OpenStruct.new| ... } ⇒ Object
Configures the service
8 9 10 |
# File 'lib/trackerific/services/base.rb', line 8 def configure(&block) yield self.config = OpenStruct.new end |
.credentials ⇒ Hash
Reads the credentials from Trackerific.config
37 38 39 |
# File 'lib/trackerific/services/base.rb', line 37 def credentials Trackerific.config[name] end |
.package_id_matchers ⇒ Object
An Array of Regexp that matches valid package ids for the service
54 55 56 |
# File 'lib/trackerific/services/base.rb', line 54 def package_id_matchers config.package_id_matchers end |
.register(name, options = {}) ⇒ Object
Registers the service with Trackerific
22 23 24 25 26 |
# File 'lib/trackerific/services/base.rb', line 22 def register(name, ={}) concerns([:as]) if [:as].present? self.name = name.to_sym Trackerific::Services[self.name] = self end |
.track(id) ⇒ Object
Creates a new instance and calls #track with the given id
31 32 33 |
# File 'lib/trackerific/services/base.rb', line 31 def track(id) new.track(id) end |
Instance Method Details
#config ⇒ Object
68 69 70 |
# File 'lib/trackerific/services/base.rb', line 68 def config self.class.config end |
#track(id) ⇒ Object
72 73 74 75 |
# File 'lib/trackerific/services/base.rb', line 72 def track(id) result = config.parser.new(id, request(id)).parse result.is_a?(Trackerific::Error) ? raise(result) : result end |