class Trackerific::Services::Base

Attributes

config[RW]
name[RW]

Public Class Methods

can_track?(id) click to toggle source

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
concerns(service_type) click to toggle source

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
configure() { |config = open_struct| ... } click to toggle source

Configures the service @api semipublic

# File lib/trackerific/services/base.rb, line 8
def configure(&block)
  yield self.config = OpenStruct.new
end
credentials() click to toggle source

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
new(credentials=self.class.credentials) click to toggle source
# 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
package_id_matchers() click to toggle source

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
register(name, options={}) click to toggle source

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
track(id) click to toggle source

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

config() click to toggle source
# File lib/trackerific/services/base.rb, line 68
def config
  self.class.config
end
track(id) click to toggle source
# 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

builder(id) click to toggle source
# 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