Sha256: b3c4fb0007b9de85d06ee97ca9af1ad695ba11d130d0d323c7207cd63ec69dea

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 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.body) 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.body) unless response.is_a?(Net::HTTPSuccess)

      JSON.parse response.body
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
search_console_api-0.0.5 lib/search_console_api/request.rb
search_console_api-0.0.4 lib/search_console_api/request.rb
search_console_api-0.0.3 lib/search_console_api/request.rb