Sha256: 2905e1dcc8e4dd479171051b8e0ee9f9ad7a65bb6c263253f843f35214d2e564

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require 'addressable'
require 'faraday'
require 'faraday_middleware'

module Reality::Describers::Wikidata::Impl
  # Internal low-level client class, used by {Api}.
  #
  # Uses [Faraday](https://github.com/lostisland/faraday) library inside (and will expose it's settings
  # in future).
  #
  # You should not use it directly, all you need is in {Api}.
  class Client
    # Default MediaWiktory User-Agent header.
    #
    # You can set yours as an option to {#initialize}
    UA = 'MediaWiktory/0.1.0 '\
         '(https://github.com/molybdenum-99/mediawiktory; zverok.offline@gmail.com)'

    class << self
      # User agent getter/setter.
      #
      # Default value is {UA}.
      #
      # You can also use per-instance option, see {#initialize}
      attr_accessor :user_agent
    end

    attr_reader :url

    def initialize(url, **options)
      @url = Addressable::URI.parse(url)
      @options = options
      @faraday = Faraday.new(url, headers: headers) do |f|
        f.request :url_encoded
        f.use FaradayMiddleware::FollowRedirects, limit: 5
        f.use FaradayMiddleware::Gzip
        f.adapter Faraday.default_adapter
      end
    end

    def user_agent
      @options[:user_agent] || @options[:ua] || self.class.user_agent || UA
    end

    def get(params)
      @faraday.get('', params).body
    end

    def post(params)
      @faraday.post('', params).body
    end

    private

    def headers
      {'Accept-Encoding' => 'gzip', 'User-Agent' => user_agent}
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
reality-0.1.0.alpha3 lib/reality/describers/wikidata/client.rb
reality-0.1.0.alpha2 lib/reality/describers/wikidata/client.rb
reality-0.1.0.alpha lib/reality/describers/wikidata/client.rb