Class: Spire::API::Resource

Inherits:
Object
  • Object
show all
Includes:
Requestable
Defined in:
lib/spire/api/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Requestable

included

Constructor Details

#initialize(api, data) ⇒ Resource

Returns a new instance of Resource.



48
49
50
51
52
53
54
# File 'lib/spire/api/resource.rb', line 48

def initialize(api, data)
  @api = api
  @client = api.client
  @url = data["url"]
  @capabilities = data["capabilities"]
  @properties = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/spire/api/resource.rb', line 60

def method_missing(name, *args)
  if schema["properties"][name.to_s]
    properties[name.to_s]
  else
    super
  end
end

Instance Attribute Details

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



46
# File 'lib/spire/api/resource.rb', line 46

attr_reader :url, :properties, :capabilities

#propertiesObject (readonly)

Returns the value of attribute properties.



46
47
48
# File 'lib/spire/api/resource.rb', line 46

def properties
  @properties
end

#urlObject (readonly)

Url of the resource



46
47
48
# File 'lib/spire/api/resource.rb', line 46

def url
  @url
end

Instance Method Details

#[](name) ⇒ Object



72
73
74
# File 'lib/spire/api/resource.rb', line 72

def [](name)
  properties[name]
end

#deleteObject



94
95
96
97
98
99
100
# File 'lib/spire/api/resource.rb', line 94

def delete
  response = request(:delete)
  unless response.status == 204
    raise "Error deleting #{self.class.name}: (#{response.status}) #{response.body}"
  end
  true
end

#getObject



76
77
78
79
80
81
82
83
# File 'lib/spire/api/resource.rb', line 76

def get
  response = request(:get)
  unless response.status == 200
    raise "Error retrieving #{self.class.name}: (#{response.status}) #{response.body}"
  end
  @properties = response.data
  self
end

#keyObject



56
57
58
# File 'lib/spire/api/resource.rb', line 56

def key
  properties["key"]
end

#media_typeObject



106
107
108
# File 'lib/spire/api/resource.rb', line 106

def media_type
  schema["mediaType"]
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/spire/api/resource.rb', line 68

def respond_to?(name)
  schema["properties"][name.to_s] || super
end

#schemaObject



102
103
104
# File 'lib/spire/api/resource.rb', line 102

def schema
  @api.schema[resource_name]
end

#update(properties) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/spire/api/resource.rb', line 85

def update(properties)
  response = request(:update, properties)
  unless response.status == 200
    raise "Error updating #{self.class.name}: (#{response.status}) #{response.body}"
  end
  @properties = response.data
  self
end