Sha256: 3d3bda9827d99e2fb4a505a805dca4e1cd181150ed48c9bac4a3fabbfddb5774

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

require 'uri'
require 'cgi'
require 'net/http'
module RamenRails
  class Import
    def self.all_users
      User.all.each do |u|
        new(user: u).import
      end

      nil
    end

    attr_accessor :user

    def initialize(opts)
      self.user = opts[:user]
    end

    def can_get_labels?
      RamenRails.config.import_user_labels.present?
    end

    def can_get_value?
      RamenRails.config.import_user_value.present?
    end

    def get_labels
      user.
        instance_eval(&RamenRails.config.import_user_labels)
    end

    def get_value
      user.
        instance_eval(&RamenRails.config.import_user_value)
    end

    def import
      obj = {}
      [:email, :name, :id].each do |attr|
        obj[attr] = user.send(attr)
      end

      obj[:labels] = get_labels if can_get_labels?
      obj[:value] = get_value if can_get_value?

      if user.respond_to?(:created_at) && user.send(:created_at).present?
        obj[:created_at] = user.send(:created_at).to_i
      end
      
      ts = Time.now.to_i

      auth_hash = (Digest::SHA256.new << "#{obj[:email]}:#{obj[:id]}:#{obj[:name]}:#{ts}:#{RamenRails.config.organization_secret}").to_s
      
      h = {
        organization_id: RamenRails.config.organization_id,
        user: obj,
        timestamp: ts,
        auth_hash: auth_hash,
        import: true
      }

      json = h.to_json

      endpoint = ENV['RAMEN_IMPORT_URI'] || "https://ramen.is/po.json"
      uri = URI("#{endpoint}?json_payload=#{CGI.escape(json)}")
      start = Time.now.to_f
      resp = Net::HTTP.get_response(uri)
      total = Time.now.to_f - start

      if resp.code == "200"
        puts "Imported #{obj[:name]} <#{obj[:email]}> in #{total} seconds"
      else
        puts "ERROR (#{resp.code}) Importing #{obj[:name]} <#{obj[:email]}>. Continuing...."
        puts resp.body.to_s
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ramen-rails-0.5.0 lib/ramen-rails/import.rb
ramen-rails-0.4.0 lib/ramen-rails/import.rb
ramen-rails-0.3.1 lib/ramen-rails/import.rb