Sha256: 90d19f849968d0bcecf1b12be737a6ce4dadbb1a49ac40564dd3927db546fdd5

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

require 'faraday'
require 'faraday_middleware'

module Transifex
  module Request
    def set_credentials(username, password)
      @username = username
      @password = password
    end

    def connection
      @connection ||= make_connection(@username, @password)
    end

    def get(path, params = {})
      response = connection.get(build_path(:v2, path), params)
      response.body
    end

    private

    def build_path(version, path)
      "/api/2/#{path}"
    end

    def make_connection(username, password)
      options = {
        headers: {
          'Accept' => 'application/json',
          'User-Agent' => Transifex::Config::USER_AGENT,
        },
        url: Transifex::Config::BASE_URL
      }

      Faraday.new(options) do |builder|
        builder.use FaradayMiddleware::Mashify
        builder.use Faraday::Response::ParseJson, :content_type => /\bjson$/
        builder.use Transifex::Response::RaiseHttpException

        # Authentiation
        builder.basic_auth(username, password)

        # Request Middleware
        builder.use Faraday::Request::Multipart
        builder.use Faraday::Request::UrlEncoded

        builder.adapter :net_http
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
transifex-ruby-0.0.5 lib/transifex/request.rb
transifex-ruby-0.0.4 lib/transifex/request.rb
transifex-ruby-fork-jg-0.1.0 lib/transifex/request.rb