Class: Trackerific::Services::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/trackerific/services/base.rb

Direct Known Subclasses

FedEx, MockService, UPS, USPS

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

.configObject

Returns the value of attribute config.



4
5
6
# File 'lib/trackerific/services/base.rb', line 4

def config
  @config
end

.nameObject

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

Note:

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

Parameters:

  • id (String)

    The package ID

Returns:

  • (Boolean)

    true when this service can track the given ID



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

Parameters:

  • service_type (Symbol)

    The module name for the 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

Yields:

  • (self.config = OpenStruct.new)


8
9
10
# File 'lib/trackerific/services/base.rb', line 8

def configure(&block)
  yield self.config = OpenStruct.new
end

.credentialsHash

Reads the credentials from Trackerific.config

Returns:

  • (Hash)

    The service’s credentials



37
38
39
# File 'lib/trackerific/services/base.rb', line 37

def credentials
  Trackerific.config[name]
end

.package_id_matchersObject

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, options={})
  concerns(options[:as]) if options[: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

Parameters:

  • id

    The package identifier

Returns:

  • Either a Trackerific::Details or Trackerific::Error



31
32
33
# File 'lib/trackerific/services/base.rb', line 31

def track(id)
  new.track(id)
end

Instance Method Details

#configObject



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