Sha256: be537df73af3a459370eae672a67cd1e5f473d905257837da84070595c631a63

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

require 'net/http'
require 'json'

module FactoryGirl
  module RemoteApi
    class Request

      def get_response_for(factory_method, factory, attributes)
        uri = uri_for(factory_method, factory)
        uri.query = attributes_as_query(factory, attributes)
        parsed_response(Net::HTTP.get_response(uri).body, attributes[:parent_factory] || factory.to_s)
      end

    private

      def uri_for(factory_method, factory)
        factories_path = "#{configuration.server_url}#{configuration.server_mount_path}"
        factories_path << "/#{factory_method}" if factory_method != :create
        factories_path << "/#{factory}"
        URI(factories_path)
      end

      def configuration
        FactoryGirl::RemoteApi.configuration
      end

      def attributes_as_query(factory, attributes)
        attributes = { factory.to_sym => attributes } if attributes != {}
        attributes.to_query
      end

      def parsed_response(response_body, json_root)
        response_json = JSON.parse(response_body)
        attributes = response_json.key?(json_root) ? response_json[json_root] : response_json
        attributes.with_indifferent_access
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
factory_girl-remote_api-0.4.1 lib/factory_girl/remote_api/request.rb
factory_girl-remote_api-0.4.0 lib/factory_girl/remote_api/request.rb
factory_girl-remote_api-0.3.0 lib/factory_girl/remote_api/request.rb