class MagicBell::ApiResource
Attributes
id[R]
response[R]
response_hash[R]
Public Class Methods
create(client, attributes = {})
click to toggle source
# File lib/magicbell/api_resource.rb, line 8 def create(client, attributes = {}) new(client, attributes).create end
create_path()
click to toggle source
# File lib/magicbell/api_resource.rb, line 22 def create_path "/#{name}s" end
create_url()
click to toggle source
# File lib/magicbell/api_resource.rb, line 26 def create_url MagicBell.api_host + create_path end
find(client, id)
click to toggle source
# File lib/magicbell/api_resource.rb, line 12 def find(client, id) api_resource = new(client, "id" => id) api_resource.retrieve api_resource end
name()
click to toggle source
# File lib/magicbell/api_resource.rb, line 18 def name to_s.demodulize.underscore end
new(client, attributes = {})
click to toggle source
# File lib/magicbell/api_resource.rb, line 33 def initialize(client, attributes = {}) @client = client @attributes = attributes @id = @attributes["id"] @retrieved = true if @id end
Public Instance Methods
attribute(attribute_name)
click to toggle source
# File lib/magicbell/api_resource.rb, line 47 def attribute(attribute_name) attributes[attribute_name] end
attributes()
click to toggle source
# File lib/magicbell/api_resource.rb, line 41 def attributes retrieve_unless_retrieved @attributes end
Also aliased as: to_h
create()
click to toggle source
# File lib/magicbell/api_resource.rb, line 78 def create response = @client.post( create_url, body: { name => attributes }.to_json, headers: extra_headers ) parse_response(response) self end
create_path()
click to toggle source
# File lib/magicbell/api_resource.rb, line 66 def create_path "/#{name}s" end
create_url()
click to toggle source
# File lib/magicbell/api_resource.rb, line 62 def create_url MagicBell.api_host + create_path end
name()
click to toggle source
# File lib/magicbell/api_resource.rb, line 58 def name self.class.name end
path()
click to toggle source
# File lib/magicbell/api_resource.rb, line 74 def path "/#{name}s/#{id}" end
retrieve()
click to toggle source
# File lib/magicbell/api_resource.rb, line 51 def retrieve response = @client.get(url) parse_response(response) self end
update(new_attributes = {})
click to toggle source
# File lib/magicbell/api_resource.rb, line 89 def update(new_attributes = {}) response = @client.put( url, body: new_attributes.to_json, headers: extra_headers ) parse_response(response) self end
url()
click to toggle source
# File lib/magicbell/api_resource.rb, line 70 def url MagicBell.api_host + path end
Protected Instance Methods
extra_headers()
click to toggle source
# File lib/magicbell/api_resource.rb, line 102 def extra_headers {} end
Private Instance Methods
parse_response(response)
click to toggle source
# File lib/magicbell/api_resource.rb, line 117 def parse_response(response) @response = response unless response.body.blank? @response_hash = JSON.parse(@response.body) @attributes = @response_hash[name] end @retrieved = true end
retrieve_unless_retrieved()
click to toggle source
# File lib/magicbell/api_resource.rb, line 111 def retrieve_unless_retrieved return unless id # Never retrieve a new unsaved resource return if @retrieved retrieve end