Sha256: ba311558a2b373ed0f69b791afe5747aa16537539102e973d06f38b0e7751eef

Contents?: true

Size: 1.12 KB

Versions: 31

Compression:

Stored size: 1.12 KB

Contents

require 'faraday'
require 'faraday_middleware'

module Krikri::LDP
  module Resource
    extend ActiveSupport::Concern

    attr_accessor :rdf_subject, :connection_uri

    def ldp_connection
      @ldp_conn ||= Faraday.new(connection_uri) do |conn|
        conn.use Faraday::Response::RaiseError
        conn.use FaradayMiddleware::FollowRedirects, limit: 3
        conn.adapter Faraday.default_adapter
      end
    end

    def etag
      http_head.env['response_headers']['etag']
    end

    def http_head
      raise 'No subject set for LDP resource.' if rdf_subject.nil?
      @http_cache ||= ldp_connection.head { |request| request.url rdf_subject }
    end

    def exists?
      return true if rdf_subject && http_head
      false
    rescue Faraday::ResourceNotFound
      false
    end

    def save(uri, body = nil, opts = {})
      require 'pry'
      binding.pry
      create(uri, body, opts)
    end

    def create(uri, body = nil, opts = {})
      response = ldp_connection.post do |request|
        request.url uri
        request.headers = opts if opts
        request.body = body
      end
      response
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
krikri-0.5.7 lib/krikri/ldp/resource.rb~
krikri-0.5.6 lib/krikri/ldp/resource.rb~
krikri-0.5.5 lib/krikri/ldp/resource.rb~
krikri-0.4.0 lib/krikri/ldp/resource.rb~
krikri-0.3.3 lib/krikri/ldp/resource.rb~
krikri-0.3.2 lib/krikri/ldp/resource.rb~
krikri-0.3.1 lib/krikri/ldp/resource.rb~
krikri-0.1.3 lib/krikri/ldp/resource.rb~
krikri-0.1.2 lib/krikri/ldp/resource.rb~
krikri-0.1.1 lib/krikri/ldp/resource.rb~
krikri-0.1.0 lib/krikri/ldp/resource.rb~