Class: FeedTools::Cache::YAML

Inherits:
Object
  • Object
show all
Defined in:
lib/feedtools/cache/yaml.rb,
lib/feedtools/cache/yaml.rb,
lib/feedtools/cache/yaml.rb,
lib/feedtools/cache/yaml/version.rb

Overview

Caching plugin for FeedTools. Cache feched feed as yaml file.

usage

Simple to use, require library and set configuration.

require 'feedtools-cache-yaml'
  or installed by manual
require 'feedtools/cache/yaml'

FeedTools.configurations[:feed_cache] = FeedTools::Cache::YAML

configuration

You can change cache store path. The priority order is the following.

  1. path attribute of FeedTools::Cache::YAML

  2. FEEDTOOLS_CACHE_YAML_PATH environment variable

  3. default ~/.feedtools/cache/yaml

Defined Under Namespace

Modules: VERSION Classes: IDStorePath, NullLogger, StorePath, URLStorePath

Constant Summary collapse

ATTRIBUTES =

Required by FeedTools.

%w(
  id href title link
  feed_data feed_data_type
  http_headers last_retrieved
)
@@path =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeYAML

Create a FeedTools::Cache::YAML instance object.

Returns

cached feed

FeedTools::Cache::YAML : cached feed.



150
151
152
153
154
# File 'lib/feedtools/cache/yaml.rb', line 150

def initialize
  @yaml = {}
  @new_record = true
  @logger = NullLogger.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(message, *args) ⇒ Object

:nodoc:



214
215
216
217
218
219
220
221
222
223
224
# File 'lib/feedtools/cache/yaml.rb', line 214

def method_missing(message, *args) #:nodoc:
  begin
    if matched = /^(.*)=$/.match(message.to_s)
      set_attribute(matched[1], args.first)
    else
      get_attribute(message.to_s)
    end
  rescue NameError
    raise NoMethodError, "undefined method `#{message}' for #{self.to_s}"
  end
end

Instance Attribute Details

#loggerObject

debug logger. Set logger object if use need. Logger output messages when log level is DEBUG.



156
157
158
# File 'lib/feedtools/cache/yaml.rb', line 156

def logger
  @logger
end

Class Method Details

.connected?Boolean

Required by FeedTools. TODO: implement this.

Returns

Currently always true.

Returns:

  • (Boolean)


117
118
119
# File 'lib/feedtools/cache/yaml.rb', line 117

def self.connected?
  true
end

.find_by_href(url) ⇒ Object

Required by FeedTools. Find a cached feed by its url.

Parameters

url

String: feed url.

Returns

cached feed

FeedTools::Cache::YAML: cached feed. If not found, returns nil.



102
103
104
# File 'lib/feedtools/cache/yaml.rb', line 102

def self.find_by_href(url)
  create_from_yaml_cache(path_by_url(url))
end

.find_by_id(id) ⇒ Object

Required by FeedTools. Find a cached feed by its key.

Parameters

id

String: primary key.

Returns

cached feed

FeedTools::Cache::YAML: cached feed. If not found, returns nil.



91
92
93
# File 'lib/feedtools/cache/yaml.rb', line 91

def self.find_by_id(id)
  create_from_yaml_cache(path_by_id(id))
end

.initialize_cacheObject

Required by FeedTools. TODO: implement this.



108
109
110
# File 'lib/feedtools/cache/yaml.rb', line 108

def self.initialize_cache
  # nothing
end

.pathObject

Configured cache store path

Returns

path

String : cache store path you configured. Use FEEDTOOLS_CACHE_YAML_PATH environment variable or default if nil.



134
135
136
# File 'lib/feedtools/cache/yaml.rb', line 134

def self.path
  @@path
end

.path=(path) ⇒ Object

Set cache store path

Parameters

path

String : set cache store path. You can override default path.



142
143
144
# File 'lib/feedtools/cache/yaml.rb', line 142

def self.path=(path)
  @@path = path
end

.set_up_correctly?Boolean

Required by FeedTools. TODO: implement this.

Returns

Currently always true.

Returns:

  • (Boolean)


126
127
128
# File 'lib/feedtools/cache/yaml.rb', line 126

def self.set_up_correctly?
  true
end

Instance Method Details

#idObject

Required by FeedTools.

Primary key.

Returns

id

String : primary key.



174
175
176
# File 'lib/feedtools/cache/yaml.rb', line 174

def id
  @yaml['id']
end

#id=(id) ⇒ Object

Required by FeedTools.

Set primary key.

Parameters

id

String : primary key.



184
185
186
# File 'lib/feedtools/cache/yaml.rb', line 184

def id=(id)
  @yaml['id'] = id
end

#load_cache(path) ⇒ Object

Load data from yaml file.

Parameters

path

String : yaml file path.



203
204
205
206
207
208
209
210
211
212
# File 'lib/feedtools/cache/yaml.rb', line 203

def load_cache(path)
  begin
    @yaml = YAML.load_file(path)
    @new_record = false
    debug "load cache: #{path}"
    self
  rescue
    nil
  end
end

#new_record?Boolean

Required by FeedTools.

Returns whether it is a new object.

Returns

boolean

true/false : returns true if the instance has not been saved yet. Returns false if the instance loaded from cache.

Returns:

  • (Boolean)


164
165
166
# File 'lib/feedtools/cache/yaml.rb', line 164

def new_record?
  @new_record
end

#saveObject

Required by FeedTools.

Save new or update instance.



191
192
193
194
195
196
197
# File 'lib/feedtools/cache/yaml.rb', line 191

def save
  unless id
    save_new_cache
  else
    update_cache
  end
end