Class: SimpleFeed::Providers::Key

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/simplefeed/providers/key.rb

Overview

Here is a meta key for a given user ID:

   user   'm' for meta
     

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_id, key_template) ⇒ Key

Returns a new instance of Key.



26
27
28
29
30
31
# File 'lib/simplefeed/providers/key.rb', line 26

def initialize(user_id, key_template)
  self.user_id = user_id
  self.key_template = key_template

  define_key_methods
end

Instance Attribute Details

#key_templateObject

Returns the value of attribute key_template.



21
22
23
# File 'lib/simplefeed/providers/key.rb', line 21

def key_template
  @key_template
end

#user_idObject

Returns the value of attribute user_id.



21
22
23
# File 'lib/simplefeed/providers/key.rb', line 21

def user_id
  @user_id
end

Instance Method Details

#base62_user_idObject



46
47
48
49
50
51
52
# File 'lib/simplefeed/providers/key.rb', line 46

def base62_user_id
  @base62_user_id ||= if user_id.is_a?(Numeric)
                        ::Base62.encode(user_id)
                      else
                        rot13(user_id.to_s)
                      end
end

#define_key_methodsObject

Defines #data and #meta methods.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/simplefeed/providers/key.rb', line 34

def define_key_methods
  key_template.key_types.each do |type|
    key_name = type.name
    next if respond_to?(key_name)

    self.class.send(:define_method, key_name) do
      instance_variable_get("@#{key_name}") ||
        instance_variable_set("@#{key_name}", type.render(render_options))
    end
  end
end

#inspectObject



69
70
71
# File 'lib/simplefeed/providers/key.rb', line 69

def inspect
  render_options.inspect
end

#keysObject



54
55
56
# File 'lib/simplefeed/providers/key.rb', line 54

def keys
  key_names.map { |name| send(name) }
end

#render_optionsObject



58
59
60
61
62
63
# File 'lib/simplefeed/providers/key.rb', line 58

def render_options
  key_template.render_options.merge!({
                                       'user_id' => user_id,
                                       'base62_user_id' => base62_user_id
                                     })
end

#to_sObject



65
66
67
# File 'lib/simplefeed/providers/key.rb', line 65

def to_s
  super + { user_id: user_id, base62_user_id: base62_user_id, keys: keys }.to_s
end