Class: VWorkApp::Resource

Inherits:
Base
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations, HTTParty
Defined in:
lib/vworkapp_ruby/base/resource.rb

Direct Known Subclasses

Customer, Job, Worker

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#attributes, #attributes=, #attributes_eql?, hattr_accessor, hattr_reader, hattr_writer, hattrs, #hattrs, #initialize, #read_attributes, #read_attributes=, read_hattrs, #read_hattrs, #validate_and_raise, #write_attributes, #write_attributes=, #write_hattrs, write_hattrs

Constructor Details

This class inherits a constructor from VWorkApp::Base

Class Method Details

.bad_response(raw) ⇒ Object

Raises:



99
100
101
# File 'lib/vworkapp_ruby/base/resource.rb', line 99

def self.bad_response(raw)
  raise ResponseError.new(raw)
end

.find(options = {}) ⇒ Object



61
62
63
64
65
# File 'lib/vworkapp_ruby/base/resource.rb', line 61

def self.find(options = {})
  self.perform(:get, "/#{resource_name.pluralize}.xml", options) do |res|
    res[resource_name.pluralize].map { |h| self.new(h) }
  end
end

.perform(action, url, query = {}, body = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vworkapp_ruby/base/resource.rb', line 78

def self.perform(action, url, query = {}, body = nil)
  options = {}
  options[:query] = { :api_key => VWorkApp.api_key }.merge(query)

  options[:body] = body
  
  raw = self.send(action, url, options)

  case raw.response
  when Net::HTTPOK, Net::HTTPCreated
    yield(raw) if block_given?
  when Net::HTTPNotFound
    nil
  else
    bad_response(raw)
  end
end

.resource_nameObject


Misc Methods




71
72
73
# File 'lib/vworkapp_ruby/base/resource.rb', line 71

def self.resource_name
  @resource_name ||= ActiveSupport::Inflector.demodulize(self).underscore
end

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



55
56
57
58
59
# File 'lib/vworkapp_ruby/base/resource.rb', line 55

def self.show(id, options = {})
  perform(:get, "/#{resource_name.pluralize}/#{id}.xml", options) do |res|
    self.new(res[resource_name])
  end
end

Instance Method Details

#createObject


Rest Methods




39
40
41
42
43
44
# File 'lib/vworkapp_ruby/base/resource.rb', line 39

def create
  validate_and_raise
  perform(:post, "/#{resource_name.pluralize}", {}, self.to_xml) do |res|
    self.class.new(res[resource_name])
  end
end

#delete(options = {}) ⇒ Object



51
52
53
# File 'lib/vworkapp_ruby/base/resource.rb', line 51

def delete(options = {})
  perform(:delete, "/#{resource_name.pluralize}/#{id}.xml", options)
end

#perform(action, url, query = {}, body = nil, &block) ⇒ Object



95
96
97
# File 'lib/vworkapp_ruby/base/resource.rb', line 95

def perform(action, url, query = {}, body = nil, &block)
  self.class.perform(action, url, query, body, &block)
end

#persisted?Boolean


Active Model


Returns:

  • (Boolean)


31
32
33
# File 'lib/vworkapp_ruby/base/resource.rb', line 31

def persisted?
  false
end

#resource_nameObject



74
75
76
# File 'lib/vworkapp_ruby/base/resource.rb', line 74

def resource_name
  self.class.resource_name
end

#update(options = {}) ⇒ Object



46
47
48
49
# File 'lib/vworkapp_ruby/base/resource.rb', line 46

def update(options = {})
  validate_and_raise
  perform(:put, "/#{resource_name.pluralize}/#{id}.xml", options, self.to_xml)
end