Sha256: df09c121ade9dc299f10b8e2fb5a6c53986da78b4c0d356405255ae27e264f74

Contents?: true

Size: 1.75 KB

Versions: 6

Compression:

Stored size: 1.75 KB

Contents

module FlexmlsApi
  module Models
    class Photo < Base
      extend Subresource
      self.element_name = "photos"
      
      attr_accessor :update_path
      
      EDITABLE_FIELDS = [:Picture, :FileName, :Name, :Caption, :Primary]
      
      def initialize(opts={})
        defaulted_opts = {}
        EDITABLE_FIELDS.each do |k|
          key = k.to_s()
          defaulted_opts[key] = opts[key] || nil
        end
        super(opts.merge(defaulted_opts))
      end

      def primary? 
        @attributes["Primary"] == true 
      end
      
      def save(arguments={})
        begin
          return save!(arguments)
        rescue NotFound, BadResourceRequest => e
          FlexmlsApi.logger.error("Failed to save resource #{self}: #{e.message}")
        end
        false
      end
      def save!(arguments={})
        payload = {"Photos" => [ build_photo_hash]}
        if exists?
          results = connection.put "#{update_path}/#{self.Id}", payload, arguments
        else
          results = connection.post update_path, payload, arguments
        end
        result = results.first
        load(result)
        true
      end
      
      def load_picture(file_name)
        self.Picture = Base64.encode64(File.open(file_name, 'rb').read).gsub(/\n/, '')
        self.FileName = File.basename(file_name)
      end
      
      def delete(args={})
        connection.delete("#{update_path}/#{self.Id}", args)
      end
      
      def exists?
        @attributes.include?("Id")
      end
      
      private
      
      def build_photo_hash
        results_hash = {} 
        EDITABLE_FIELDS.each do |k|
          key = k.to_s
          results_hash[key] = @attributes[key] unless @attributes[key].nil?
        end
        results_hash
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
spark_api-1.0.2 lib/spark_api/models/photo.rb~
spark_api-1.0.1 lib/spark_api/models/photo.rb~
spark_api-1.0.0 lib/spark_api/models/photo.rb~
flexmls_api-0.7.3 lib/flexmls_api/models/photo.rb
flexmls_api-0.7.5 lib/flexmls_api/models/photo.rb
flexmls_api-0.7.0 lib/flexmls_api/models/photo.rb