Class: KillBillClient::Model::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/killbill_client/models/resource.rb

Constant Summary collapse

KILLBILL_API_PREFIX =
'/1.0/kb/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#uriObject

Set on create call



99
100
101
# File 'lib/killbill_client/models/resource.rb', line 99

def uri
  @uri
end

Class Method Details

.attribute(name) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/killbill_client/models/resource.rb', line 82

def attribute(name)
  self.send('attr_accessor', name.to_sym)

  self.instance_variable_set('@json_attributes', []) unless self.instance_variable_get('@json_attributes')
  self.instance_variable_get('@json_attributes') << name.to_s

  attributes = self.instance_variable_get('@json_attributes')
  (
  class << self;
    self
  end).send(:define_method, :json_attributes) do
    attributes
  end
end

.delete(uri, params = {}, options = {}, clazz = self) ⇒ Object



30
31
32
33
# File 'lib/killbill_client/models/resource.rb', line 30

def delete(uri, params = {}, options = {}, clazz = self)
  response = KillBillClient::API.delete uri, params, options
  from_response clazz, response
end

.from_json(resource_class, json) ⇒ Resource

Instantiates a record from a JSON blob.

Parameters:

  • resource_class (Resource)
  • json (String)

Returns:

See Also:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/killbill_client/models/resource.rb', line 60

def from_json(resource_class, json)
  # e.g. DELETE
  return nil if json.nil? or json.size == 0

  data = JSON.parse json
  if data.is_a? Array
    records = []
    data.each do |data_element|
      records << instantiate_record_from_json(resource_class, data_element)
    end
    records
  else
    instantiate_record_from_json(resource_class, data)
  end
end

.from_response(resource_class, response) ⇒ Resource

Instantiates a record from an HTTP response, setting the record’s response attribute in the process.

Parameters:

  • resource_class (Resource)
  • response (Net::HTTPResponse)

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/killbill_client/models/resource.rb', line 41

def from_response(resource_class, response)
  case response['Content-Type']
    when %r{application/pdf}
      response.body
    when %r{application/json}
      record = from_json resource_class, response.body
      record.instance_eval { @etag, @response = response['ETag'], response }
      record
    else
      raise ArgumentError, "#{response['Content-Type']} is not supported by the library"
  end
end

.get(uri, params = {}, options = {}, clazz = self) ⇒ Object



15
16
17
18
# File 'lib/killbill_client/models/resource.rb', line 15

def get(uri, params = {}, options = {}, clazz = self)
  response = KillBillClient::API.get uri, params, options
  from_response clazz, response
end

.head(uri, params = {}, options = {}, clazz = self) ⇒ Object



10
11
12
13
# File 'lib/killbill_client/models/resource.rb', line 10

def head(uri, params = {}, options = {}, clazz = self)
  response = KillBillClient::API.head uri, params, options
  from_response clazz, response
end

.instantiate_record_from_json(resource_class, data) ⇒ Object



76
77
78
79
80
# File 'lib/killbill_client/models/resource.rb', line 76

def instantiate_record_from_json(resource_class, data)
  record = resource_class.send :new
  data.each { |k, v| record.send("#{Utils.underscore k}=", v) }
  record
end

.post(uri, body = nil, params = {}, options = {}, clazz = self) ⇒ Object



20
21
22
23
# File 'lib/killbill_client/models/resource.rb', line 20

def post(uri, body = nil, params = {}, options = {}, clazz = self)
  response = KillBillClient::API.post uri, body, params, options
  from_response clazz, response
end

.put(uri, body = nil, params = {}, options = {}, clazz = self) ⇒ Object



25
26
27
28
# File 'lib/killbill_client/models/resource.rb', line 25

def put(uri, body = nil, params = {}, options = {}, clazz = self)
  response = KillBillClient::API.put uri, body, params, options
  from_response clazz, response
end

Instance Method Details

#refresh(clazz = self.class) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/killbill_client/models/resource.rb', line 116

def refresh(clazz=self.class)
  if @uri
    self.class.get @uri, {}, {}, clazz
  else
    self
  end
end

#to_hashObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/killbill_client/models/resource.rb', line 101

def to_hash
  json_hash = {}
  self.class.json_attributes.each do |name|
    value = self.send(name)
    if value
      json_hash[Utils.camelize name, :lower] = value.is_a?(Resource) ? value.to_hash : value
    end
  end
  json_hash
end

#to_jsonObject



112
113
114
# File 'lib/killbill_client/models/resource.rb', line 112

def to_json
  to_hash.to_json
end