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
- #find_next_link(response) ⇒ String
- #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::*
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/yao/resources/restfully_accessible.rb', line 170 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
196 197 198 199 |
# File 'lib/yao/resources/restfully_accessible.rb', line 196 def destroy(id) res = DELETE(create_url(id)) res.body end |
#find_by_name(name, query = {}) ⇒ Object
164 165 166 |
# File 'lib/yao/resources/restfully_accessible.rb', line 164 def find_by_name(name, query={}) list(query.merge({"name" => name})) end |
#find_next_link(response) ⇒ String
131 132 133 134 135 136 137 |
# File 'lib/yao/resources/restfully_accessible.rb', line 131 def find_next_link(response) if links = response.find {|k, v| k =~ /links/ && v.is_a?(Array) }&.last links.find{|s| s["rel"] == "next" }&.fetch("href") else response["next"] end end |
#get(id_or_name_or_permalink, query = {}) ⇒ Yao::Resources::* Also known as: find
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/yao/resources/restfully_accessible.rb', line 142 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::*
158 159 160 161 162 |
# File 'lib/yao/resources/restfully_accessible.rb', line 158 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 |
# 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) if next_link = find_next_link(r) uri = URI.parse(next_link) 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 @return_single_on_querying end |
#return_single_on_querying=(bool) ⇒ Boolean
51 52 53 |
# File 'lib/yao/resources/restfully_accessible.rb', line 51 def (bool) @return_single_on_querying = bool end |
#update(id, resource_params) ⇒ Yao::Resources::*
183 184 185 186 187 188 189 190 191 192 |
# File 'lib/yao/resources/restfully_accessible.rb', line 183 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 |