How do I tell PG use the database.yml file? - ruby-on-rails

Here is a simple database connection, and query thats working for me;
postgre_conn = PG::Connection.open(:host => 'dbsvr', :user => 'csdashboard', :password => 'csdashboard', :dbname => 'csdashboard')
#filtered_snort_detail_query = postgre_conn.exec("SELECT * from event where timestamp >= (now() - '1 day'::INTERVAL);")
How can I tell pg to use the database.yml file for connection information?
#
#
Closed issue
#
#
I'm sorry to waist everyones time, I think I was doing this all wrong. Since this is a database function, I think it needs to be defined by a model first. Very sorry for that...

Related

Affiliate Links using GEM: rack-affiliates with localhost - not working

If one could kindly help or advise me, i would be very grateful. I am trying to get the gem "rack-affiliates" to work but unfortunately it is not working and there is not enough help nor documentation out there.
i have bundle installed the gem "rack-affiliates"
i have created the table "affiliates" with the columns name:string, tag:string
for the table "users" i have the columns ref:string, affiliate_tag:string, affiliate_tag_ref:string
the affiliate_tag is automatically produced when a user signups on the platform with the below method is the usr.rb file
class User < ApplicationRecord
before_create :generate_affiliate_tag
def generate_affiliate_tag
begin
reference_length = 6
self.affiliate_tag = "SPz_" + Devise.friendly_token.first(reference_length).tr('+/=-', '0aZ')
end while self.class.exists?(affiliate_tag: affiliate_tag)
end
end
therefore example when user_A registers on the platform, user_A will receive an affiliate_tag: SPz_bpQFGq
with this the user_A [affiliate user] will have the affiliate link: http://localhost:3000/users/sign_up?ref=SPz_bpQFGq
When user_A gives the affiliate link to user_B. After user_B has signuped, i would like the ref in the url SPz_bpQFGq to be stored under the column affiliate_tag_ref:string for user_B (under the users table)
i have done all that the documentation requested:
bundle installed the gem "rack-affiliates"
updated the file config/application.rb with the below code:
but i am sure why it is not working. If i am doing ti all wrong, you advise and guidance will be much much appreciated.
Or if one could direct me to a better documentation on how to build affiliate links and track them successfully i would be grateful
Try
config.middleware.use Rack::Affiliates, { :param => 'ref', :domain => 'localhost', :path => "/" }
instead of
config.middleware.use Rack::Affiliates, { :param => 'ref', :domain => '.localhost', :path => "/" }

rackspace private containers via fog gem

I'm looking for someone who can give me some tips - or, ideally, knows where to find a step-by-step guide or something - for working with private rackspace containers (via temp URL) using Fog in a Rails app. I've got fairly far using just their documentation, but none of the temp URLs I generate seem to be valid (401 errors).
Anyone have any tips? I know this is fairly vague, but was hoping there might be a comprehensive guide out there or something - wasn't able to find one via googling around.
Thanks!
EDITED
So in response to a comment, I tried following the directions from the getting started guide exactly. When I go to the URL returned by the code below, I get ERR_CONNECTION_REFUSED. Any ideas?
require "fog"
#storage = Fog::Storage.new(:rackspace_username => '{myUsername}',
:rackspace_api_key => '{myAPIKey}',
:rackspace_region => '{myRegion}',
:provider => 'Rackspace')
directory = #storage.directories.get('{myContainer}')
directory.public = false
directory.save
file = directory.files.create(
:key => 'somefile.txt',
:body => 'Rackspace is awesome!'
)
account = #storage.account
account.meta_temp_url_key = '{myTempUrlKey}'
account.save
#storage = Fog::Storage.new(:rackspace_username => '{myUsername}',
:rackspace_api_key => '{myAPIKey}',
:rackspace_region => '{myRegion}',
:rackspace_temp_url_key => '{myTempUrlKey}',
:provider => 'Rackspace')
directory = #storage.directories.get('{myContainer}')
file = directory.files.get('somefile.txt')
temp_url = file.url(Time.now.to_i + 1000000)
puts temp_url
SOLVED
By getting rid of the directory, file, and temp_url variables at the end and instead using
#storage.get_object_https_url('{myContainer}', 'somefile.txt', Time.now + 60)
which was found in the fog source here.

Ldap search in net-ldap Ruby Rails [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am trying to search ldap. This is doing successful authentication, I verified using a correct and incorrect password. Ldap connection is successful. Then when I make a search query it cannot read attributes from LDAP (AD). What is wrong here? Does ldap/AD has any restrictions/permissions on who can what?
#!/usr/bin/env ruby
require "net-ldap"
$username = String.new
class ActiveDirectoryUser
SERVER = '10.10.10.10'
PORT = 389
BASE = 'DC=mydomain,DC=com'
DOMAIN = 'mydomain.com'
def self.authenticate(login, pass)
conn = Net::LDAP.new :host => SERVER,
:port => PORT,
:base => BASE,
:auth => { :username => "#{login}##{DOMAIN}",
:password => pass,
:method => :simple }
if conn.bind
conn.search(
:base => BASE, :filter => Net::LDAP::Filter.eq(
"sAMAccountName", login ),
:attributes => %w[ givenName ], :return_result => true) do
|entry|
puts "givenName: #{entry.givenName}"
$username = entry.givenName
end
return true
else
return false
end
rescue Net::LDAP::LdapError => e
return false
end
end
if ActiveDirectoryUser.authenticate('myusername', 'mypassword')
puts "Authentication Successful! The user is "+$username
#### I get this,but blank username
else
puts "Authentication FAILED!"
end
-----------
# ./ad.rb
Authentication Successful! The user is
Thanks Terry for the answer.
It was a small issue. I was missing some details in the treebase. It is working now.
LDAP-compliant directory servers must provide access control, so it is possible that the LDAP client:
cannot search for an entry
cannot read any attributes from an entry
cannot read some attributes from an entry
It is also possible the entry for which the LDAP client searches does not exist - clients cannot read attributes from entries that do not exist.
The application code should check that the entry in question exists using command-line tools if questions exist as to whether the authentication was successful or whether an entry exists. For information about the command-line ldapsearch tool, see LDAP: using ldapsearch.

PostgreSQL and Heroku, find and group

I'm trying to get an app to run on Heroku properly. (Heroku uses the postgreSQL database, yeh?)
In development, I'm using sqlite, and this is my code in a controller =>
#productsort = Products.find(:all,
:select => 'count(*) count, color',
:group => 'color',
:order => 'count DESC',
:conditions => 'size = "Small"')
As you can see, I'm trying to group products by their colors, and order them by greatest amount to least.
Also, the products must be "Small". (the conditions)
In SQL, it works fine.
But not in PostgreSQL (heroku).
This is from running "heroku log"
2011-06-20T18:20:33+00:00 app[web.1]: ActiveRecord::StatementInvalid (PGError: ERROR: column "Small" does not exist
2011-06-20T18:20:33+00:00 app[web.1]: LINE 1: ...ducts".* FROM "products" WHERE (size = "Smal...
Hm... I've searched around and I couldn't find anything similar to what I have.
All help would be appreicated. Thank you
You need to be using single quotes around your strings in the conditions (double quotes may work with sqlite, but they definitely don't with PostgreSQL).
So replace your conditions with this:
:conditions => "size = 'Small'"
It will still work in SQLite too.

ArgumentError on application requests

I've written a basic Rails 3 application that shows a form and an upload form on specific URLs. It was all working fine yesterday, but now I'm running into several problems that require fixing. I'll try to describe each problem as best as I can. The reason i'm combining them, is because I feel they're all related and preventing me from finishing my task.
1. Cannot run the application in development mode
For some unknown reason, I cannot get the application to run in development mode. Currently i've overwritten the production.rb file from the environment with the settings from the development environment to get actuall stacktraces.
I've added the RailsEnv production setting to my VirtualHost setting in apache2, but it seems to make no difference. Nor does settings ENV variable to production.
2. ArgumentError on all calls
Whatever call I seem to make, results in this error message. The logfile tells me the following:
Started GET "/" for 192.168.33.82 at
Thu Apr 07 00:54:48 -0700 2011
ArgumentError (wrong number of
arguments (1 for 0)):
Rendered
/usr/lib/ruby/gems/1.8/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.erb
(1.0ms) Rendered
/usr/lib/ruby/gems/1.8/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb
(4.1ms) Rendered
/usr/lib/ruby/gems/1.8/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb
within rescues/layout (8.4ms)
This means nothing to me really. I have no clue what's going wrong. I currently have only one controller which looks like this:
class SearchEngineController < ApplicationController
def upload
end
def search
#rows = nil
end
# This function will receive the query string from the search form and perform a search on the
# F.I.S.E index to find any matching results
def query
index = Ferret::Index::Index.new :path => "/public/F.I.S.E", :default_field => 'content'
#rows = Array.New
index.search_each "content|title:#{params[:query]}" do |id,score, title|
#rows << {:id => id, :score => score, :title => title}
end
render :search
end
# This function will receive the file uploaded by the user and process it into the
# F.I.S.E for searching on keywords and synonims
def process
index = Ferret::Index::Index.new :path => "public/F.I.S.E", :default_field => 'content'
file = File.open params[:file], "r"
xml = REXML::Document.new file
filename = params[:file]
title = xml.root.elements['//body/title/text()']
content = xml.root.elements['normalize-space(//body)']
index << { :filename => filename, :title => title, :content => content}
file.close
FileUtils.rm file
end
end
The routing of my application has the following setup: Again this is all pretty basic and probably can be done better.
Roularta::Application.routes.draw do
# define all the url paths we support
match '/upload' => 'search_engine#upload', :via => :get
match '/process' => 'search_engine#process', :via => :post
# redirect the root of the application to the search page
root :to => 'search_engine#search'
# redirect all incoming requests to the query view of the search engine
match '/:controller(/:action(/:id))' => 'search_engine#search'
end
If anyone can spot what's wrong and why this application is failing, please let me know. If needed I can edit this awnser and include additional files that might be required to solve this problem.
EDIT
i've managed to get further by renaming one of the functions on the controller. I renamed search into create and now I'm getting back HAML errors. Perhaps I used a keyword...?
woot, finally found the solutions....
Seems I used keywords to define my actions, and Rails didn't like this. This solved issue 2.
Issue 1 got solved by adding Rails.env= 'development' to the environment.rb file

Resources