Class: Routemaster::Resources::RestResource

Inherits:
Object
  • Object
show all
Defined in:
lib/routemaster/resources/rest_resource.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, client: nil) ⇒ RestResource

Returns a new instance of RestResource.



10
11
12
13
# File 'lib/routemaster/resources/rest_resource.rb', line 10

def initialize(url, client: nil)
  @url_template = Addressable::Template.new(url)
  @client = client || Routemaster::APIClient.new
end

Instance Method Details

#create(params) ⇒ Object



15
16
17
# File 'lib/routemaster/resources/rest_resource.rb', line 15

def create(params)
  @client.post(expanded_url, body: params)
end

#destroy(id = nil) ⇒ Object



36
37
38
39
# File 'lib/routemaster/resources/rest_resource.rb', line 36

def destroy(id=nil)
  # no response wrapping as DELETE is supposed to 204.
  @client.delete(expanded_url(id: id))
end

#index(params: {}, filters: {}, enable_caching: false) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/routemaster/resources/rest_resource.rb', line 23

def index(params: {}, filters: {}, enable_caching: false)
  @client.get(
    expanded_url, params: params.merge(filters), options: {
      enable_caching: enable_caching,
      response_class: Responses::HateoasEnumerableResponse
    }
  )
end

#show(id = nil, enable_caching: true) ⇒ Object



19
20
21
# File 'lib/routemaster/resources/rest_resource.rb', line 19

def show(id=nil, enable_caching: true)
  @client.get(expanded_url(id: id), options: { enable_caching: enable_caching })
end

#update(id = nil, params) ⇒ Object



32
33
34
# File 'lib/routemaster/resources/rest_resource.rb', line 32

def update(id=nil, params)
  @client.patch(expanded_url(id: id), body: params)
end

#urlObject



41
42
43
# File 'lib/routemaster/resources/rest_resource.rb', line 41

def url
  @url_template.pattern
end