Module: Yao::Resources::RestfullyAccessible
- Included in:
- Base
- Defined in:
- lib/yao/resources/restfully_accessible.rb
Instance Attribute Summary collapse
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#service ⇒ Object
Returns the value of attribute service.
Class Method Summary collapse
Instance Method Summary collapse
- #admin=(bool) ⇒ Object
- #as_member(&blk) ⇒ Object
- #client ⇒ Object
- #create(resource_params) ⇒ Object
- #destroy(id) ⇒ Object
- #find_by_name(name, query = {}) ⇒ Object
- #get(id_or_name_or_permalink, query = {}) ⇒ Object (also: #find)
-
#list(query = {}) ⇒ Object
restful methods.
- #resources_path ⇒ Object
- #resources_path=(path) ⇒ Object
- #return_single_on_querying=(bool) ⇒ Object
- #update(id, resource_params) ⇒ Object
- #with_resources_path(path, &blk) ⇒ Object
Instance Attribute Details
#api_version ⇒ Object
Returns the value of attribute api_version.
31 32 33 |
# File 'lib/yao/resources/restfully_accessible.rb', line 31 def api_version @api_version end |
#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 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 |
#as_member(&blk) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/yao/resources/restfully_accessible.rb', line 57 def as_member(&blk) if @admin @admin = false result = yield @admin = true result else yield end end |
#client ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/yao/resources/restfully_accessible.rb', line 49 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) ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/yao/resources/restfully_accessible.rb', line 104 def create(resource_params) params = { resource_name_in_json => resource_params } res = POST(resources_path) do |req| req.body = params.to_json req.headers['Content-Type'] = 'application/json' end return_resource(resource_from_json(res.body)) end |
#destroy(id) ⇒ Object
126 127 128 129 |
# File 'lib/yao/resources/restfully_accessible.rb', line 126 def destroy(id) res = DELETE([resources_path, id].join("/")) res.body end |
#find_by_name(name, query = {}) ⇒ Object
100 101 102 |
# File 'lib/yao/resources/restfully_accessible.rb', line 100 def find_by_name(name, query={}) list(query.merge({"name" => name})) end |
#get(id_or_name_or_permalink, query = {}) ⇒ Object Also known as: find
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/yao/resources/restfully_accessible.rb', line 87 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([resources_path, id_or_name_or_permalink].join("/"), query) else get_by_name(id_or_name_or_permalink, query) end return_resource(resource_from_json(res.body)) end |
#list(query = {}) ⇒ Object
restful methods
78 79 80 81 82 83 84 85 |
# File 'lib/yao/resources/restfully_accessible.rb', line 78 def list(query={}) json = GET(resources_path, query).body if && !query.empty? return_resource(resource_from_json(json)) else return_resources(resources_from_json(json)) end end |
#resources_path ⇒ Object
41 42 43 |
# File 'lib/yao/resources/restfully_accessible.rb', line 41 def resources_path @resources_path || resources_name end |
#resources_path=(path) ⇒ Object
45 46 47 |
# File 'lib/yao/resources/restfully_accessible.rb', line 45 def resources_path=(path) @resources_path = path.sub(%r!^\/!, "") end |
#return_single_on_querying=(bool) ⇒ Object
37 38 39 |
# File 'lib/yao/resources/restfully_accessible.rb', line 37 def (bool) = bool end |
#update(id, resource_params) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/yao/resources/restfully_accessible.rb', line 115 def update(id, resource_params) params = { resource_name_in_json => resource_params } res = PUT([resources_path, id].join("/")) do |req| req.body = params.to_json req.headers['Content-Type'] = 'application/json' end return_resource(resource_from_json(res.body)) end |
#with_resources_path(path, &blk) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/yao/resources/restfully_accessible.rb', line 68 def with_resources_path(path, &blk) original = @resources_path @resources_path = path result = yield @resources_path = original result end |