Sha256: 9d8875e808a67fed9896b83c2d4eae140716a2f207def7b775824a9d7a24ded5

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

module Homeflow
  module API
   YAML::ENGINE.yamler = "syck"
   class Request

    attr_accessor :resource_class, :request_specification

    def initialize(request_specification)
      @request_specification = request_specification
    end

    def perform
      begin
        response = perform_request
      rescue Errno::ECONNREFUSED => e
        raise Homeflow::API::Exceptions::APIConnectionError, "Connection error. Homeflow might be down?"
      end
      response
    end

    def perform_request
      if request_specification.is_a? Query
        return (HTTParty.get("#{Homeflow::API.config.source}/#{request_specification.resource_class.resource_uri}", :query => @request_specification.to_params.merge(constant_params))).body
      elsif request_specification.is_a? ResourceIdentifier
        return (HTTParty.get("#{Homeflow::API.config.source}/#{request_specification.resource_uri}", :query => @request_specification.to_params.merge(constant_params))).body
      elsif request_specification.is_a? Delete
        return (HTTParty.delete("#{Homeflow::API.config.source}/#{request_specification.resource_uri}", :query => @request_specification.to_params.merge(constant_params))).body
      elsif request_specification.is_a? Put
        return (HTTParty.put("#{Homeflow::API.config.source}/#{request_specification.resource_uri}", :query => @request_specification.to_params.merge(constant_params), :body => @request_specification.post_params)).body
      elsif request_specification.is_a? Post
        return (HTTParty.post("#{Homeflow::API.config.source}/#{request_specification.resource_uri}", :query => @request_specification.to_params.merge(constant_params), :body => @request_specification.post_params)).body
      end
    end

    def constant_params
      {:api_key=> Homeflow::API.config.api_key}
    end

    class << self
      def run_for(request_specification)
        r = Request.new(request_specification)
        Response.new_from_json(r.perform)
      end
    end
    
   end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
homeflow_api-0.14.4 lib/homeflow/api/request.rb
homeflow_api-0.14.3 lib/homeflow/api/request.rb