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:



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

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

.find(options = {}) ⇒ Object



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

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



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

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




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

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

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



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

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




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

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



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

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

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



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

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

#persisted?Boolean


Active Model


Returns:

  • (Boolean)


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

def persisted?
  false
end

#resource_nameObject



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

def resource_name
  self.class.resource_name
end

#update(options = {}) ⇒ Object



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

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