Class: Twords::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/twords/configuration.rb

Overview

Configuration object for the Twords namespace. One instance to rule them all. All options can be changed with public setter methods. One Twords::Configuration instance is shared across all objects in the Twords namespace. Changing the configuration will affect all objects, even those that are already instantiated. To set app configuration, do not initialize a Twords::Configuration object directly - nothing will happen. Do it through Twords.config(&block).

Examples:

Twords.config do |config|
  config.rejects = %w[my us we an w/ because b/c or are this is from
                      be on the for to and at our of in rt a with &
                      that it by as if was]

  config.range   = 30
  config.up_to { Time.now }
  config.include_hashtags = false
  config.include_uris     = false
  config.include_mentions = false

  config.twitter_client do |twitter|
    twitter.consumer_key        = ENV['TWITTER_CONSUMER_KEY']
    twitter.consumer_secret     = ENV['TWITTER_CONSUMER_SECRET']
    twitter.access_token        = ENV['TWITTER_ACCESS_TOKEN']
    twitter.access_token_secret = ENV['TWITTER_ACCESS_TOKEN_SECRET']
  end
end

See Also:

Constant Summary collapse

DEFAULT_REJECTS =

Default words to ignore. Strings must match exactly and are checked with Array#include?

%w[
  my us we an w/ because
  b/c or are this is from
  be on the for to and at
  our of in rt a with &
  that it by as if was
].freeze
DEFAULT_TWITTER_CONFIG =

Default configuration block to pass to Twords::TwitterClient.new. Feel free to customize the variables in a configuration block of your own, but never hard code the values. Or just make the values available at the default locations.

lambda do |twitter|
  twitter.consumer_key        = ENV['TWITTER_CONSUMER_KEY']
  twitter.consumer_secret     = ENV['TWITTER_CONSUMER_SECRET']
  twitter.access_token        = ENV['TWITTER_ACCESS_TOKEN']
  twitter.access_token_secret = ENV['TWITTER_ACCESS_TOKEN_SECRET']
end
DEFAULT_OPTIONS =

Full set of default options that will be passed to the Configuration object on initialization and reset.

{
  include_uris:     false,
  include_hashtags: false,
  include_mentions: false,
  range:            30,
  client:           TwitterClient.new(&DEFAULT_TWITTER_CONFIG),
  up_to_block:      -> { Time.now },
  rejects:          DEFAULT_REJECTS
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTwords::Configuration

Initializes a new Twords::Configuration object with default configuration.



77
78
79
# File 'lib/twords/configuration.rb', line 77

def initialize
  set_defaults
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



68
69
70
# File 'lib/twords/configuration.rb', line 68

def client
  @client
end

#include_hashtagsObject

Returns the value of attribute include_hashtags.



68
69
70
# File 'lib/twords/configuration.rb', line 68

def include_hashtags
  @include_hashtags
end

#include_mentionsObject

Returns the value of attribute include_mentions.



68
69
70
# File 'lib/twords/configuration.rb', line 68

def include_mentions
  @include_mentions
end

#include_urisObject

Returns the value of attribute include_uris.



68
69
70
# File 'lib/twords/configuration.rb', line 68

def include_uris
  @include_uris
end

#rangeObject

Returns the value of attribute range.



71
72
73
# File 'lib/twords/configuration.rb', line 71

def range
  @range
end

#rejectsObject

Returns the value of attribute rejects.



68
69
70
# File 'lib/twords/configuration.rb', line 68

def rejects
  @rejects
end

#up_to_blockObject (readonly)

Returns the value of attribute up_to_block.



68
69
70
# File 'lib/twords/configuration.rb', line 68

def up_to_block
  @up_to_block
end

Instance Method Details

#reset!Twords::Configuration

Resets all configuration options to “factory” default settings.



86
87
88
# File 'lib/twords/configuration.rb', line 86

def reset!
  tap { set_defaults }
end

#twitter_client(&block) ⇒ Twords::TwitterClient

Configure a new Twords::TwitterClient with a configuration block. If no block is given the existing client is returned unchanged.



95
96
97
98
# File 'lib/twords/configuration.rb', line 95

def twitter_client(&block)
  @client = TwitterClient.new(&block) if block_given?
  @client
end

#up_to(&time_block) ⇒ Proc

Takes a block and stores for lazy evaluation to define the end of the time range being checked. The return value of the block must respond to #to_time and return a Time object when called.

Returns:

  • (Proc)


138
139
140
# File 'lib/twords/configuration.rb', line 138

def up_to(&time_block)
  @up_to_block = time_block
end

#up_to_timeTime

Calls the Proc value of #up_to_block and calls #to_time on the return value. Expects a Time object to be returned.

Returns:

  • (Time)


146
147
148
# File 'lib/twords/configuration.rb', line 146

def up_to_time
  up_to_block.call.to_time
end