class Trackerific::Services::Base
Attributes
Public Class Methods
Checks if the given package ID can be tracked by this service @param [String] id The package ID @return [Boolean] true when this service can track the given ID @note This will always be false if no credentials were found for the service in Trackerific.config
# 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
Includes the service concerns for the given service type @param [Symbol] service_type The module name for the service type @api semipublic
# File lib/trackerific/services/base.rb, line 15 def concerns(service_type) self.send :include, "Trackerific::Services::Concerns::#{service_type}".constantize end
Configures the service @api semipublic
# File lib/trackerific/services/base.rb, line 8 def configure(&block) yield self.config = OpenStruct.new end
Reads the credentials from Trackerific.config @return [Hash] The service's credentials
# File lib/trackerific/services/base.rb, line 37 def credentials Trackerific.config[name] end
# 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
An Array of Regexp that matches valid package ids for the service @api semipublic
# File lib/trackerific/services/base.rb, line 54 def package_id_matchers config.package_id_matchers end
Registers the service with Trackerific
@api semipublic
# File lib/trackerific/services/base.rb, line 22 def register(name, options={}) concerns(options[:as]) if options[:as].present? self.name = name.to_sym Trackerific::Services[self.name] = self end
Creates a new instance and calls track
with the given id @param id The package identifier @return Either a Trackerific::Details
or Trackerific::Error
# File lib/trackerific/services/base.rb, line 31 def track(id) new.track(id) end
Public Instance Methods
# File lib/trackerific/services/base.rb, line 68 def config self.class.config end
# 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
Protected Instance Methods
# File lib/trackerific/services/base.rb, line 79 def builder(id) members = config.builder.members - [:package_id] credentials = @credentials.values_at(*members) credentials << id config.builder.new(*credentials) end