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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all(options = {}) ⇒ Object



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

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

.create(attributes) ⇒ Object



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

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

.destroy(id) ⇒ Object



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

def destroy(id)
  resource delete("/#{plural_name}/#{id}.#{format}")
end

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



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

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

.model_nameObject



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

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

.resource_nameObject

Gets the resource_name



23
24
25
26
27
28
29
30
# File 'lib/rfid_api/client.rb', line 23

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



33
34
35
# File 'lib/rfid_api/client.rb', line 33

def resource_name=(name)
  @resource_name = name
end

.update(id, attributes) ⇒ Object



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

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

Instance Method Details

#datetime(colum_name) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/rfid_api/client.rb', line 145

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

#destroyObject



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

def destroy
  unless new_record?
    self.class.destroy(_id)
    @destroyed = true
  end
end

#destroyed?Boolean

Returns:

  • (Boolean)


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

def destroyed?
  @destroyed == true
end

#errorsObject



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

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

#errors=(attributes) ⇒ Object



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

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

#new_record?Boolean

Returns:

  • (Boolean)


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

def new_record?
  _id.blank?
end

#persisted?Boolean

Returns:

  • (Boolean)


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

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

#saveObject



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

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

#to_keyObject



104
105
106
# File 'lib/rfid_api/client.rb', line 104

def to_key
  [_id]
end

#to_paramObject



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

def to_param
  _id
end