<commit_before>require 'json'
require 'sawyer'

require 'voog_api/error'

require 'voog_api/api/articles'
require 'voog_api/api/assets'
require 'voog_api/api/comments'
require 'voog_api/api/content_partials'
require 'voog_api/api/contents'
require 'voog_api/api/element_definitions'
require 'voog_api/api/elements'
require 'voog_api/api/forms'
require 'voog_api/api/languages'
require 'voog_api/api/layouts'
require 'voog_api/api/layout_assets'
require 'voog_api/api/media_sets'
require 'voog_api/api/nodes'
require 'voog_api/api/site'
require 'voog_api/api/pages'
require 'voog_api/api/people'
require 'voog_api/api/search'
require 'voog_api/api/site_users'
require 'voog_api/api/tags'
require 'voog_api/api/texts'
require 'voog_api/api/tickets'

module Voog

  # Voog API client.
  #
  # @see http://www.voog.com/developers/api
  class Client

    MAX_PER_PAGE = 250

    include Voog::API::Articles
    include Voog::API::Assets
    include Voog::API::Comments
    include Voog::API::ContentPartials
    include Voog::API::Contents
    include Voog::API::ElementDefinitions
    include Voog::API::Elements
    include Voog::API::Forms
    include Voog::API::Languages
    include Voog::API::Layouts
    include Voog::API::LayoutAssets
    include Voog::API::MediaSets
    include Voog::API::Nodes
    include Voog::API::Pages
    include Voog::API::People
    include Voog::API::Search
    include Voog::API::Site
    include Voog::API::SiteUsers
    include Voog::API::Tags
    include Voog::API::Texts
    include Voog::API::Tickets

    attr_reader :api_token, :host, :protocol, :auto_paginate, :per_page

    # Initialize Voog API client.
    #
    # @param host [String] a Voog site host.
    # @param api_token [String] a Voog site API token.
    # @option options [String] :protocol endpoint protocol ("http" or "https"). Defaults to "http".
    # @option options [String] :auto_paginate enable auto pagination for list requests. Defaults to "false".
    # @option options [String] :per_page set default "per_page" value for list requests. Defaults to "nil".
    # @option options [Boolean] :raise_on_error interrupts program with error ("Faraday::Error") when request response code is between "400" and "600" (default is "false").
    # @example Initialize client
    #   client = Voog::Client.new('example.com', 'afcf30182aecfc8155d390d7d4552d14', protocol: :http, raise_on_error: false)
    def initialize(host = Voog.host, api_token = Voog.api_token, options = {})
      @host = host
      @api_token = api_token
      @options = options
      @protocol = options[:protocol].to_s.downcase == 'https' ? 'https' : 'http'
      @auto_paginate = options.fetch(:auto_paginate, Voog.auto_paginate)
      @per_page = options.fetch(:per_page, Voog.per_page)
      @raise_on_error = options.fetch(:raise_on_error, true)
    end

    def get(url, options = {})
      request :get, url, nil, options
    end
    
    def post(url, data, options = {})
      request :post, url, data, options
    end

    def put(url, data, options = {})
      request :put, url, data, options
    end

    def patch(url, data, options = {})
      request :patch, url, data, options
    end

    def delete(url, options = {})
      request :delete, url, nil, options
    end

    def head(url, options = {})
      request :head, url, nil, options
    end
    
    def api_endpoint
      "#{host_with_protocol}/admin/api".freeze
    end

    def host_with_protocol
      "#{protocol}://#{host}".freeze
    end
    
    def agent
      @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
        http.headers[:content_type] = 'application/json'
        http.headers[:accept] = 'application/json'
        http.headers[:user_agent] = 'Voog.rb Ruby wrapper'
        http.headers[:x_api_token] = @api_token
      end
    end

    def multipart_agent
      @multipart_agent ||= Faraday.new do |faraday|
        faraday.request :multipart
        faraday.response :raise_error if @raise_on_error
        faraday.adapter :net_http

        faraday.headers[:x_api_token] = @api_token
        faraday.headers[:user_agent] = 'Voog.rb Ruby wrapper'
      end
    end
    
    def last_response
      @last_response
    end

    def parse_response(response)
      JSON.parse(response).inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
    end

    # Fetch all elements for requested API resource when {#auto_paginate} is turned on.
    #
    # @see http://www.voog.com/developers/api/basics/pagination
    def paginate(url, options = {}, &block)
      opts = options.dup
      if @auto_paginate || @per_page
        opts[:query][:per_page] ||= @per_page || (@auto_paginate ? MAX_PER_PAGE : nil)
      end

      data = request(:get, url, nil, opts)
<commit_msg>Fixed pagination when autopagination is enabled.<commit_after>require 'json'
require 'sawyer'

require 'voog_api/error'

require 'voog_api/api/articles'
require 'voog_api/api/assets'
require 'voog_api/api/comments'
require 'voog_api/api/content_partials'
require 'voog_api/api/contents'
require 'voog_api/api/element_definitions'
require 'voog_api/api/elements'
require 'voog_api/api/forms'
require 'voog_api/api/languages'
require 'voog_api/api/layouts'
require 'voog_api/api/layout_assets'
require 'voog_api/api/media_sets'
require 'voog_api/api/nodes'
require 'voog_api/api/site'
require 'voog_api/api/pages'
require 'voog_api/api/people'
require 'voog_api/api/search'
require 'voog_api/api/site_users'
require 'voog_api/api/tags'
require 'voog_api/api/texts'
require 'voog_api/api/tickets'

module Voog

  # Voog API client.
  #
  # @see http://www.voog.com/developers/api
  class Client

    MAX_PER_PAGE = 250

    include Voog::API::Articles
    include Voog::API::Assets
    include Voog::API::Comments
    include Voog::API::ContentPartials
    include Voog::API::Contents
    include Voog::API::ElementDefinitions
    include Voog::API::Elements
    include Voog::API::Forms
    include Voog::API::Languages
    include Voog::API::Layouts
    include Voog::API::LayoutAssets
    include Voog::API::MediaSets
    include Voog::API::Nodes
    include Voog::API::Pages
    include Voog::API::People
    include Voog::API::Search
    include Voog::API::Site
    include Voog::API::SiteUsers
    include Voog::API::Tags
    include Voog::API::Texts
    include Voog::API::Tickets

    attr_reader :api_token, :host, :protocol, :auto_paginate, :per_page

    # Initialize Voog API client.
    #
    # @param host [String] a Voog site host.
    # @param api_token [String] a Voog site API token.
    # @option options [String] :protocol endpoint protocol ("http" or "https"). Defaults to "http".
    # @option options [String] :auto_paginate enable auto pagination for list requests. Defaults to "false".
    # @option options [String] :per_page set default "per_page" value for list requests. Defaults to "nil".
    # @option options [Boolean] :raise_on_error interrupts program with error ("Faraday::Error") when request response code is between "400" and "600" (default is "false").
    # @example Initialize client
    #   client = Voog::Client.new('example.com', 'afcf30182aecfc8155d390d7d4552d14', protocol: :http, raise_on_error: false)
    def initialize(host = Voog.host, api_token = Voog.api_token, options = {})
      @host = host
      @api_token = <PASSWORD>_token
      @options = options
      @protocol = options[:protocol].to_s.downcase == 'https' ? 'https' : 'http'
      @auto_paginate = options.fetch(:auto_paginate, Voog.auto_paginate)
      @per_page = options.fetch(:per_page, Voog.per_page)
      @raise_on_error = options.fetch(:raise_on_error, true)
    end

    def get(url, options = {})
      request :get, url, nil, options
    end
    
    def post(url, data, options = {})
      request :post, url, data, options
    end

    def put(url, data, options = {})
      request :put, url, data, options
    end

    def patch(url, data, options = {})
      request :patch, url, data, options
    end

    def delete(url, options = {})
      request :delete, url, nil, options
    end

    def head(url, options = {})
      request :head, url, nil, options
    end
    
    def api_endpoint
      "#{host_with_protocol}/admin/api".freeze
    end

    def host_with_protocol
      "#{protocol}://#{host}".freeze
    end
    
    def agent
      @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
        http.headers[:content_type] = 'application/json'
        http.headers[:accept] = 'application/json'
        http.headers[:user_agent] = 'Voog.rb Ruby wrapper'
        http.headers[:x_api_token] = @api_token
      end
    end

    def multipart_agent
      @multipart_agent ||= Faraday.new do |faraday|
        faraday.request :multipart
        faraday.response :raise_error if @raise_on_error
        faraday.adapter :net_http

        faraday.headers[:x_api_token] = @api_token
        faraday.headers[:user_agent] = 'Voog.rb Ruby wrapper'
      end
    end
    
    def last_response
      @last_response
    end

    def parse_response(response)
      JSON.parse(response).inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
    end

    # Fetch all elements for requested API resource when {#auto_paginate} is turned on.
    #
    # @see http://www.voog.com/developers/api/basics/pagination
    def paginate(url, options = {}, &block)
      opts = options.dup
      if @auto_paginate || @per_page
        opts[:query] ||= {}
        opts[:query][:per_page] ||= @per_page || (@auto_paginate ? MAX_PER_PAGE : nil)
      end

      data = request(:get, url, nil, opts)

{'fraction_non_alphanumeric': 0.12176003344481605, 'fraction_numerical': 0.005852842809364548, 'mean_word_length': 3.425994449583719, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3181867', 'n_tokens_mistral': 3232, 'n_tokens_neox': 3157, 'n_words': 754}