Sha256: e1414e7076e2b3a6d7bcca55df0184045e23b8835eb6d6e62d38d06a48ebeb45

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require "json"
require "net/http"

module VimeoRuby
  class Base
    attr_reader :access_token, :vimeo_id, :additional_info

    def initialize(access_token: nil, vimeo_id: nil, remaining_attrs: {})
      @access_token = access_token
      @vimeo_id = extract_vimeo_id_from_uri(vimeo_id)
      @additional_info = OpenStruct.new(remaining_attrs)
    end

    class << self
      def base_uri
        "https://api.vimeo.com"
      end

      def client_identifier
        ENV["VIMEO_CLIENT_IDENTIFIER"]
      end

      def client_secret
        ENV["VIMEO_CLIENT_SECRET"]
      end

      def get(uri, query_params: {}, access_token: nil)
        uri = URI.parse(uri)
        uri.query = URI.encode_www_form(query_params)

        Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |https|
          request = Net::HTTP::Get.new(uri)
          request["Authorization"] = "bearer #{access_token}"

          response = https.request(request)
          JSON.parse(response.body)
        end
      end
    end

    private

    def extract_vimeo_id_from_uri(vimeo_uri)
      vimeo_uri.match(/\d+/)[0].to_i
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vimeo_ruby-0.5.0 lib/vimeo_ruby/base.rb