lib/billogram/resource.rb in billogram-0.3.0 vs lib/billogram/resource.rb in billogram-0.3.1
- old
+ new
@@ -13,22 +13,32 @@
end
def search(options = {})
query = DEFAULT_OPTIONS.merge(options)
response = Billogram.client.get("#{endpoint}", {query: query})
- parse_response(response)
+ build_objects(response)
end
def fetch(id)
response = Billogram.client.get("#{endpoint}/#{id}")
- parse_response(response)
+ build_objects(response)
end
- def parse_response(response)
- response.code == 200 ? build_objects(response.parsed_response['data']) : process_error(response)
+ def create(attributes)
+ response = Billogram.client.post("#{endpoint}", {body: attributes.to_json})
+ build_objects(response)
end
+ def update(id, attributes)
+ response = Billogram.client.put("#{endpoint}/#{id}", {body: attributes.to_json})
+ build_objects(response)
+ end
+
+ def delete(id)
+ Billogram.client.put("#{endpoint}/#{id}")
+ end
+
def relation(relation_name, relation_type = :one)
relations[relation_type] << relation_name
attr_accessor relation_name
end
@@ -37,20 +47,24 @@
when Hash then new(data)
when Array then data.map{|item| build_objects(item) }
else data
end
end
-
- def process_error(response)
- raise Billogram::Error.from_response(response)
- end
end
def initialize(attributes = {})
Hash(attributes).each do |key, value|
public_send("#{key}=", value) if respond_to?(key)
end
RelationBuilder.new(self, attributes).call
+ end
+
+ def update(attributes)
+ self.class.update(id, attributes)
+ end
+
+ def delete
+ self.class.delete(id)
end
end
end