Class: PuppetForge::V3::Base Private

Inherits:
Object
  • Object
show all
Extended by:
Connection
Includes:
LazyAccessors, LazyRelations
Defined in:
lib/puppet_forge/v3/base.rb,
lib/puppet_forge/v3/base/paginated_collection.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Acts as the base class for all PuppetForge::V3::* models.

Direct Known Subclasses

Module, Release, User

Defined Under Namespace

Classes: PaginatedCollection

Constant Summary collapse

API_VERSION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"v3"

Constants included from Connection

Connection::AUTHORIZATION_TOKEN_REGEX, Connection::USER_AGENT

Instance Attribute Summary

Attributes included from Connection

#conn

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Connection

accept_language, accept_language=, authorization, authorization=, default_connection, make_connection, proxy, proxy=

Methods included from LazyRelations

included, #lazy, #lazy_collection, #name, #parent

Methods included from LazyAccessors

#class_name, #fetch, included, #inspect, #method_missing

Constructor Details

#initialize(json_response) ⇒ Base

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Base.



19
20
21
22
# File 'lib/puppet_forge/v3/base.rb', line 19

def initialize(json_response)
  @attributes = json_response
  orm_resp_item json_response
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class PuppetForge::LazyAccessors

Class Method Details

.all(params = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a paginated collection of all modules



125
126
127
# File 'lib/puppet_forge/v3/base.rb', line 125

def all(params = {})
  where(params)
end

.all_stateless(params = {}, conn_opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a paginated collection of all modules



130
131
132
# File 'lib/puppet_forge/v3/base.rb', line 130

def all_stateless(params = {}, conn_opts = {})
  where_stateless(params, conn_opts)
end

.api_versionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



53
54
55
# File 'lib/puppet_forge/v3/base.rb', line 53

def api_version
  API_VERSION
end

.convert_plus_to_space(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Faraday’s Util#escape method will replace a ‘+’ with ‘%2B’ to prevent it being interpreted as a space. For compatibility with the Forge API, we would like a ‘+’ to be interpreted as a space so they are changed to spaces here.



153
154
155
# File 'lib/puppet_forge/v3/base.rb', line 153

def convert_plus_to_space(str)
  str.gsub(/[+]/, ' ')
end

.find(slug) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



101
102
103
# File 'lib/puppet_forge/v3/base.rb', line 101

def find(slug)
  find_request(slug)
end

.find_request(slug, reset_connection = false, conn_opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



93
94
95
96
97
98
99
# File 'lib/puppet_forge/v3/base.rb', line 93

def find_request(slug, reset_connection = false, conn_opts = {})
  return nil if slug.nil?

  resp = request("#{self.name.split("::").last.downcase}s", slug, {}, reset_connection, conn_opts)

  self.new(resp.body)
end

.find_stateless(slug, conn_opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



105
106
107
# File 'lib/puppet_forge/v3/base.rb', line 105

def find_stateless(slug, conn_opts = {})
  find_request(slug, true, conn_opts)
end

.get_collection(uri_path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



142
143
144
# File 'lib/puppet_forge/v3/base.rb', line 142

def get_collection(uri_path)
  get_collection_request(uri_path)
end

.get_collection_request(uri_path, reset_connection = false, conn_opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



135
136
137
138
139
140
# File 'lib/puppet_forge/v3/base.rb', line 135

def get_collection_request(uri_path, reset_connection = false, conn_opts = {})
  resource, params = split_uri_path uri_path
  resp = request(resource, nil, params, reset_connection, conn_opts)

  new_collection(resp)
end

.get_collection_stateless(uri_path, conn_opts) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



146
147
148
# File 'lib/puppet_forge/v3/base.rb', line 146

def get_collection_stateless(uri_path, conn_opts)
  get_collection_request(uri_path, true, conn_opts)
end

.lru_cacheObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
# File 'lib/puppet_forge/v3/base.rb', line 58

def lru_cache
  @lru_cache ||= PuppetForge::LruCache.new
end

.lru_cache_key(*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



63
64
65
# File 'lib/puppet_forge/v3/base.rb', line 63

def lru_cache_key(*args)
  PuppetForge::LruCache.new_key(*args)
end

.new_collection(faraday_resp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



173
174
175
176
177
178
179
# File 'lib/puppet_forge/v3/base.rb', line 173

def new_collection(faraday_resp)
  if faraday_resp[:errors].nil?
    PaginatedCollection.new(self, faraday_resp.body[:results], faraday_resp.body[:pagination], nil)
  else
    PaginatedCollection.new(self)
  end
end

.request(resource, item = nil, params = {}, reset_connection = false, conn_opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/puppet_forge/v3/base.rb', line 68

def request(resource, item = nil, params = {}, reset_connection = false, conn_opts = {})
  cache_key = lru_cache_key(resource, item, params)
  cached = lru_cache.get(cache_key)
  return cached unless cached.nil?

  conn(reset_connection, conn_opts) if reset_connection
  unless conn.url_prefix.to_s =~ /^#{PuppetForge.host}/
    conn.url_prefix = "#{PuppetForge.host}"
  end

  if item.nil?
    uri_path = "v3/#{resource}"
  else
    uri_path = "v3/#{resource}/#{item}"
  end

  # The API expects a space separated string. This allows the user to invoke it with a more natural feeling array.
  params['endorsements'] = params['endorsements'].join(' ') if params['endorsements'].is_a? Array

  result = PuppetForge::V3::Base.conn.get uri_path, params
  lru_cache.put(cache_key, result)
  result
end

.split_uri_path(uri_path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/puppet_forge/v3/base.rb', line 158

def split_uri_path(uri_path)
  all, resource, params = /(?:\/v3\/)([^\/]+)(?:\?)(.*)/.match(uri_path).to_a

  params = convert_plus_to_space(params).split('&')

  param_hash = Hash.new
  params.each do |param|
    key, val = param.split('=')
    param_hash[key] = val
  end

  [resource, param_hash]
end

.where(params) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



116
117
118
# File 'lib/puppet_forge/v3/base.rb', line 116

def where(params)
  where_request(params)
end

.where_request(params, reset_connection = false, conn_opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



110
111
112
113
114
# File 'lib/puppet_forge/v3/base.rb', line 110

def where_request(params, reset_connection = false, conn_opts = {})
  resp = request("#{self.name.split("::").last.downcase}s", nil, params, reset_connection, conn_opts)

  new_collection(resp)
end

.where_stateless(params, conn_opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



120
121
122
# File 'lib/puppet_forge/v3/base.rb', line 120

def where_stateless(params, conn_opts = {})
  where_request(params, true, conn_opts)
end

Instance Method Details

#attribute(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



39
40
41
# File 'lib/puppet_forge/v3/base.rb', line 39

def attribute(name)
  @attributes[:"#{name}"]
end

#attributesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
# File 'lib/puppet_forge/v3/base.rb', line 43

def attributes
  @attributes
end

#has_attribute?(attr) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if attribute exists, false otherwise.

Returns:

  • (Boolean)

    true if attribute exists, false otherwise



35
36
37
# File 'lib/puppet_forge/v3/base.rb', line 35

def has_attribute?(attr)
  @attributes.has_key?(:"#{attr}")
end

#orm_resp_item(json_response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



24
25
26
27
28
29
30
31
# File 'lib/puppet_forge/v3/base.rb', line 24

def orm_resp_item(json_response)
  json_response.each do |key, value|
    unless respond_to? key
      define_singleton_method("#{key}") { @attributes[key] }
      define_singleton_method("#{key}=") { |val| @attributes[key] = val }
    end
  end
end