Class: Pushr::Feedback

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/pushr/feedback.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Feedback

Returns a new instance of Feedback.



11
12
13
14
15
# File 'lib/pushr/feedback.rb', line 11

def initialize(attributes = {})
  attributes.each do |name, value|
    send("#{name}=", value) if respond_to?("#{name}=")
  end
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



5
6
7
# File 'lib/pushr/feedback.rb', line 5

def app
  @app
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/pushr/feedback.rb', line 5

def type
  @type
end

Class Method Details

.create(attributes = {}) ⇒ Object



26
27
28
29
30
# File 'lib/pushr/feedback.rb', line 26

def self.create(attributes = {})
  m = new(attributes)
  m.save
  m
end

.create!(attributes = {}) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/pushr/feedback.rb', line 32

def self.create!(attributes = {})
  m = new(attributes)
  unless m.save
    raise Pushr::Error::RecordInvalid
  end
  m
end

.instantiate(config) ⇒ Object



51
52
53
54
55
# File 'lib/pushr/feedback.rb', line 51

def self.instantiate(config)
  hsh = ::MultiJson.load(config)
  klass = hsh['type'].split('::').reduce(Object) { |a, e| a.const_get e }
  klass.new(hsh)
end

.next(timeout = 3) ⇒ Object



44
45
46
47
48
49
# File 'lib/pushr/feedback.rb', line 44

def self.next(timeout = 3)
  Pushr::Core.redis do |conn|
    feedback = conn.blpop('pushr:feedback', timeout: timeout)
    return instantiate(feedback[1]) if feedback
  end
end

Instance Method Details

#saveObject



17
18
19
20
21
22
23
24
# File 'lib/pushr/feedback.rb', line 17

def save
  if valid?
    Pushr::Core.redis { |conn| conn.rpush('pushr:feedback', to_json) }
    return true
  else
    return false
  end
end

#to_jsonObject



40
41
42
# File 'lib/pushr/feedback.rb', line 40

def to_json
  MultiJson.dump(to_hash)
end