Class: JSONAPI::Home::V1::Resource

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/jsonapi/home/v1/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route) ⇒ Resource

Returns a new instance of Resource.



32
33
34
# File 'app/models/jsonapi/home/v1/resource.rb', line 32

def initialize(route)
  @route = route
end

Class Method Details

.allObject



13
14
15
# File 'app/models/jsonapi/home/v1/resource.rb', line 13

def self.all
  routes.map(&method(:new)).select(&:valid?)
end

.fetch(id) ⇒ Object



21
22
23
# File 'app/models/jsonapi/home/v1/resource.rb', line 21

def self.fetch(id)
  all.index_by(&:id).fetch(id)
end

.where(attributes) ⇒ Object



17
18
19
# File 'app/models/jsonapi/home/v1/resource.rb', line 17

def self.where(attributes)
  all.where(attributes)
end

Instance Method Details

#created_atObject



90
91
92
# File 'app/models/jsonapi/home/v1/resource.rb', line 90

def created_at
  CREATED_AT
end

#deprecatedObject



56
57
58
59
60
61
62
# File 'app/models/jsonapi/home/v1/resource.rb', line 56

def deprecated
  if configuration.fetch(:deprecated).respond_to?(:fetch)
    configuration.fetch(:deprecated).fetch(action, nil)
  else
    configuration.fetch(:deprecated)
  end
end

#descriptionObject



52
53
54
# File 'app/models/jsonapi/home/v1/resource.rb', line 52

def description
  configuration.fetch(:description)
end

#documentationObject



64
65
66
# File 'app/models/jsonapi/home/v1/resource.rb', line 64

def documentation
  configuration.fetch(:documentation)
end

#hrefObject



76
77
78
# File 'app/models/jsonapi/home/v1/resource.rb', line 76

def href
  File.join(location, path)
end

#idObject



36
37
38
# File 'app/models/jsonapi/home/v1/resource.rb', line 36

def id
  "#{namespace}-#{version}-#{intent}"
end

#intentObject



40
41
42
# File 'app/models/jsonapi/home/v1/resource.rb', line 40

def intent
  defaults.fetch(:action)
end

#jsonapi_versionObject



68
69
70
# File 'app/models/jsonapi/home/v1/resource.rb', line 68

def jsonapi_version
  configuration.fetch(:version)
end

#locationObject



80
81
82
83
84
# File 'app/models/jsonapi/home/v1/resource.rb', line 80

def location
  configuration.fetch(:location) ||
  ENV.fetch("HOME_LOCATION", nil) ||
  Rails.application.routes.url_helpers.root_path
end

#mediatypeObject



86
87
88
# File 'app/models/jsonapi/home/v1/resource.rb', line 86

def mediatype
  JSONAPI::MEDIA_TYPE
end

#namespaceObject



44
45
46
# File 'app/models/jsonapi/home/v1/resource.rb', line 44

def namespace
  configuration.fetch(:namespace)
end

#updated_atObject



94
95
96
# File 'app/models/jsonapi/home/v1/resource.rb', line 94

def updated_at
  Time.now
end

#valid?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'app/models/jsonapi/home/v1/resource.rb', line 98

def valid?
  !route.internal && defaults.any? && controller.present? && configuration
end

#verbObject



72
73
74
# File 'app/models/jsonapi/home/v1/resource.rb', line 72

def verb
  route.verb
end

#versionObject



48
49
50
# File 'app/models/jsonapi/home/v1/resource.rb', line 48

def version
  configuration.fetch(:version)
end