Class: Frenetic

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ActiveSupport::Configurable, BrieflyMemoizable
Defined in:
lib/frenetic/briefly_memoizable.rb,
lib/frenetic.rb,
lib/frenetic/errors.rb,
lib/frenetic/version.rb,
lib/frenetic/resource.rb,
lib/frenetic/behaviors.rb,
lib/frenetic/connection.rb,
lib/frenetic/hypermedia_link.rb,
lib/frenetic/concerns/related.rb,
lib/frenetic/resource_mockery.rb,
lib/frenetic/structure_registry.rb,
lib/frenetic/concerns/hal_linked.rb,
lib/frenetic/hypermedia_link_set.rb,
lib/frenetic/middleware/hal_json.rb,
lib/frenetic/resource_collection.rb,
lib/frenetic/concerns/persistence.rb,
lib/frenetic/concerns/member_rest_methods.rb,
lib/frenetic/structure_registry/rebuilder.rb,
lib/frenetic/structure_registry/retriever.rb,
lib/frenetic/concerns/collection_rest_methods.rb,
lib/frenetic/concerns/structure_method_definer.rb,
lib/frenetic/behaviors/alternate_string_identifier.rb

Overview

Allows a resource to be found by a string-based alternative key

For example

module MyClient

module MyResource < Frenetic::Resource
  extend Frenetic::Behaviors::AlternateStringIdentifier

  def self.find(id)
    super(finder_params(id, :username))
  end
end

end

Given an Api Schema such as:

_links: {
  my_resource: [{
    { href: '/api/my_resource/{id', rel: 'id' },
    { href: '/api/my_resource/username?specific_to=username', rel: 'username' },
  }]
}

}

MyClient::MyResource.find will choose the alternate link relation based on the string-based ID passed in.

MyClient::MyResource.find(1) # Executes /api/my_resource/1

MyClient::MyResource.find(‘100’) # Executes /api/my_resource/100

MyClient::MyResource.find(‘jdoe’) Executes /api/my_resource/jdoe?specific_to=username

Defined Under Namespace

Modules: Behaviors, BrieflyMemoizable, CollectionRestMethods, HalLinked, MemberRestMethods, Middleware, Persistence, Related, ResourceMockery, StructureMethodDefiner Classes: ConfigError, Connection, HypermediaLink, HypermediaLinkSet, MissingDependency, MissingRelevantLink, MissingResourceUrl, MissingSchemaDefinition, ParsingError, Resource, ResourceCollection, ResourceInvalid, ResourceNotFound, ResponseError, StructureRegistry, UndefinedResourceMock, UnfulfilledLinkTemplate, UnknownParsingError

Constant Summary collapse

MaxAge =
/max-age=(?<max_age>\d+)/
Error =

Generic Frenetic exception class.

Class.new(StandardError)
HypermediaError =

Raised when there is a Hypermedia error

Class.new(Error)
LinkTemplateError =

Raised when there is a Link Template error

Class.new(Error)
ClientError =

Raised when a network response returns a 400-level error

Class.new(ResponseError)
ServerError =

Raised when a network response returns a 500-level error

Class.new(ResponseError)
ClientParsingError =

Raised when there is a problem parsing the response body of a 400-level error

Class.new(ParsingError)
ServerParsingError =

Raised when there is a problem parsing the response body of a 400-level error

Class.new(ParsingError)
VERSION =
'3.0.1'
@@defaults =

Can’t explicitly use config_accessor because we need defaults and ActiveSupport < 4 does not support them

{
  adapter: Faraday.default_adapter,
  api_token: nil,
  cache: false,
  default_root_cache_age: nil,
  headers: {
    accept: 'application/hal+json',
    user_agent: "Frenetic v#{Frenetic::VERSION}; #{Socket.gethostname}"
  },
  middleware: [],
  password: nil,
  ssl: { verify:true },
  test_mode: false,
  url: nil,
  username: nil
}

Instance Method Summary collapse

Constructor Details

#initialize(cfg = {}) {|config| ... } ⇒ Frenetic

PENDING: [ActiveSupport4] Remove merge with class defaults

Yields:

  • (config)


61
62
63
64
# File 'lib/frenetic.rb', line 61

def initialize(cfg = {})
  config.merge!(cfg.reverse_merge(self.class.config))
  yield config if block_given?
end

Instance Method Details

#configureObject

PENDING: [ActiveSupport4] Use super.



71
72
73
# File 'lib/frenetic.rb', line 71

def configure
  yield(config).tap { reset_connection! }
end

#connectionObject



66
67
68
# File 'lib/frenetic.rb', line 66

def connection
  @connection ||= Connection.new(config)
end

#descriptionObject

Since Frenetic needs to frequently refer to the API design, the result of this method is essentially cached, regardless of what caching middleware it is configured with.

It fully honors the HTTP Cache-Control headers that are returned by the API.

If no Cache-Control header is returned, then the results are not memoized.



82
83
84
85
86
87
# File 'lib/frenetic.rb', line 82

def description
  response = get(config.url.to_s)
  return unless response.success?
  @description_age = cache_control_age(response.headers)
  response.body
end

#reset_connection!Object



98
99
100
# File 'lib/frenetic.rb', line 98

def reset_connection!
  @connection = nil
end

#schemaObject



90
91
92
# File 'lib/frenetic.rb', line 90

def schema
  description.fetch('_embedded', {}).fetch('schema')
end

#structure_registryObject



94
95
96
# File 'lib/frenetic.rb', line 94

def structure_registry
  @structure_registry ||= StructureRegistry.new
end