Class: Eson::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/eson/request.rb

Overview

A request is a generic object that implements parameter manipulation methods and Esons plugin API. It is always bound to a client.

This class cannot be used directly but must be subclassed by protocol implementations. Proper implementations must respond to ‘#call`.

Examples:

constructing a request by hand

r = protocol::Request.new(protocol::IndexExists, [ResponseParser], client)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, plugins, client) ⇒ Request

Returns a new instance of Request.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/eson/request.rb', line 16

def initialize(api, plugins, client)
  self.api = api
  self.client = client
  self.extend(api)

  Array(plugins).each do |p|
    if pluggable?(api, p, client)
      self.extend(p)
    end
  end
end

Instance Attribute Details

#apiObject

Returns the value of attribute api.



11
12
13
# File 'lib/eson/request.rb', line 11

def api
  @api
end

#caseObject

Returns the value of attribute case.



13
14
15
# File 'lib/eson/request.rb', line 13

def case
  @case
end

#clientObject

Returns the value of attribute client.



11
12
13
# File 'lib/eson/request.rb', line 11

def client
  @client
end

#indexObject

Returns the value of attribute index.



13
14
15
# File 'lib/eson/request.rb', line 13

def index
  @index
end

#indicesObject

Returns the value of attribute indices.



13
14
15
# File 'lib/eson/request.rb', line 13

def indices
  @indices
end

#prettyObject

Returns the value of attribute pretty.



13
14
15
# File 'lib/eson/request.rb', line 13

def pretty
  @pretty
end

#sourceObject

Returns the value of attribute source.



13
14
15
# File 'lib/eson/request.rb', line 13

def source
  @source
end

Instance Method Details

#encode(obj) ⇒ String

Encode any object. If it is an Eson-Object and responds to ‘to_query_hash`, this will be preferred. Otherwise, MultiJson is used.

Parameters:

  • obj (Object)

    the object to encode

Returns:

  • (String)

    the object, encoded as JSON



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/eson/request.rb', line 142

def encode(obj)
  if obj.respond_to? :to_query_hash
    obj = obj.to_query_hash
  end

  if obj.respond_to? :to_json
    obj.to_json
  else
    MultiJson.encode(obj)
  end
end

#handle_block { ... } ⇒ Object

This is a default implementation of ‘handle_block` that can be overriden by apis or plugins.

Yields:

  • self



32
33
34
# File 'lib/eson/request.rb', line 32

def handle_block
  yield self
end

#parametersObject



89
90
91
92
93
94
95
96
97
# File 'lib/eson/request.rb', line 89

def parameters
  if self.respond_to?(:multi_index) && (multi_index == true)
    [:pretty, :indices, :case]
  elsif self.respond_to?(:multi_index) && (multi_index == false)
    [:pretty, :index, :case]
  else
    [:pretty, :case]
  end
end

#parameters=(params) ⇒ Object

Mass assignment of parameters to a request. Invalid parameters will raise an exception.

Parameters:

  • params (Hash)

    The parameters, keyed by name

Raises:

  • (NoMethodError)


56
57
58
59
60
61
62
63
64
# File 'lib/eson/request.rb', line 56

def parameters=(params)
  params.each do |k,v|
    begin
      self.send("#{k}=", v)
    rescue NoMethodError => e
      raise NoMethodError, "Tried to set parameter `#{k}`, but request has no such parameter."
    end
  end
end

#pluggable?(api, plugin, client) ⇒ true, false

Checks whether a Plugin works with a certain type of request. For example, Search plugins usually only work with Search requests. The decision is up to the plugin. If the plugin does not list a set of APIs that it works with, it will be considered compatible to all.

Parameters:

  • api (Eson::Api)

    the api in question

  • plugin (Module)

    the plugin module

  • client (Eson::Client)

    the client to which the plugin should be added

Returns:

  • (true, false)

    Whether the plugin is compatible or not.



77
78
79
80
81
82
83
# File 'lib/eson/request.rb', line 77

def pluggable?(api, plugin, client)
  if plugin.respond_to? :plugin_for
    plugin.plugin_for(client.protocol).include?(api)
  else
    true
  end
end

#set_parameters_without_exceptions(params) ⇒ Object

This is an internal method that allows the client to set default parameters to requests without having to check for the presence of the method beforehand.



41
42
43
44
45
46
47
48
49
# File 'lib/eson/request.rb', line 41

def set_parameters_without_exceptions(params)
  params.each do |k,v|
    begin
      self.send("#{k}=", v)
    rescue NoMethodError => e
      #drop
    end
  end
end

#source_from_paramsString

Extracts the source parameters from the parameter set.

Returns:

  • (String)

    the request source, as String



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/eson/request.rb', line 114

def source_from_params
  return nil unless self.respond_to? :source_param

  if Symbol === source_param
    obj = self.send source_param
    if (String === obj || obj.nil?)
      return obj
    else
      return encode(obj)
    end
  else
    obj = {}
    source_param.each_with_object(obj) do |p, o|
      if v = self.send(p)
        o[p] = v
      end
    end

    return nil if obj.empty?
    return encode(obj)
  end
end