Sha256: b3f83861db67936392badcf708e235ba1a785d64ee7ea944f81def7c482a1883

Contents?: true

Size: 847 Bytes

Versions: 7

Compression:

Stored size: 847 Bytes

Contents

# frozen_string_literal: true

class Contentful::Client
  class << self
    alias_method :old_get_http, :get_http
  end

  def self.adapter
    @adapter ||=
      WCC::Contentful::SimpleClient.load_adapter(WCC::Contentful.configuration.http_adapter) ||
      ->(url, query, headers, proxy) { old_get_http(url, query, headers, proxy) }
  end

  def self.get_http(url, query, headers = {}, proxy = {})
    if environment = WCC::Contentful.configuration.environment
      url = rewrite_to_environment(url, environment)
    end

    adapter.call(url, query, headers, proxy)
  end

  REWRITE_REGEXP = /^(https?\:\/\/(?:\w+)\.contentful\.com\/spaces\/[^\/]+\/)(?!environments)(.+)$/
  def self.rewrite_to_environment(url, environment)
    return url unless m = REWRITE_REGEXP.match(url)

    File.join(m[1], 'environments', environment, m[2])
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
wcc-contentful-0.4.0.pre.rc lib/wcc/contentful/client_ext.rb
wcc-contentful-0.4.0.pre.beta lib/wcc/contentful/client_ext.rb
wcc-contentful-0.4.0.pre.alpha lib/wcc/contentful/client_ext.rb
wcc-contentful-0.3.0 lib/wcc/contentful/client_ext.rb
wcc-contentful-0.3.0.pre.rc3 lib/wcc/contentful/client_ext.rb
wcc-contentful-0.3.0.pre.rc2 lib/wcc/contentful/client_ext.rb
wcc-contentful-0.3.0.pre.rc lib/wcc/contentful/client_ext.rb