Class: Interpol::Endpoint

Inherits:
Object
  • Object
show all
Includes:
HashFetcher
Defined in:
lib/interpol/endpoint.rb

Overview

Represents an endpoint. Instances of this class are constructed based on the endpoint definitions in the YAML files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashFetcher

#fetch_from

Constructor Details

#initialize(endpoint_hash, configuration = Interpol.default_configuration) ⇒ Endpoint

Returns a new instance of Endpoint.



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/interpol/endpoint.rb', line 74

def initialize(endpoint_hash, configuration = Interpol.default_configuration)
  @name        = fetch_from(endpoint_hash, 'name')
  @route       = fetch_from(endpoint_hash, 'route')
  @method      = fetch_from(endpoint_hash, 'method').downcase.to_sym

  @configuration   = configuration
  @custom_metadata = endpoint_hash.fetch('meta') { {} }

  @definitions_hash, @all_definitions = extract_definitions_from(endpoint_hash)

  validate_name!
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



72
73
74
# File 'lib/interpol/endpoint.rb', line 72

def configuration
  @configuration
end

#custom_metadataObject (readonly)

Returns the value of attribute custom_metadata.



72
73
74
# File 'lib/interpol/endpoint.rb', line 72

def 
  @custom_metadata
end

#methodObject (readonly)

Returns the value of attribute method.



72
73
74
# File 'lib/interpol/endpoint.rb', line 72

def method
  @method
end

#nameObject (readonly)

Returns the value of attribute name.



72
73
74
# File 'lib/interpol/endpoint.rb', line 72

def name
  @name
end

#routeObject (readonly)

Returns the value of attribute route.



72
73
74
# File 'lib/interpol/endpoint.rb', line 72

def route
  @route
end

Instance Method Details

#available_request_versionsObject



104
105
106
# File 'lib/interpol/endpoint.rb', line 104

def available_request_versions
  available_versions_matching &:request?
end

#available_response_versionsObject



108
109
110
# File 'lib/interpol/endpoint.rb', line 108

def available_response_versions
  available_versions_matching &:response?
end

#definitionsObject



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/interpol/endpoint.rb', line 112

def definitions
  # sort all requests before all responses
  # sort higher version numbers before lower version numbers
  @sorted_definitions ||= @all_definitions.sort do |x, y|
    if x.message_type == y.message_type
      y.version <=> x.version
    else
      x.message_type <=> y.message_type
    end
  end
end

#find_definition!(version, message_type) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/interpol/endpoint.rb', line 87

def find_definition!(version, message_type)
  defs = find_definitions(version, message_type) do
    message = "No definition found for #{name} endpoint for version #{version}"
    message << " and message_type #{message_type}"
    raise NoEndpointDefinitionFoundError.new(message)
  end

  return defs.first if defs.size == 1

  raise MultipleEndpointDefinitionsFoundError, "#{defs.size} endpoint definitions " +
    "were found for #{name} / #{version} / #{message_type}"
end

#find_definitions(version, message_type, &block) ⇒ Object



100
101
102
# File 'lib/interpol/endpoint.rb', line 100

def find_definitions(version, message_type, &block)
  @definitions_hash.fetch([message_type, version], &block)
end

#inspectObject Also known as: to_s



128
129
130
# File 'lib/interpol/endpoint.rb', line 128

def inspect
  "#<#{self.class.name} #{method} #{route} (#{name})>"
end

#route_matches?(path) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/interpol/endpoint.rb', line 124

def route_matches?(path)
  !!(path =~ route_regex)
end