Class: Moneta::Adapters::RestClient

Inherits:
Object
  • Object
show all
Includes:
Defaults
Defined in:
lib/moneta/adapters/restclient.rb

Overview

Moneta rest client backend which works together with ‘Rack::MonetaRest`

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #decrement, #fetch, #increment

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ RestClient

Returns a new instance of RestClient.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :url (String)

    URL

Raises:

  • (ArgumentError)


12
13
14
15
16
17
# File 'lib/moneta/adapters/restclient.rb', line 12

def initialize(options = {})
  raise ArgumentError, 'Option :url is required' unless url = options[:url]
  url = URI(url)
  @path = url.path
  @client = ::Net::HTTP.start(url.host, url.port)
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

  • options (Hash) (defaults to: {})


45
46
47
48
# File 'lib/moneta/adapters/restclient.rb', line 45

def clear(options = {})
  @client.request(::Net::HTTP::Delete.new(@path))
  self
end

#closeObject



50
51
52
53
# File 'lib/moneta/adapters/restclient.rb', line 50

def close
  @client.finish
  nil
end

#delete(key, options = {}) ⇒ Object

Delete the key from the store and return the current value

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See ‘Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See ‘Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



39
40
41
42
# File 'lib/moneta/adapters/restclient.rb', line 39

def delete(key, options = {})
  response = @client.request(::Net::HTTP::Delete.new(@path + key))
  response.code == '200' ? response.body : nil
end

#key?(key, options = {}) ⇒ Boolean

Exists the value with key

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See ‘Moneta::Expires`)

  • :prefix (String)

    Prefix key (See ‘Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/moneta/adapters/restclient.rb', line 20

def key?(key, options = {})
  response = @client.request_head(@path + key)
  response.code == '200'
end

#load(key, options = {}) ⇒ Object

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See ‘Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See ‘Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See ‘Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



26
27
28
29
# File 'lib/moneta/adapters/restclient.rb', line 26

def load(key, options = {})
  response = @client.request_get(@path + key)
  response.code == '200' ? response.body : nil
end

#store(key, value, options = {}) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See ‘Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See ‘Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See ‘Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value



32
33
34
35
36
# File 'lib/moneta/adapters/restclient.rb', line 32

def store(key, value, options = {})
  response = @client.request_post(@path + key, value)
  raise "HTTP error #{response.code}" unless response.code == '200'
  value
end