Class: SuggestGrid::Action

Inherits:
BaseModel show all
Defined in:
lib/suggest_grid/models/action.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#method_missing, #respond_to?, #to_hash, #to_json

Constructor Details

#initialize(type = nil, user_id = nil, item_id = nil, rating = nil, timestamp = nil, additional_properties = {}) ⇒ Action

Returns a new instance of Action.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/suggest_grid/models/action.rb', line 38

def initialize(type = nil,
               user_id = nil,
               item_id = nil,
               rating = nil,
               timestamp = nil,
               additional_properties = {})
  @type = type
  @user_id = user_id
  @item_id = item_id
  @rating = rating
  @timestamp = timestamp

  # Add additional model properties to the instance
  additional_properties.each {|name, value| instance_variable_set("@#{name}", value)}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SuggestGrid::BaseModel

Instance Attribute Details

#item_idString

The item id of the item the action is performed on.

Returns:



15
16
17
# File 'lib/suggest_grid/models/action.rb', line 15

def item_id
  @item_id
end

#ratingFloat

The optional rating given by the user, if the type is explicit.

Returns:

  • (Float)


19
20
21
# File 'lib/suggest_grid/models/action.rb', line 19

def rating
  @rating
end

#timestampLong

The optional UNIX epoch timestamp of the action. Defaults to the current timestamp.

Returns:

  • (Long)


23
24
25
# File 'lib/suggest_grid/models/action.rb', line 23

def timestamp
  @timestamp
end

#typeString

The type that the action belongs to.

Returns:



7
8
9
# File 'lib/suggest_grid/models/action.rb', line 7

def type
  @type
end

#user_idString

The user id of the performer of the action.

Returns:



11
12
13
# File 'lib/suggest_grid/models/action.rb', line 11

def user_id
  @user_id
end

Class Method Details

.from_hash(hash) ⇒ Object

Creates an instance of the object from a hash



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/suggest_grid/models/action.rb', line 55

def self.from_hash(hash)
  if hash == nil
    nil
  else
    # Extract variables from the hash
    type = hash["type"]
    user_id = hash["user_id"]
    item_id = hash["item_id"]
    rating = hash["rating"]
    timestamp = hash["timestamp"]

    # Clean out expected properties from Hash
    names.values.each {|k| hash.delete(k)}

    # Create object from extracted values
    Action.new(type,
               user_id,
               item_id,
               rating,
               timestamp,
               hash)
  end
end

.namesObject

A mapping from model property names to API property names



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/suggest_grid/models/action.rb', line 26

def self.names
  if @hash.nil?
    @hash = {}
    @hash["type"] = "type"
    @hash["user_id"] = "user_id"
    @hash["item_id"] = "item_id"
    @hash["rating"] = "rating"
    @hash["timestamp"] = "timestamp"
  end
  @hash
end