Module: Yao::Resources::RestfullyAccessible

Included in:
Base
Defined in:
lib/yao/resources/restfully_accessible.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#serviceObject

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_versionObject



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

#clientObject



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::*

Parameters:

  • resource_params (Hash)

Returns:



146
147
148
149
150
151
152
153
154
155
# File 'lib/yao/resources/restfully_accessible.rb', line 146

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

Parameters:

  • id (String)

Returns:

  • (String)


172
173
174
175
# File 'lib/yao/resources/restfully_accessible.rb', line 172

def destroy(id)
  res = DELETE(create_url(id))
  res.body
end

#find_by_name(name, query = {}) ⇒ Object



140
141
142
# File 'lib/yao/resources/restfully_accessible.rb', line 140

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

Parameters:

  • id_or_name_or_permalink (Stirng)
  • query (Hash) (defaults to: {})

Returns:



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/yao/resources/restfully_accessible.rb', line 118

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::*

Parameters:

  • id_or_name_or_permalink (Stirng)
  • query (Hash) (defaults to: {})

Returns:



134
135
136
137
138
# File 'lib/yao/resources/restfully_accessible.rb', line 134

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

Parameters:

  • query (Hash) (defaults to: {})

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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
  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 return_single_on_querying && !query.empty?
    [resource_from_json(res)]
  else
    resources_from_json(res)
  end
end

#resources_pathObject



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_queryingObject



37
38
39
# File 'lib/yao/resources/restfully_accessible.rb', line 37

def return_single_on_querying
  @return_single_on_querying
end

#return_single_on_querying=(bool) ⇒ Object



41
42
43
# File 'lib/yao/resources/restfully_accessible.rb', line 41

def return_single_on_querying=(bool)
  @return_single_on_querying = bool
end

#update(id, resource_params) ⇒ Yao::Resources::*

Parameters:

  • id (String)

Returns:



159
160
161
162
163
164
165
166
167
168
# File 'lib/yao/resources/restfully_accessible.rb', line 159

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