<commit_before>require 'rest_client'
require 'values'

module CrystalApi
  class StoreEndpoint
    attr_reader :base_url, :token

    class Response < Value.new(:parsed, :raw)
      def to_s
        "<CrystalApi::StoreEndpoint::Response parsed:#{parsed.class.name} raw.length:#{raw.length}>"
      end
    end

    def initialize(args = {})
      @base_url = args[:base_url]
      @token = args[:token]
    end

    def headers
      {
        'Authorization' => "OAuth #{token}"
      }
    end

    def get(path)
      raw = RestClient.get(base_url + path, headers)

      wrap_response(raw)
    end

    def post(path, payload)
      raw = RestClient.post(base_url + path, payload, headers)

      wrap_response(raw)
    end

    def delete(path)
      raw = RestClient.delete(base_url + path, headers)

      wrap_response(raw)
    end

    private

    def wrap_response(raw)
      parsed = parse_response_body(raw)

      Response.new(parsed, raw)
    end

    def parse_response_body(raw)
      json = JSON.parse(raw)

      if json.is_a?(Array)
        json.map {|obj| parse(obj)}
      else
        parse(json)
      end
    rescue JSON::ParserError
      nil
    end

    def parse(object)
      CrystalApi.from_json(object)
    end
  end
end
<commit_msg>Add parsed json to response<commit_after>require 'rest_client'
require 'values'

module CrystalApi
  class StoreEndpoint
    attr_reader :base_url, :token

    class Response < Value.new(:parsed, :raw, :json)
      def to_s
        "<CrystalApi::StoreEndpoint::Response parsed:#{parsed.class.name} raw.length:#{raw.length}>"
      end
    end

    def initialize(args = {})
      @base_url = args[:base_url]
      @token = args[:token]
    end

    def headers
      {
        'Authorization' => "OAuth #{token}"
      }
    end

    def get(path)
      raw = RestClient.get(base_url + path, headers)

      wrap_response(raw)
    end

    def post(path, payload)
      raw = RestClient.post(base_url + path, payload, headers)

      wrap_response(raw)
    end

    def delete(path)
      raw = RestClient.delete(base_url + path, headers)

      wrap_response(raw)
    end

    private

    def wrap_response(raw)
      json = JSON.parse(raw)
      parsed = parse_response_body(json)

      Response.new(parsed, raw, json)
    end

    def parse_response_body(json)
      if json.is_a?(Array)
        json.map {|obj| parse(obj)}
      else
        parse(json)
      end
    rescue JSON::ParserError
      nil
    end

    def parse(object)
      CrystalApi.from_json(object)
    end
  end
end

{'fraction_non_alphanumeric': 0.10281469898358092, 'fraction_numerical': 0.0, 'mean_word_length': 2.37598944591029, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6180767', 'n_tokens_mistral': 841, 'n_tokens_neox': 825, 'n_words': 188}