Class: Weeblycloud::CloudResource

Inherits:
Object
  • Object
show all
Defined in:
lib/weeblycloud/cloudresource.rb

Overview

A base resource that all other resources inherit from.

Direct Known Subclasses

Account, Blog, BlogPost, Form, FormEntry, Group, Member, Page, Plan, Site, Theme, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil) ⇒ CloudResource

Returns a new instance of CloudResource.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/weeblycloud/cloudresource.rb', line 10

def initialize(data = nil)
  @client = CloudClient.new
  @properties = {}
  @changed = {}

  # If data isn't provided, make an API call to get it
  if data
    @properties = data
    @got = true
  else
    get()
    @got = true
  end
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



8
9
10
# File 'lib/weeblycloud/cloudresource.rb', line 8

def properties
  @properties
end

Instance Method Details

#[](prop) ⇒ Object

Get a property. Returns nil if the property does not exist.



41
42
43
# File 'lib/weeblycloud/cloudresource.rb', line 41

def [](prop)
  get_property(prop)
end

#getObject

Gets the resources with an API call



56
57
58
59
# File 'lib/weeblycloud/cloudresource.rb', line 56

def get
  @response = @client.get(@endpoint)
  @properties = @response.json
end

#get_property(prop) ⇒ Object

Get a property. Returns nil if the property does not exist.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/weeblycloud/cloudresource.rb', line 26

def get_property(prop)
  begin
    return @properties.fetch(prop)
  raise KeyError
    if @got
      return nil
    else
      get()
      @got = true
      return get_property(prop)
    end
  end
end

#idObject

Returns the ID for the resource object



51
52
53
# File 'lib/weeblycloud/cloudresource.rb', line 51

def id
  raise "Method not implemented."
end

#to_sObject

Returns the properties as a json string



46
47
48
# File 'lib/weeblycloud/cloudresource.rb', line 46

def to_s
  @properties.to_json
end