Sha256: 8b84516f451dc70854d9b13b71514c88dced3dca1ddec90a7b96373752ff4000

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

module SirenClient
  class Action
    attr_reader :payload, :name, :classes, :method, :href, :title, :type, :fields, :config

    def initialize(data, config={})
      if data.class != Hash
        raise ArgumentError, "You must pass in a Hash to SirenClient::Action.new"
      end
      @payload = data

      @config = { format: :json }.merge config 
      @name    = @payload['name']   || ''
      @classes = @payload['class']  || []
      @method  = (@payload['method'] || 'GET').downcase
      @href    = @payload['href']   || ''
      @title   = @payload['title']  || ''
      @type    = @payload['type']   || 'application/x-www-form-urlencoded'
      @fields  = @payload['fields'] || []
      @fields.map! do |data|
        SirenClient::Field.new(data)
      end
    end

    def where(params = {})
      options = { headers: {}, query: {}, body: {} }.merge @config
      if @method == 'get'
        options[:query] = params
      else
        options[:body] = params
      end
      options[:headers]['Content-Type'] = @type
      begin
        query = options[:query].empty? ? '' : ('?' + options[:query].to_query)
        SirenClient.logger.debug "#{@method.upcase} #{@href}#{query}"
        options[:headers].each do |k, v|
          SirenClient.logger.debug "  #{k}: #{v}"
        end
        SirenClient.logger.debug '  ' + options[:body].to_query unless options[:body].empty?
        Entity.new(HTTParty.send(@method.to_sym, @href, options).parsed_response, @config)
      rescue URI::InvalidURIError => e
        raise InvalidURIError, e.message
      rescue JSON::ParserError => e
        raise InvalidResponseError, e.message
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
siren_client-0.1.1 lib/siren_client/action.rb
siren_client-0.1 lib/siren_client/action.rb