Class: Supplejack::UserSetRelation

Inherits:
Object
  • Object
show all
Includes:
Request
Defined in:
lib/supplejack/user_set_relation.rb

Overview

The UserSetRelation class provides ActiveRecord like functionality to the relationship between a User object and it’s UserSet objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Request

#delete, #get, #post, #put

Constructor Details

#initialize(user) ⇒ UserSetRelation

Returns a new instance of UserSetRelation.



25
26
27
# File 'lib/supplejack/user_set_relation.rb', line 25

def initialize(user)
  @user = user
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Any method missing on this class is delegated to the UserSet objects array so that the developer can easily execute any Array method on the UserSetRelation

Examples:

user.sets.each ....     => Iterate through the UserSet objects array
user.sets.size          => Get the size of the UserSet objects array


138
139
140
# File 'lib/supplejack/user_set_relation.rb', line 138

def method_missing(method, *args, &block)
  sets.send(method, *args, &block)
end

Instance Attribute Details

#userObject (readonly)

Returns the value of attribute user.



23
24
25
# File 'lib/supplejack/user_set_relation.rb', line 23

def user
  @user
end

Instance Method Details

#allObject

Returns an array of all UserSet objects for the current User



127
128
129
# File 'lib/supplejack/user_set_relation.rb', line 127

def all
  self.sets
end

#build(attributes = {}) ⇒ Supplejack::UserSet

Initializes a new UserSet object and links it to the current User

Parameters:

  • A (Hash)

    hash with the UserSet field names and values

Returns:



87
88
89
90
91
# File 'lib/supplejack/user_set_relation.rb', line 87

def build(attributes={})
  user_set = Supplejack::UserSet.new(attributes)
  user_set.api_key = user.api_key
  user_set
end

#create(attributes = {}) ⇒ Supplejack::UserSet

Build and persist a UserSet object with the provided attributes

Parameters:

  • A (Hash)

    hash with the UserSet field names and values

Returns:



99
100
101
102
103
# File 'lib/supplejack/user_set_relation.rb', line 99

def create(attributes={})
  user_set = self.build(attributes)
  user_set.save
  user_set
end

#fetch_setsArray

Initialize an array of UserSet objects and orders them by priority.

Returns:

  • (Array)

    A Array of Supplejack::UserSet objects



39
40
41
42
43
# File 'lib/supplejack/user_set_relation.rb', line 39

def fetch_sets
  response = sets_response
  sets_array = response["sets"] || []
  @sets = sets_array.map {|attributes| Supplejack::UserSet.new(attributes) }.sort_by { |set| set.priority }
end

#find(user_set_id) ⇒ Supplejack::UserSet

Finds a UserSet object with the provided ID that belongs to the current User

Parameters:

  • A (String)

    24 character string representing the UserSet ID

Returns:



77
78
79
# File 'lib/supplejack/user_set_relation.rb', line 77

def find(user_set_id)
  Supplejack::UserSet.find(user_set_id, self.user.api_key)
end

#order(attribute) ⇒ Array

Return a array of UserSet objects ordered by the priority first and then in ascending order by the specified attribute.

The only exception is for the “updated_at” attribute, for which ignores the priority and orders on descending order.

Parameters:

  • A (Symbol)

    symbol with the attribute to order the UserSet objects on.

Returns:

  • (Array)

    A array of Supplejack::UserSet objects.



115
116
117
118
119
120
121
122
123
# File 'lib/supplejack/user_set_relation.rb', line 115

def order(attribute)
  @sets = sets.sort_by do |set|
    value = set.send(attribute)
    value = value.downcase if value.is_a?(String)
    attribute == :updated_at ? value : [set.priority, value]
  end
  @sets = @sets.reverse if attribute == :updated_at
  @sets
end

#setsObject

Returns an array of UserSet objects and memoizes the array



31
32
33
# File 'lib/supplejack/user_set_relation.rb', line 31

def sets
  @sets ||= self.fetch_sets
end

#sets_responseHash

Execute a GET request to the API to retrieve the user_sets from a User. It caches the response from the API for a day. The cache is invalidated every time any set for the user changes.

Returns:

  • (Hash)

    A hash parsed from a JSON string. See the API Documentation for more details.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/supplejack/user_set_relation.rb', line 51

def sets_response
  params = {}

  if user.use_own_api_key?
    path = "/sets"
    params[:api_key] = user.api_key
  else
    path = "/users/#{user.api_key}/sets"
  end

  if Supplejack.enable_caching
    Rails.cache.fetch(path, expires_in: 1.day) do
      get(path, params)
    end
  else
    get(path, params)
  end
end