Class: Eson::Request
- Inherits:
-
Object
- Object
- Eson::Request
- 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`.
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
-
#case ⇒ Object
Returns the value of attribute case.
-
#client ⇒ Object
Returns the value of attribute client.
-
#index ⇒ Object
Returns the value of attribute index.
-
#indices ⇒ Object
Returns the value of attribute indices.
-
#pretty ⇒ Object
Returns the value of attribute pretty.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
-
#encode(obj) ⇒ String
Encode any object.
-
#handle_block { ... } ⇒ Object
This is a default implementation of ‘handle_block` that can be overriden by apis or plugins.
-
#initialize(api, plugins, client) ⇒ Request
constructor
A new instance of Request.
- #parameters ⇒ Object
-
#parameters=(params) ⇒ Object
Mass assignment of parameters to a request.
-
#pluggable?(api, plugin, client) ⇒ true, false
Checks whether a Plugin works with a certain type of request.
-
#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.
-
#source_from_params ⇒ String
Extracts the source parameters from the parameter set.
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
#api ⇒ Object
Returns the value of attribute api.
11 12 13 |
# File 'lib/eson/request.rb', line 11 def api @api end |
#case ⇒ Object
Returns the value of attribute case.
13 14 15 |
# File 'lib/eson/request.rb', line 13 def case @case end |
#client ⇒ Object
Returns the value of attribute client.
11 12 13 |
# File 'lib/eson/request.rb', line 11 def client @client end |
#index ⇒ Object
Returns the value of attribute index.
13 14 15 |
# File 'lib/eson/request.rb', line 13 def index @index end |
#indices ⇒ Object
Returns the value of attribute indices.
13 14 15 |
# File 'lib/eson/request.rb', line 13 def indices @indices end |
#pretty ⇒ Object
Returns the value of attribute pretty.
13 14 15 |
# File 'lib/eson/request.rb', line 13 def pretty @pretty end |
#source ⇒ Object
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.
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.
32 33 34 |
# File 'lib/eson/request.rb', line 32 def handle_block yield self end |
#parameters ⇒ Object
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.
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.
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_params ⇒ String
Extracts the source parameters from the parameter set.
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 |