Class: RfidApi::Client

Inherits:
OpenStruct
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
HTTParty
Defined in:
lib/rfid_api/client.rb

Direct Known Subclasses

Action, Card, Device, Event, SocialAccount

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil) ⇒ Client

Returns a new instance of Client.



100
101
102
103
104
105
106
# File 'lib/rfid_api/client.rb', line 100

def initialize(hash = nil)
  if hash && (hash[:errors] || hash['errors'])
    self.errors = (hash.delete(:errors) || hash.delete('errors'))
  end
  
  super(hash)
end

Instance Attribute Details

#destroyed=(value) ⇒ Object (writeonly)

Sets the attribute destroyed

Parameters:

  • value

    the value to set the attribute destroyed to.



21
22
23
# File 'lib/rfid_api/client.rb', line 21

def destroyed=(value)
  @destroyed = value
end

Class Method Details

.all(options = {}) ⇒ Object



50
51
52
# File 'lib/rfid_api/client.rb', line 50

def all(options = {})
  resources get("/#{plural_name}.#{format}", :query => options)
end

.create(attributes) ⇒ Object



40
41
42
43
# File 'lib/rfid_api/client.rb', line 40

def create(attributes)
  response = post("/#{plural_name}.#{format}", :body => { singular_name => attributes })
  resource(response, attributes)
end

.destroy(id) ⇒ Object



58
59
60
61
62
# File 'lib/rfid_api/client.rb', line 58

def destroy(id)
  record = resource( delete("/#{plural_name}/#{id}.#{format}") )
  record.destroyed = (record && record.valid?)
  record
end

.find(id, options = {}) ⇒ Object



54
55
56
# File 'lib/rfid_api/client.rb', line 54

def find(id, options = {})
  resource get("/#{plural_name}/#{id}.#{format}", :query => options)
end

.model_nameObject



64
65
66
# File 'lib/rfid_api/client.rb', line 64

def model_name
  @_model_name ||= ActiveModel::Name.new(self)
end

.resource_nameObject

Gets the resource_name



26
27
28
29
30
31
32
33
# File 'lib/rfid_api/client.rb', line 26

def resource_name
  # Not using superclass_delegating_reader. See +site+ for explanation
  if defined?(@resource_name)
    @resource_name
  elsif superclass != Object && superclass.proxy
    superclass.proxy.dup.freeze
  end
end

.resource_name=(name) ⇒ Object

Set resource_name



36
37
38
# File 'lib/rfid_api/client.rb', line 36

def resource_name=(name)
  @resource_name = name
end

.update(id, attributes) ⇒ Object



45
46
47
48
# File 'lib/rfid_api/client.rb', line 45

def update(id, attributes)
  response = put("/#{plural_name}/#{id}.#{format}", :body => { singular_name => attributes })
  resource(response, attributes)
end

Instance Method Details

#datetime(colum_name) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/rfid_api/client.rb', line 158

def datetime(colum_name)
  begin
    DateTime.parse(send(colum_name))
  rescue Exception => e
    nil
  end
end

#destroyObject



120
121
122
# File 'lib/rfid_api/client.rb', line 120

def destroy
  self.class.destroy(_id) unless new_record?
end

#destroyed?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/rfid_api/client.rb', line 132

def destroyed?
  @destroyed === true
end

#errorsObject



140
141
142
# File 'lib/rfid_api/client.rb', line 140

def errors
  @errors ||= ActiveModel::Errors.new(self)
end

#errors=(attributes) ⇒ Object



144
145
146
147
148
# File 'lib/rfid_api/client.rb', line 144

def errors=(attributes)
  attributes.each do |key, value|
    [value].flatten.each { |message| errors.add(key, message) }
  end
end

#new_record?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/rfid_api/client.rb', line 124

def new_record?
  _id.blank?
end

#persisted?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/rfid_api/client.rb', line 128

def persisted?
  !(new_record? || destroyed?) && valid?
end

#read_attribute(name) ⇒ Object



150
151
152
# File 'lib/rfid_api/client.rb', line 150

def read_attribute(name)
  @table[name.to_sym]
end

#saveObject



116
117
118
# File 'lib/rfid_api/client.rb', line 116

def save
  new_record? ? create(@table) : update(_id, @table)
end

#to_keyObject



108
109
110
# File 'lib/rfid_api/client.rb', line 108

def to_key
  [_id]
end

#to_paramObject



112
113
114
# File 'lib/rfid_api/client.rb', line 112

def to_param
  _id
end

#valid?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/rfid_api/client.rb', line 136

def valid?
  errors.empty?
end

#write_attribute(name, value) ⇒ Object



154
155
156
# File 'lib/rfid_api/client.rb', line 154

def write_attribute(name, value)
  @table[name.to_sym] = value
end