Class: Snitch::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/snitch/service.rb

Overview

Service is the base class for all services. All services should inherit from this class.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Service

Returns a new instance of Service.



24
25
26
# File 'lib/snitch/service.rb', line 24

def initialize(attributes = {})
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Uses method missing to return the value of a key in the attributes hash. Allows for doing this…

service.

instead of this…

service.attributes[:login]


36
37
38
39
40
41
42
# File 'lib/snitch/service.rb', line 36

def method_missing(method, *args, &block)
  if method.to_s =~ /=$/
    @attributes[method.to_s.chop] = args[0]
  else
    attributes[method.to_s] || attributes[method] || super
  end
end

Class Attribute Details

.message_lengthObject

Set up the class instance attribute for the commit message length



9
10
11
# File 'lib/snitch/service.rb', line 9

def message_length
  @message_length
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/snitch/service.rb', line 4

def attributes
  @attributes
end

#baseObject (readonly)

Returns the value of attribute base.



4
5
6
# File 'lib/snitch/service.rb', line 4

def base
  @base
end

Class Method Details

.new_from_name(s, attributes) ⇒ Object

Handy for creating a new instance of a service from the config file. Simply pass in the service name and the attributes for the service and you get a new instance of the service.

Snitch::Service.new_from_name(:twitter, {:login => 'jnunemaker', :password => 'secret'})
# => #<Snitch::Services::Twitter:0x15a6508 @attributes={:login=>"jnunemaker", :password=>"secret"}>


18
19
20
21
# File 'lib/snitch/service.rb', line 18

def new_from_name(s, attributes)
  service = "Snitch::Services::#{s.to_s.camelize}".constantize
  service.new(attributes)
end