Sha256: cf0f1b8f3f327df63ad05c7c1d8bb44212d4976ab47b3598df91b2447e9b46d9

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

require 'base64'

module SparkApi
  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 BadResourceRequest => e
          SparkApi.logger.warn("Failed to save resource #{self}: #{e.message}")
        rescue NotFound => e
          SparkApi.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

4 entries across 4 versions & 1 rubygems

Version Path
spark_api-1.3.10 lib/spark_api/models/photo.rb
spark_api-1.3.9 lib/spark_api/models/photo.rb
spark_api-1.3.6 lib/spark_api/models/photo.rb
spark_api-1.3.3 lib/spark_api/models/photo.rb