Class: Homeflow::API::Request
- Inherits:
-
Object
- Object
- Homeflow::API::Request
- Includes:
- HTTParty
- Defined in:
- lib/homeflow/api/request.rb
Instance Attribute Summary collapse
-
#request_specification ⇒ Object
Returns the value of attribute request_specification.
-
#resource_class ⇒ Object
Returns the value of attribute resource_class.
Class Method Summary collapse
Instance Method Summary collapse
- #body_of_request(request) ⇒ Object
- #constant_params ⇒ Object
-
#initialize(request_specification) ⇒ Request
constructor
A new instance of Request.
- #normalised_base_url ⇒ Object
- #perform ⇒ Object
- #perform_request ⇒ Object
Constructor Details
#initialize(request_specification) ⇒ Request
Returns a new instance of Request.
11 12 13 |
# File 'lib/homeflow/api/request.rb', line 11 def initialize(request_specification) @request_specification = request_specification end |
Instance Attribute Details
#request_specification ⇒ Object
Returns the value of attribute request_specification.
9 10 11 |
# File 'lib/homeflow/api/request.rb', line 9 def request_specification @request_specification end |
#resource_class ⇒ Object
Returns the value of attribute resource_class.
9 10 11 |
# File 'lib/homeflow/api/request.rb', line 9 def resource_class @resource_class end |
Class Method Details
.run_for(request_specification) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/homeflow/api/request.rb', line 77 def run_for(request_specification) r = Request.new(request_specification) r = r.perform if r.is_a? Hash Response.new(r) else Response.new_from_json(r) end end |
Instance Method Details
#body_of_request(request) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/homeflow/api/request.rb', line 60 def body_of_request(request) if request.respond_to? :body request.body else request end end |
#constant_params ⇒ Object
69 70 71 72 73 74 |
# File 'lib/homeflow/api/request.rb', line 69 def constant_params { :api_key => Homeflow::API.config.api_key, :request_key => Homeflow::API.config.request_key } end |
#normalised_base_url ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/homeflow/api/request.rb', line 51 def normalised_base_url source = Homeflow::API.config.source.gsub(/.\/$/,'') if request_specification.is_a? Query return "#{source}/#{request_specification.resource_class.resource_uri}" else return "#{source}/#{request_specification.resource_uri}" end end |
#perform ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/homeflow/api/request.rb', line 15 def perform begin response = body_of_request(perform_request) rescue Errno::ECONNREFUSED => e raise Homeflow::API::Exceptions::APIConnectionError, "Connection error. Homeflow might be down?" end response end |
#perform_request ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/homeflow/api/request.rb', line 24 def perform_request url = normalised_base_url query_params = @request_specification.to_params.merge(constant_params) post_params = (@request_specification.respond_to?(:post_params) ? @request_specification.post_params : {}) if Homeflow::API.config.show_debug && Homeflow::API.configuration.logger log_line = [] log_line << "Destination - #{url}" log_line << "Request params:\n#{query_params.to_json}\n" log_line << "Post params:\n#{post_params.to_json}\n" log_line << "request_specification:\n#{request_specification.to_json}\n" log_line << "@request_specification:\n#{@request_specification.to_json}\n" Homeflow::API.configuration.logger.info(log_line.join("\n")) end if request_specification.is_a? Query return (self.class.get(url, :query => query_params)) elsif request_specification.is_a? ResourceIdentifier return (self.class.get(url, :query => query_params)) elsif request_specification.is_a? Delete return (self.class.delete(url, :query => query_params)) elsif request_specification.is_a? Put return (self.class.put(url, :query => query_params, :body => post_params)) elsif request_specification.is_a? Post return (self.class.post(url, :query => query_params, :body => post_params)) end end |