module Yao::Resources::RestfullyAccessible
Attributes
Public Class Methods
# 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
Public Instance Methods
@param bool [Boolean] @return [Boolean]
# File lib/yao/resources/restfully_accessible.rb, line 40 def admin=(bool) @admin = bool end
@return [String]
# File lib/yao/resources/restfully_accessible.rb, line 26 def api_version @api_version || '' end
@param v [String] @return [String]
# 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
@param blk [Proc]
# 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
@return [Faraday::Connection]
# 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
@param resource_params [Hash] @return [Yao::Resources::*]
# 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
@param id [String] @return [String]
# File lib/yao/resources/restfully_accessible.rb, line 196 def destroy(id) res = DELETE(create_url(id)) res.body end
# File lib/yao/resources/restfully_accessible.rb, line 164 def find_by_name(name, query={}) list(query.merge({"name" => name})) end
@param response [Hash] @return [String]
# 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
@param id_or_name_or_permalink [Stirng] @param query [Hash] @return [Yao::Resources::*]
# 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
@param id_or_name_or_permalink [Stirng] @param query [Hash] @return [Yao::Resources::*]
# 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
@param query [Hash] @return [Yao::Resources::*] @return [Array<Yao::Resources::*]
# 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 return_single_on_querying && !query.empty? [resource_from_json(res)] else resources_from_json(res) end end
@note .list is defined to keep backward compatibility and will be deprecated
@return [String]
# File lib/yao/resources/restfully_accessible.rb, line 56 def resources_path @resources_path || resources_name end
@param path [String] @return [String]
# File lib/yao/resources/restfully_accessible.rb, line 62 def resources_path=(path) @resources_path = path.sub(%r!^\/!, "") end
@return [Boolean]
# File lib/yao/resources/restfully_accessible.rb, line 45 def return_single_on_querying @return_single_on_querying end
@param bool [Boolean] @return [Boolean]
# File lib/yao/resources/restfully_accessible.rb, line 51 def return_single_on_querying=(bool) @return_single_on_querying = bool end
@param name [String] @return
# File lib/yao/resources/restfully_accessible.rb, line 20 def service=(name) @service = name end
@param id [String] @return [Yao::Resources::*]
# 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
Private Instance Methods
returns pathname of resource URL @param subpath [String] @return [String]
# File lib/yao/resources/restfully_accessible.rb, line 207 def create_url(subpath='') paths = [ api_version, resources_path, subpath ] paths.select{|s| s != ''}.join('/') end
At first, search by ID. If nothing is found, search by name. @param name [String] @param query [Hash] @return [Yao::Resources::*]
# File lib/yao/resources/restfully_accessible.rb, line 245 def get_by_name(name, query={}) # 空またnilの場合listと同じURLにリクエストしてしまい意図しないレスポンスが返ってくる if name.nil? || name.empty? raise Yao::InvalidRequest.new("Invalid request with empty name or nil") end begin GET(create_url(name), query) rescue Yao::ItemNotFound, Yao::NotFound => e item = find_by_name(name).select { |r| r.name == name } if item.size > 1 raise Yao::TooManyItemFonud.new("More than one resource exists with the name '#{name}'") elsif item.size.zero? raise e end GET(create_url(item.first.id), query) end end
@param json [Hash] @return [Yao::Resources::*]
# File lib/yao/resources/restfully_accessible.rb, line 224 def resource_from_json(json) attribute = json[resource_name_in_json] new(attribute) end
@return [String]
# File lib/yao/resources/restfully_accessible.rb, line 213 def resource_name_in_json @_resource_name_in_json ||= resource_name.sub(/^os-/, "").tr("-", "_") end
@param json [Hash] @return [Array<Yao::Resources::*>]
# File lib/yao/resources/restfully_accessible.rb, line 231 def resources_from_json(json) json[resources_name_in_json].map { |attribute| new(attribute) # instance of Yao::Resources::* } end
@return [String]
# File lib/yao/resources/restfully_accessible.rb, line 218 def resources_name_in_json @resources_name_in_json ||= resources_name.sub(/^os-/, "").tr("-", "_") end
# File lib/yao/resources/restfully_accessible.rb, line 237 def uuid?(str) /^[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}$/ === str end