Class: Rapid::Services::Requests::UrlHashHandler

Inherits:
Handler show all
Defined in:
lib/ury_rapid/services/requests/url_hash_handler.rb

Overview

A request handler that understands URL and hash-based payloads

This class expects its subclasses to contain two constants, URL_TYPES and HASH_TYPES, mapping URL protocols and hash type identifiers to methods.

Direct Known Subclasses

PlayerHandler, PlaylistHandler

Constant Summary

Constants inherited from Handler

Handler::HOOKS

Class Method Summary collapse

Methods inherited from Handler

add_hook, add_id_hook, delete_by_deleting_children, #initialize, on_delete, post_by_putting_to_child_for, put_by_payload_processor, put_by_posting_to_parent, #run, service_should_override, #to_s, use_payload_processor_for

Methods inherited from Handler

def_targets, #initialize, register_into, #targets, targets?, use_poster

Constructor Details

This class inherits a constructor from Rapid::Services::Requests::Handler

Class Method Details

.hash_type(type, &block) ⇒ Object


77
78
79
# File 'lib/ury_rapid/services/requests/url_hash_handler.rb', line 77

def self.hash_type(type, &block)
  register_type(:hash, type, &block)
end

.merge_types(our_types, their_types) ⇒ Object

Overlays this class's URL/hash types atop of those of the superclass


20
21
22
23
24
25
26
27
28
# File 'lib/ury_rapid/services/requests/url_hash_handler.rb', line 20

def self.merge_types(our_types, their_types)
  types = their_types.dup

  our_types.keys.each do |key|
    types[key] = types.fetch(key, {}).merge(our_types[key] || {})
  end

  types
end

.playlist_reference_type(type, &block) ⇒ Object

Shorthand for creating a type that takes playlist references


52
53
54
55
56
57
58
59
# File 'lib/ury_rapid/services/requests/url_hash_handler.rb', line 52

def self.playlist_reference_type(type, &block)
  url_and_hash_type(
    type,
    ->(url) { parse_playlist_reference_url(url) },
    ->(hash) { parse_playlist_reference_hash(hash) },
    &block
  )
end

.register_type(style, type, &block) ⇒ Object


38
39
40
41
42
# File 'lib/ury_rapid/services/requests/url_hash_handler.rb', line 38

def self.register_type(style, type, &block)
  @types ||= {}
  @types[style] ||= {}
  @types[style][type] = block
end

.superclass_typesObject


30
31
32
# File 'lib/ury_rapid/services/requests/url_hash_handler.rb', line 30

def self.superclass_types
  valid_superclass? ? superclass.types : {}
end

.typesObject

Somewhat hacky way of making URL/hash types propagate to subclasses while not polluting UrlHashHandler with their types.


15
16
17
# File 'lib/ury_rapid/services/requests/url_hash_handler.rb', line 15

def self.types
  merge_types(@types || {}, superclass_types)
end

.url_and_hash_type(type, url_processor, hash_processor, &block) ⇒ Object

Shorthand for creating a type valid in both URL and hash forms


62
63
64
65
66
67
68
69
70
71
# File 'lib/ury_rapid/services/requests/url_hash_handler.rb', line 62

def self.url_and_hash_type(type, url_processor, hash_processor, &block)
  url_type(type) do |url|
    url = instance_exec(url, &url_processor)
    instance_exec(*url, &block)
  end
  hash_type(type) do |hash|
    hash = instance_exec(hash, &hash_processor)
    instance_exec(*hash, &block)
  end
end

.url_type(type, &block) ⇒ Object


73
74
75
# File 'lib/ury_rapid/services/requests/url_hash_handler.rb', line 73

def self.url_type(type, &block)
  register_type(:url, type, &block)
end

.valid_superclass?Boolean

Returns:

  • (Boolean)

34
35
36
# File 'lib/ury_rapid/services/requests/url_hash_handler.rb', line 34

def self.valid_superclass?
  superclass.ancestors.include?(UrlHashHandler)
end