class Plyom::PlyomProduct

Public Class Methods

all() click to toggle source
# File lib/plyom_user.rb, line 87
def self.all
  list = []
  response = action_by("read")
  objs = JSON.parse(response.body)
  objs.each { |obj| list << self.new(obj) }
  list
end
find(id) click to toggle source
# File lib/plyom_user.rb, line 95
def self.find(id)
  response = action_by("read", id: id)
  obj = JSON.parse(response.body)
  self.new(obj)
end
new(params={}) click to toggle source
# File lib/plyom_user.rb, line 83
def initialize(params={})
  params.each { |var, val| public_send("#{var}=", val) }
end

Private Class Methods

action_by(action, params={}) click to toggle source
# File lib/plyom_user.rb, line 131
def self.action_by(action, params={})
  auth_token = { "Authorization" => "Token token=\"#{ENV['plyom_user_token']}\"" }

  case action
    when "read"
      HTTParty.get("#{uri}/#{params[:id]}", headers: auth_token)
    when "add"
      HTTParty.post("#{uri}", query: params, headers: auth_token)
    when "del"
      HTTParty.delete("#{uri}/#{params[:id]}", headers: auth_token)
    when "update"
      HTTParty.patch("#{uri}/#{params[:id]}", query: params, headers: auth_token) if params.size > 1
      HTTParty.put("#{uri}/#{params[:id]}", query: params, headers: auth_token) if params.size == 1
  end   
end
uri() click to toggle source
# File lib/plyom_user.rb, line 147
def self.uri
  host = ENV["plyom_user_host"]
  path = "/api/products"
  host + path
end

Public Instance Methods

remove() click to toggle source
# File lib/plyom_user.rb, line 116
def remove
  PlyomUser.action_by("del", id: @id)
end
save() click to toggle source
# File lib/plyom_user.rb, line 101
def save
  param_names = ["name", "status"]
  data = filter_params(param_names)
  if data.length > 0
    paramters = { user: data }
    paramters.update({ id: @id }) if @id.to_i > 0
    action = @id.to_i > 0 ? "update" : "add"
    response = PlyomUser.action_by(action, paramters)
    @id = response['id'] if action == "add"
    response
  else
    nil
  end
end

Private Instance Methods

filter_params(names=[]) click to toggle source
# File lib/plyom_user.rb, line 121
def filter_params(names=[])
  params = {}
  self.instance_variables.each do |var|
      var_name = var.to_s.delete "@"
      var_value = self.instance_variable_get var
      params.update({var_name => var_value}) if names.include?(var_name)
  end
  params
end