Class: WCC::Contentful::SimpleClient

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/contentful/simple_client.rb,
lib/wcc/contentful/simple_client/response.rb

Overview

The SimpleClient accesses the Contentful CDN to get JSON responses, returning the raw JSON data as a parsed hash. It can be configured to access any API url and exposes only a single method, get. This method returns a WCC::Contentful::SimpleClient::Response that handles paging automatically.

The SimpleClient by default uses ‘http’ to perform the gets, but any HTTP client can be injected by passing a proc as the adapter: option.

Direct Known Subclasses

Cdn, Management

Defined Under Namespace

Classes: ApiError, Cdn, Management, NotFoundError, Preview, Response, SyncResponse

Constant Summary collapse

ADAPTERS =
{
  http: ['http', '> 1.0', '< 3.0'],
  typhoeus: ['typhoeus', '~> 1.0']
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_url:, space:, access_token:, **options) ⇒ SimpleClient

Returns a new instance of SimpleClient.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/wcc/contentful/simple_client.rb', line 16

def initialize(api_url:, space:, access_token:, **options)
  @api_url = URI.join(api_url, '/spaces/', space + '/')
  @space = space
  @access_token = access_token

  @get_http = SimpleClient.load_adapter(options[:adapter])

  @options = options
  @query_defaults = {}
  @query_defaults[:locale] = @options[:default_locale] if @options[:default_locale]

  return unless env = options[:environment]
  @api_url = URI.join(@api_url, 'environments/', env + '/')
end

Class Method Details

.load_adapter(adapter) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/wcc/contentful/simple_client.rb', line 44

def self.load_adapter(adapter)
  case adapter
  when nil
    ADAPTERS.each do |a, spec|
      begin
        gem(*spec)
        return load_adapter(a)
      rescue Gem::LoadError
        next
      end
    end
    raise ArgumentError, 'Unable to load adapter!  Please install one of '\
      "#{ADAPTERS.values.map(&:join).join(',')}"
  when :http
    require_relative 'simple_client/http_adapter'
    HttpAdapter.new
  when :typhoeus
    require_relative 'simple_client/typhoeus_adapter'
    TyphoeusAdapter.new
  else
    unless adapter.respond_to?(:call)
      raise ArgumentError, "Adapter #{adapter} is not invokeable!  Please "\
        "pass a proc or use one of #{ADAPTERS.keys}"
    end
    adapter
  end
end

Instance Method Details

#get(path, query = {}) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/wcc/contentful/simple_client.rb', line 31

def get(path, query = {})
  url = URI.join(@api_url, path)

  Response.new(self,
    { url: url, query: query },
    get_http(url, query))
end