Sha256: 14d382e9b2d72593b0f4d9cbec90db86e79df8b28eda8a7b7c6bd894d4047162

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require 'phone_gap/build/creatable'

module PhoneGap
  module Build
    class RestResource < Base

      include PhoneGap::Build::Creatable
      attr_reader :id

      def create
        response = ApiRequest.new.post(path, post_options)
        if response.success?
          populate_from_json(JSON.parse(response.body))
        end
        self
      end

      def update
        ApiRequest.new.put(path, query: {data: as_json(only: updatable_attributes)})
      end

      def save
        @id ? update : create
      end

      def destroy
        ApiRequest.new.delete(path)
      end

      def as_json(params = {})
        if params[:only]
          json = params[:only].inject({}) do | memo, attribute_name|
            memo[attribute_name[1..-1].to_sym] = instance_variable_get(attribute_name)
            memo
          end
        else
          json = {}
        end
        params[:remove_nils] ? json.delete_if {|k, v| v.nil? } : json
      end

      private

      def post_options
        { query: { data: as_json(only: creatable_attributes) } }
      end

      def path
        @id ? "#{self.class.const_get('PATH')}/#{@id}" : "#{self.class.const_get('PATH')}"
      end

      def token
        PhoneGap::Build::Credentials.instance.token
      end

      def populate_from_json(json)
        json.each do |key, value|
          if respond_to?("#{key}=")
            send("#{key}=", value)
          else
            instance_variable_set("@#{key}", value)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
phone_gap-build-0.4.0 lib/phone_gap/build/rest_resource.rb