class PuppetForge::V3::Base

Acts as the base class for all PuppetForge::V3::* models. This class provides some overrides of behaviors from Her, in addition to convenience methods and abstractions of common behavior.

@api private

Public Class Methods

new_collection(parsed_data) click to toggle source

Overrides Her::Model#new_collection with custom logic for handling the paginated collections produced by the Forge API. These collections are then wrapped in a {PaginatedCollection}, which enables navigation of the paginated dataset.

@api private @api her @param parsed_data [Hash<(:data, :errors)>] the parsed response data @return [PaginatedCollection] the collection

Calls superclass method
# File lib/puppet_forge/v3/base.rb, line 71
def new_collection(parsed_data)
  col = super :data =>     parsed_data[:data][:results] || [],
              :metadata => parsed_data[:data][:pagination] || { limit: 10, total: 0, offset: 0 },
              :errors =>   parsed_data[:errors]

  PaginatedCollection.new(self, col.to_a, col.metadata, col.errors)
end
request(*args) click to toggle source

Overrides Her::Model#request to allow end users to dynamically update both the Forge host being communicated with and the user agent string.

@api private @api her @see Her::Model#request @see PuppetForge.host @see PuppetForge.user_agent

Calls superclass method
# File lib/puppet_forge/v3/base.rb, line 46
def request(*args)
  unless her_api.base_uri =~ /^#{PuppetForge.host}/
    her_api.connection.url_prefix = "#{PuppetForge.host}/v3/"
  end

  her_api.connection.headers[:user_agent] = %W[
    #{PuppetForge.user_agent}
    PuppetForge.gem/#{PuppetForge::VERSION}
    Her/#{Her::VERSION}
    Faraday/#{Faraday::VERSION}
    Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} (#{RUBY_PLATFORM})
  ].join(' ').strip

  super
end

Public Instance Methods

request_path(*args) click to toggle source

Since our data is primarily URI based rather than ID based, we should use our URIs as the #request_path whenever possible.

@api private @api her @see Her::Model::Paths#request_path

Calls superclass method
# File lib/puppet_forge/v3/base.rb, line 93
def request_path(*args)
  if has_attribute? :uri then uri else super end
end