Class: Eson::Request
- Inherits:
-
Object
- Object
- Eson::Request
- Defined in:
- lib/eson/request.rb
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) ⇒ Object
- #handle_block {|_self| ... } ⇒ Object
-
#initialize(api, plugins, client) ⇒ Request
constructor
A new instance of Request.
- #parameters ⇒ Object
- #params=(params) ⇒ Object
- #pluggable?(api, plugin, client) ⇒ Boolean
-
#source_from_params ⇒ Object
TODO: woah, this needs refactoring.
Constructor Details
#initialize(api, plugins, client) ⇒ Request
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/eson/request.rb', line 7 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.
3 4 5 |
# File 'lib/eson/request.rb', line 3 def api @api end |
#case ⇒ Object
Returns the value of attribute case.
5 6 7 |
# File 'lib/eson/request.rb', line 5 def case @case end |
#client ⇒ Object
Returns the value of attribute client.
3 4 5 |
# File 'lib/eson/request.rb', line 3 def client @client end |
#index ⇒ Object
Returns the value of attribute index.
5 6 7 |
# File 'lib/eson/request.rb', line 5 def index @index end |
#indices ⇒ Object
Returns the value of attribute indices.
5 6 7 |
# File 'lib/eson/request.rb', line 5 def indices @indices end |
#pretty ⇒ Object
Returns the value of attribute pretty.
5 6 7 |
# File 'lib/eson/request.rb', line 5 def pretty @pretty end |
#source ⇒ Object
Returns the value of attribute source.
5 6 7 |
# File 'lib/eson/request.rb', line 5 def source @source end |
Instance Method Details
#encode(obj) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/eson/request.rb', line 101 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 {|_self| ... } ⇒ Object
19 20 21 |
# File 'lib/eson/request.rb', line 19 def handle_block yield self end |
#parameters ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/eson/request.rb', line 45 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 |
#params=(params) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/eson/request.rb', line 23 def params=(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) ⇒ Boolean
33 34 35 36 37 38 39 |
# File 'lib/eson/request.rb', line 33 def pluggable?(api, plugin, client) if plugin.respond_to? :plugin_for plugin.plugin_for(client.protocol).include?(api) else true end end |
#source_from_params ⇒ Object
TODO: woah, this needs refactoring
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/eson/request.rb', line 69 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 pairs = source_param.map do |p| if v = self.send(p) [p, v] else nil end end pairs.compact! return nil if pairs.empty? obj = {} pairs.each do |p, v| obj[p] = v end return encode(obj) end end |