Sha256: c28b9281ee37907b950125561405b308660048c20464b3cfdb7e1765be9e1601

Contents?: true

Size: 1.83 KB

Versions: 14

Compression:

Stored size: 1.83 KB

Contents

require 'rest-client'
require 'json'
require 'uri'

module Trackman
  module Assets
    class RemoteAsset < Asset
      @@server_url = ENV['TRACKMAN_URL']

      @@site = "#{@@server_url}/assets"

      attr_reader :id

      def initialize attributes = {}
        ensure_config
        super

        @id = attributes[:id]
        @file_hash = attributes[:file_hash]
      end
      
      def file_hash
        @file_hash || super
      end
      
      def validate_path?
        false
      end

      def self.find id
        response = RestClient.get "#{@@site}/#{id}"
        
        body = Hash[JSON.parse(response).map{ |k, v| [k.to_sym, v] }]
        RemoteAsset.new(body)
      end

      def self.all
        get_attributes.map{ |r| RemoteAsset.new(r) }.sort
      end

      def create!
        response = RestClient.post @@site, build_params, :content_type => :json, :accept => :json
        path = response.headers[:location]
        @id = path[/\d+$/].to_i
      end

      def update!
        RestClient.put "#{@@site}/#{id}", build_params, :content_type => :json, :accept => :json
      end  
      def delete
        response = RestClient.delete "#{@@site}/#{id}"
        true
      end

      def ==(other)
        result = super
        if result
          if other.is_a? RemoteAsset
            result = other.id == id && other.file_hash == file_hash 
          end
          return result
        end
        false 
      end

      private
        def build_params
          { :asset => { :path => path.to_s, :file => File.open(path) } }
        end 
        def ensure_config
          raise Errors::ConfigNotFoundError, "The config TRACKMAN_URL is missing." if @@server_url.nil?      
        end
        def self.get_attributes
          JSON.parse(RestClient.get @@site).map{|r|  Hash[r.map{ |k, v| [k.to_sym, v] }] }
        end
    end 
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
trackman-0.2.65 lib/trackman/assets/remote_asset.rb
trackman-0.2.64 lib/trackman/assets/remote_asset.rb
trackman-0.2.63 lib/trackman/assets/remote_asset.rb
trackman-0.2.62 lib/trackman/assets/remote_asset.rb
trackman-0.2.61 lib/trackman/assets/remote_asset.rb
trackman-0.2.5 lib/trackman/assets/remote_asset.rb
trackman-0.2.4 lib/trackman/assets/remote_asset.rb
trackman-0.2.3 lib/trackman/assets/remote_asset.rb
trackman-0.2.1 lib/trackman/assets/remote_asset.rb
trackman-0.2.0 lib/trackman/assets/remote_asset.rb
trackman-0.1.9 lib/trackman/assets/remote_asset.rb
trackman-0.1.8 lib/trackman/assets/remote_asset.rb
trackman-0.1.7 lib/trackman/assets/remote_asset.rb
trackman-0.1.6 lib/trackman/assets/remote_asset.rb