Sha256: 94c74b09dc3911ee2593c66f31a4f29dd414f6ce4be9582b2d780e2cad859f29

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'rsa'
require 'openssl'
require 'httparty'

module PlataformaSocial
  class Request
    
    def self.post url, params = {}
      request url, params, 'post'
    end
    
    def self.get url, params = {}
      request url, params, 'get'
    end
    
    def self.request url, params = {}, method = "get"
      secret           = params[:secret].present? ? params[:secret] : PlataformaSocial.secret
      platform_api_key = params[:platform_api_key].present? ? params[:platform_api_key] : PlataformaSocial.api_key
      secret_key       = OpenSSL::PKey::RSA.new(File.read("#{Rails.root}/plataforma_social/keys/public.pem")).public_encrypt secret
      params_name      = method == 'get' ? :query : :body
      params           = { :network_name => "fb", :platform_api_key => platform_api_key, :signature => secret_key }.merge(params)
      
      begin
        response = HTTParty.send(method.to_sym, url, params_name => params)
      rescue => e
        return e.to_s
      end
      
      return nil if response.nil?
      
      if response.parsed_response["data"]
        response = response.parsed_response["data"]
      
        begin
          response = JSON.parse(response) if response.present? && response.is_a?(String)
        rescue JSON::ParserError => e
          response = response
        end
      elsif response.parsed_response["error"]
        response = { "error" => response.parsed_response["error"] }
      end
      
      return response
    end  
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
plataforma_social-0.0.13 lib/plataforma_social/request.rb