Module: Yao::Resources::RestfullyAccessible
- Included in:
- Base
- Defined in:
- lib/yao/resources/restfully_accessible.rb
Instance Attribute Summary collapse
-
#service ⇒ Object
Returns the value of attribute service.
Class Method Summary collapse
Instance Method Summary collapse
- #admin=(bool) ⇒ Object
- #api_version ⇒ Object
- #api_version=(v) ⇒ Object
- #as_member(&blk) ⇒ Object
- #client ⇒ Object
- #create(resource_params) ⇒ Yao::Resources::*
- #destroy(id) ⇒ String
- #find_by_name(name, query = {}) ⇒ Object
- #get(id_or_name_or_permalink, query = {}) ⇒ Yao::Resources::* (also: #find)
- #get!(id_or_name_or_permalink, query = {}) ⇒ Yao::Resources::*
- #list(query = {}) ⇒ Yao::Resources::*, Array<Yao::Resources::*] (also: #list_detail)
- #resources_path ⇒ Object
- #resources_path=(path) ⇒ Object
- #return_single_on_querying ⇒ Object
- #return_single_on_querying=(bool) ⇒ Object
- #update(id, resource_params) ⇒ Yao::Resources::*
Instance Attribute Details
#service ⇒ Object
Returns the value of attribute service.
21 22 23 |
# File 'lib/yao/resources/restfully_accessible.rb', line 21 def service @service end |
Class Method Details
.extended(base) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/yao/resources/restfully_accessible.rb', line 5 def self.extended(base) base.class_eval do class << self attr_accessor :resource_name, :resources_name, :resources_detail_available extend Forwardable %w(get post put delete).each do |method_name| def_delegator :client, method_name, method_name.upcase end end end end |
Instance Method Details
#admin=(bool) ⇒ Object
33 34 35 |
# File 'lib/yao/resources/restfully_accessible.rb', line 33 def admin=(bool) @admin = bool end |
#api_version ⇒ Object
23 24 25 |
# File 'lib/yao/resources/restfully_accessible.rb', line 23 def api_version @api_version || '' end |
#api_version=(v) ⇒ Object
27 28 29 30 31 |
# File 'lib/yao/resources/restfully_accessible.rb', line 27 def api_version=(v) raise("Set api_version after service is declared") unless service @api_version = v api_version end |
#as_member(&blk) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/yao/resources/restfully_accessible.rb', line 61 def as_member(&blk) if @admin @admin = false result = yield @admin = true result else yield end end |
#client ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/yao/resources/restfully_accessible.rb', line 53 def client if @admin Yao.default_client.admin_pool[service] else Yao.default_client.pool[service] end or raise "You do not have #{@admin ? 'admin' : 'public'} access to the #{service} service" end |
#create(resource_params) ⇒ Yao::Resources::*
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/yao/resources/restfully_accessible.rb', line 131 def create(resource_params) params = { resource_name_in_json => resource_params } res = POST(create_url) do |req| req.body = params.to_json req.headers['Content-Type'] = 'application/json' end resource_from_json(res.body) end |
#destroy(id) ⇒ String
157 158 159 160 |
# File 'lib/yao/resources/restfully_accessible.rb', line 157 def destroy(id) res = DELETE(create_url(id)) res.body end |
#find_by_name(name, query = {}) ⇒ Object
125 126 127 |
# File 'lib/yao/resources/restfully_accessible.rb', line 125 def find_by_name(name, query={}) list(query.merge({"name" => name})) end |
#get(id_or_name_or_permalink, query = {}) ⇒ Yao::Resources::* Also known as: find
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/yao/resources/restfully_accessible.rb', line 103 def get(id_or_name_or_permalink, query={}) res = if id_or_name_or_permalink.start_with?("http://", "https://") GET(id_or_name_or_permalink, query) elsif uuid?(id_or_name_or_permalink) GET(create_url(id_or_name_or_permalink), query) else get_by_name(id_or_name_or_permalink, query) end resource_from_json(res.body) end |
#get!(id_or_name_or_permalink, query = {}) ⇒ Yao::Resources::*
119 120 121 122 123 |
# File 'lib/yao/resources/restfully_accessible.rb', line 119 def get!(id_or_name_or_permalink, query={}) get(id_or_name_or_permalink, query) rescue Yao::ItemNotFound, Yao::NotFound nil end |
#list(query = {}) ⇒ Yao::Resources::*, Array<Yao::Resources::*] Also known as: list_detail
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/yao/resources/restfully_accessible.rb', line 75 def list(query={}) url = if resources_detail_available # If the resource has 'detail', #list tries to GET /${resourece}/detail # For example. # # GET /servers/detail # GET /flavors/detail # create_url('detail') else create_url end json = GET(url, query).body if && !query.empty? [resource_from_json(json)] else resources_from_json(json) end end |
#resources_path ⇒ Object
45 46 47 |
# File 'lib/yao/resources/restfully_accessible.rb', line 45 def resources_path @resources_path || resources_name end |
#resources_path=(path) ⇒ Object
49 50 51 |
# File 'lib/yao/resources/restfully_accessible.rb', line 49 def resources_path=(path) @resources_path = path.sub(%r!^\/!, "") end |
#return_single_on_querying ⇒ Object
37 38 39 |
# File 'lib/yao/resources/restfully_accessible.rb', line 37 def @return_single_on_querying end |
#return_single_on_querying=(bool) ⇒ Object
41 42 43 |
# File 'lib/yao/resources/restfully_accessible.rb', line 41 def (bool) @return_single_on_querying = bool end |
#update(id, resource_params) ⇒ Yao::Resources::*
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/yao/resources/restfully_accessible.rb', line 144 def update(id, resource_params) params = { resource_name_in_json => resource_params } res = PUT(create_url(id)) do |req| req.body = params.to_json req.headers['Content-Type'] = 'application/json' end resource_from_json(res.body) end |