Sha256: 6569085d766f9e0e0c7ab6125ca0cfeda5d79a32df7065cc985f7cc96b1a1e58

Contents?: true

Size: 1.85 KB

Versions: 6

Compression:

Stored size: 1.85 KB

Contents

module GetnetApi
  class Base
    require 'uri'
    require 'net/http'

    def self.build_request endpoint, metodo, body=nil

      url = URI("#{GetnetApi.base_uri}/#{endpoint}")

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

      case metodo
      when "delete"
        request = Net::HTTP::Delete.new(url)
      when "get"
        request = Net::HTTP::Get.new(url)
      when "put"
        request = Net::HTTP::Put.new(url)
      when "post"
        request = Net::HTTP::Post.new(url)
      end
      request = GetnetApi::Base.default_headers request

      request.body = body.to_json
      return http.request(request)
    end

    def self.default_headers request
      request["authorization"] = "Bearer #{GetnetApi::Base.valid_bearer}"
      request["Content-Type"] = "application/json"
      request["seller_id"] = "#{GetnetApi.seller_id}"
      return request
    end

    def self.get_token_de_bearer
      url = URI("#{GetnetApi.endpoint}/auth/oauth/v2/token")
      http = Net::HTTP.new(url.host, url.port)
      http.use_ssl = true
      request = Net::HTTP::Post.new(url)
      request.basic_auth "#{GetnetApi.client_id}", "#{GetnetApi.client_secret}"
      request["Content-Type"] = "application/x-www-form-urlencoded"
      hash =  {
                "scope" => 'oob',
                "grant_type" => 'client_credentials',
              }
      request.body = hash.to_query
      response =  http.request(request)
      result = JSON.parse(response.read_body)
      GetnetApi.access_token = result["access_token"]
      GetnetApi.expires_in = DateTime.now + result["expires_in"].to_i.seconds - 60.seconds
    end

    def self.valid_bearer
      if GetnetApi.expires_in > DateTime.now
        return GetnetApi.access_token
      else
        GetnetApi::Base.get_token_de_bearer
        return GetnetApi.access_token
      end
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
getnet_api-1.0.3 lib/getnet_api/base.rb
getnet_api-1.0.2 lib/getnet_api/base.rb
getnet_api-1.0.1 lib/getnet_api/base.rb
getnet_api-1.0.0 lib/getnet_api/base.rb
getnet_api-0.1.2 lib/getnet_api/base.rb
getnet_api-0.1.1 lib/getnet_api/base.rb