Class: Pige::Client::Resource

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/pige/client/resource.rb

Direct Known Subclasses

Chunk, Label

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Resource



8
9
10
# File 'lib/pige/client/resource.rb', line 8

def initialize(attributes = {})
  attributes.each { |k,v| send "#{k}=", v }
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#errorsObject

Returns the value of attribute errors.



24
25
26
# File 'lib/pige/client/resource.rb', line 24

def errors
  @errors
end

#idObject

Returns the value of attribute id.



29
30
31
# File 'lib/pige/client/resource.rb', line 29

def id
  @id
end

#source_idObject

Returns the value of attribute source_id.



29
30
31
# File 'lib/pige/client/resource.rb', line 29

def source_id
  @source_id
end

#updated_atObject

Returns the value of attribute updated_at.



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

def updated_at
  @updated_at
end

Class Method Details

.allObject



56
57
58
59
60
# File 'lib/pige/client/resource.rb', line 56

def self.all
  get("#{Pige::Client.box_url}/sources/1/#{resources_name}.json").map do |attributes|
    new attributes[resource_name]
  end
end

.base_urlObject



47
48
49
# File 'lib/pige/client/resource.rb', line 47

def self.base_url
  "#{Pige::Client.box_url}/sources/1/#{resource_name}s"
end

.create(attributes = {}) ⇒ Object



51
52
53
54
# File 'lib/pige/client/resource.rb', line 51

def self.create(attributes = {})
  response = post("#{base_url}.json", :query => { resource_name => attributes })
  new response
end

.destroy(resource) ⇒ Object



79
80
81
# File 'lib/pige/client/resource.rb', line 79

def self.destroy(resource)
  delete "#{base_url}/#{resource.id}.json"
end

.destroy_all(conditions = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/pige/client/resource.rb', line 68

def self.destroy_all(conditions = {})
  resources =
    if older_than = conditions[:older_than]
      older_than(older_than)
    else
      all
    end

  resources.each(&:destroy)
end

.older_than(date) ⇒ Object



62
63
64
65
66
# File 'lib/pige/client/resource.rb', line 62

def self.older_than(date)
  all.select do |resource|
    resource.older_than?(date)
  end
end

.resource_nameObject



39
40
41
# File 'lib/pige/client/resource.rb', line 39

def self.resource_name
  name.downcase.split("::").last
end

.resources_nameObject



43
44
45
# File 'lib/pige/client/resource.rb', line 43

def self.resources_name
  "#{resource_name}s"
end

.time_attribute(*names) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/pige/client/resource.rb', line 12

def self.time_attribute(*names)
  names.each do |time_attribute|
    define_method("#{time_attribute}=") do |value|
      value = Time.parse(value) if String === value
      instance_variable_set "@#{time_attribute}", value
    end
  end
end

Instance Method Details

#destroyObject



35
36
37
# File 'lib/pige/client/resource.rb', line 35

def destroy
  self.class.destroy(self)
end

#older_than?(date) ⇒ Boolean



31
32
33
# File 'lib/pige/client/resource.rb', line 31

def older_than?(date)
  self.created_at < date
end