vendredi 24 juillet 2015

uninitialized constant Net::HTTP::SSL_IVNAMES (NameError) RoR

I'm getting this error when I try to start the server.I'm using SDK MercadoPago. The gem that I use is mercadoPago-sdk. What I did was copy the contents of lib and test in my application

http://ift.tt/1LCt0rf

This is my code :

lib/mercadopago.rb

require 'rubygems'
require 'json'
require 'uri'
require 'net/https'
require 'yaml'
require File.dirname(__FILE__) + '/version'
require 'ssl_options_patch'

class MercadoPago
def initialize(*args)
    if args.size < 1 or args.size > 2
        raise "Invalid arguments. Use CLIENT_ID and CLIENT SECRET, or ACCESS_TOKEN"
    end

    @client_id = args.at(0) if args.size == 2
    @client_secret = args.at(1) if args.size == 2
    @ll_access_token = args.at(0) if args.size == 1

    @rest_client = RestClient.new()
    @sandbox = false
end

def set_debug_logger(debug_logger)
    @rest_client.set_debug_logger(debug_logger)
end

def sandbox_mode(enable=nil)
    if not enable.nil?
        @sandbox = enable
    end

    return @sandbox
end

# Get Access Token for API use
def get_access_token
    if @ll_access_token
        @ll_access_token
    else
        app_client_values = {
            'grant_type' => 'client_credentials',
            'client_id' => @client_id,
            'client_secret' => @client_secret
        }

        @access_data = @rest_client.post("/oauth/token", build_query(app_client_values), RestClient::MIME_FORM)

        if @access_data['status'] == "200"
            @access_data = @access_data["response"]
            @access_data['access_token']
        else
            raise @access_data.inspect
        end
    end
end

# Generic resource get
def get(uri, params = nil, authenticate = true)
    if not params.class == Hash
        params = Hash.new
    end

    if authenticate
        begin
            access_token = get_access_token
        rescue => e
            return e.message
        end

        params["access_token"] = access_token
    end

    if not params.empty?
        uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
    end

    @rest_client.get(uri)
end

# Generic resource post
def post(uri, data, params = nil)
    if not params.class == Hash
        params = Hash.new
    end

    begin
        access_token = get_access_token
    rescue => e
        return e.message
    end

    params["access_token"] = access_token

    if not params.empty?
        uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
    end

    @rest_client.post(uri, data)
end

# Generic resource put
def put(uri, data, params = nil)
    if not params.class == Hash
        params = Hash.new
    end

    begin
        access_token = get_access_token
    rescue => e
        return e.message
    end

    params["access_token"] = access_token

    if not params.empty?
        uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
    end

    @rest_client.put(uri, data)
end

# Generic resource delete
def delete(uri, params = nil)
    if not params.class == Hash
        params = Hash.new
    end

    begin
        access_token = get_access_token
    rescue => e
        return e.message
    end

    params["access_token"] = access_token

    if not params.empty?
        uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
    end

    @rest_client.delete(uri)
end

def build_query(params)
    URI.escape(params.collect { |k, v| "#{k}=#{v}" }.join('&'))
end

private

class RestClient

    MIME_JSON = 'application/json'
    MIME_FORM = 'application/x-www-form-urlencoded'
    API_BASE_URL = URI.parse('http://ift.tt/1LCt0rh')

    def initialize(debug_logger=nil)
        @http = Net::HTTP.new(API_BASE_URL.host, API_BASE_URL.port)

        if API_BASE_URL.scheme == "https" # enable SSL/TLS
            @http.use_ssl = true
            @http.ssl_options = OpenSSL::SSL::OP_NO_SSLv3 # explicitly tell OpenSSL not to use SSL3
        end

        @http.set_debug_output debug_logger if debug_logger
    end

    def set_debug_logger(debug_logger)
        @http.set_debug_output debug_logger
    end

    def exec(method, uri, data, content_type)
        if not data.nil? and content_type == MIME_JSON
            data = data.to_json
        end

        headers = {
            'User-Agent' => "MercadoPago Ruby SDK v" + MERCADO_PAGO_VERSION,
            'Content-type' => content_type,
            'Accept' => MIME_JSON
        }

        api_result = @http.send_request(method, uri, data, headers)

        {
            "status" => api_result.code,
            "response" => JSON.parse(api_result.body)
        }
    end

    def get(uri, content_type=MIME_JSON)
        exec("GET", uri, nil, content_type)
    end

    def post(uri, data = nil, content_type=MIME_JSON)
        exec("POST", uri, data, content_type)
    end

    def put(uri, data = nil, content_type=MIME_JSON)
        exec("PUT", uri, data, content_type)
    end

    def delete(uri, content_type=MIME_JSON)
        exec("DELETE", uri, nil, content_type)
    end
end

end

lib/ssl_options_patch.rb

require 'net/http'

(Net::HTTP::SSL_IVNAMES << :@ssl_options).uniq!
(Net::HTTP::SSL_ATTRIBUTES << :options).uniq!

Net::HTTP.class_eval do
  attr_accessor :ssl_options
end

Aucun commentaire:

Enregistrer un commentaire