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) ⇒ Boolean
- #api_version ⇒ String
- #api_version=(v) ⇒ String
- #as_member(&blk) ⇒ Object
- #client ⇒ Faraday::Connection
- #create(resource_params) ⇒ Yao::Resources::*
- #destroy(id) ⇒ String (also: #delete)
- #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 ⇒ String
- #resources_path=(path) ⇒ String
- #return_single_on_querying ⇒ Boolean
- #return_single_on_querying=(bool) ⇒ Boolean
- #update(id, resource_params) ⇒ Yao::Resources::*
Instance Attribute Details
#service ⇒ Object
Returns the value of attribute service.
23 24 25 |
# File 'lib/yao/resources/restfully_accessible.rb', line 23 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) ⇒ Boolean
40 41 42 |
# File 'lib/yao/resources/restfully_accessible.rb', line 40 def admin=(bool) @admin = bool end |
#api_version ⇒ String
26 27 28 |
# File 'lib/yao/resources/restfully_accessible.rb', line 26 def api_version @api_version || '' end |
#api_version=(v) ⇒ String
32 33 34 35 36 |
# File 'lib/yao/resources/restfully_accessible.rb', line 32 def api_version=(v) raise("Set api_version after service is declared") unless service @api_version = v api_version end |
#as_member(&blk) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/yao/resources/restfully_accessible.rb', line 76 def as_member(&blk) if @admin @admin = false result = yield @admin = true result else yield end end |
#client ⇒ Faraday::Connection
67 68 69 70 71 72 73 |
# File 'lib/yao/resources/restfully_accessible.rb', line 67 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::*
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/yao/resources/restfully_accessible.rb', line 161 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 Also known as: delete
187 188 189 190 |
# File 'lib/yao/resources/restfully_accessible.rb', line 187 def destroy(id) res = DELETE(create_url(id)) res.body end |
#find_by_name(name, query = {}) ⇒ Object
155 156 157 |
# File 'lib/yao/resources/restfully_accessible.rb', line 155 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
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/yao/resources/restfully_accessible.rb', line 133 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::*
149 150 151 152 153 |
# File 'lib/yao/resources/restfully_accessible.rb', line 149 def get!(id_or_name_or_permalink, query={}) get(id_or_name_or_permalink, query) rescue Yao::ItemNotFound, Yao::NotFound, Yao::InvalidRequest nil end |
#list(query = {}) ⇒ Yao::Resources::*, Array<Yao::Resources::*] Also known as: list_detail
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/yao/resources/restfully_accessible.rb', line 90 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 memo_query = query res = {} loop do r = GET(url, query).body if r.is_a?(Hash) res.deep_merge!(r) links = r.find {|k,_| k =~ /links/ } if links && links.last.is_a?(Array) && next_link = links.last.find{|s| s["rel"] == "next" } uri = URI.parse(next_link["href"]) query = Hash[URI::decode_www_form(uri.query)] if uri.query next end else res = r end break end if && !query.empty? [resource_from_json(res)] else resources_from_json(res) end end |
#resources_path ⇒ String
56 57 58 |
# File 'lib/yao/resources/restfully_accessible.rb', line 56 def resources_path @resources_path || resources_name end |
#resources_path=(path) ⇒ String
62 63 64 |
# File 'lib/yao/resources/restfully_accessible.rb', line 62 def resources_path=(path) @resources_path = path.sub(%r!^\/!, "") end |
#return_single_on_querying ⇒ Boolean
45 46 47 |
# File 'lib/yao/resources/restfully_accessible.rb', line 45 def end |
#return_single_on_querying=(bool) ⇒ Boolean
51 52 53 |
# File 'lib/yao/resources/restfully_accessible.rb', line 51 def (bool) = bool end |
#update(id, resource_params) ⇒ Yao::Resources::*
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/yao/resources/restfully_accessible.rb', line 174 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 |