Sha256: 676bd15d2ad383cf66fbbacd69e25a985a3cc3cffcfc393030c2c08233f86970

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require "net/http"
require "uri"
require "json"

module SearchConsoleApi
  class Request

    BASE_URL = "https://www.googleapis.com/webmasters/v3"

    def self.get(access_token:, path:, params: {})
      url = "#{BASE_URL}#{path}"
      url += "?#{URI.encode_www_form params}" unless params.empty?
      uri = URI(url)

      response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
        request = Net::HTTP::Get.new uri
        request["Authorization"] = "Bearer #{access_token}"
        request["Content-Type"] = "application/json"

        http.request request
      end
      raise SearchConsoleApi::Error.new(response) unless response.is_a?(Net::HTTPSuccess)

      JSON.parse response.body
    end

    def self.post(access_token:, path:, payload: nil)
      url = URI("#{BASE_URL}#{path}")

      https = Net::HTTP.new(url.host, url.port)
      https.use_ssl = true

      request = Net::HTTP::Post.new(url)
      request["Authorization"] = "Bearer #{access_token}"
      request["Content-Type"] = "application/javascript"
      request.body = payload

      response = https.request(request)
      raise SearchConsoleApi::Error.new(response) unless response.is_a?(Net::HTTPSuccess)

      JSON.parse response.body
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
search_console_api-0.0.2 lib/search_console_api/request.rb
search_console_api-0.0.1 lib/search_console_api/request.rb