Class: Facile::Request
- Inherits:
-
Object
- Object
- Facile::Request
- Defined in:
- lib/facile/request.rb
Defined Under Namespace
Classes: RequestError
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#method ⇒ Object
Returns the value of attribute method.
-
#options ⇒ Object
Returns the value of attribute options.
-
#params ⇒ Object
Returns the value of attribute params.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #go ⇒ Object
-
#initialize(options = {}, &block) ⇒ Request
constructor
A new instance of Request.
- #parse_response(res) ⇒ Object
Constructor Details
#initialize(options = {}, &block) ⇒ Request
Returns a new instance of Request.
15 16 17 18 19 20 21 22 23 |
# File 'lib/facile/request.rb', line 15 def initialize( = {}, &block) .merge!({ :method => :get }) unless .has_key?(:method) .each do |k, v| self.send("#{k}=", v) if respond_to?("#{k}=") end instance_eval(&block) if block_given? end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
53 54 55 |
# File 'lib/facile/request.rb', line 53 def body @body end |
#headers ⇒ Object
Returns the value of attribute headers.
53 54 55 |
# File 'lib/facile/request.rb', line 53 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
53 54 55 |
# File 'lib/facile/request.rb', line 53 def method @method end |
#options ⇒ Object
Returns the value of attribute options.
53 54 55 |
# File 'lib/facile/request.rb', line 53 def @options end |
#params ⇒ Object
Returns the value of attribute params.
53 54 55 |
# File 'lib/facile/request.rb', line 53 def params @params end |
#url ⇒ Object
Returns the value of attribute url.
53 54 55 |
# File 'lib/facile/request.rb', line 53 def url @url end |
Class Method Details
.method_missing(method, *args, &block) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/facile/request.rb', line 4 def self.method_missing(method, *args, &block) supported_methods = [:get, :post, :delete, :options, :patch, :put] raise NoMethodError, "undefined method '#{method}'" if !supported_methods.include?(method) args = args.count == 0 ? {} : args args.merge!({:method => method || :get }) new(args, &block).go end |
Instance Method Details
#go ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/facile/request.rb', line 25 def go begin conn = Faraday.new( || {}) res = conn.send(method) do |req| req.url url headers.map { |k,v| req.headers[k] = v } if !headers.nil? params.map { |k,v| req.params[k] = v } if !params.nil? req.body = body if !body.nil? end rescue => e raise e end parse_response(res) end |