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.13.2 lib/krikri/ldp/resource.rb~
krikri-0.13.1 lib/krikri/ldp/resource.rb~
krikri-0.13.0 lib/krikri/ldp/resource.rb~
krikri-0.12.4 lib/krikri/ldp/resource.rb~
krikri-0.11.2 lib/krikri/ldp/resource.rb~
krikri-0.11.1 lib/krikri/ldp/resource.rb~
krikri-0.11.0 lib/krikri/ldp/resource.rb~
krikri-0.10.1 lib/krikri/ldp/resource.rb~
krikri-0.8.9 lib/krikri/ldp/resource.rb~
krikri-0.8.8 lib/krikri/ldp/resource.rb~
krikri-0.8.7 lib/krikri/ldp/resource.rb~
krikri-0.8.6 lib/krikri/ldp/resource.rb~
krikri-0.8.5 lib/krikri/ldp/resource.rb~
krikri-0.8.4 lib/krikri/ldp/resource.rb~
krikri-0.8.2 lib/krikri/ldp/resource.rb~
krikri-0.8.0 lib/krikri/ldp/resource.rb~
krikri-0.7.3 lib/krikri/ldp/resource.rb~
krikri-0.7.2 lib/krikri/ldp/resource.rb~
krikri-0.7.1 lib/krikri/ldp/resource.rb~
krikri-0.6.0 lib/krikri/ldp/resource.rb~