Keep unveiled credentials in Elasticsearch (using jdbc-river) - ruby-on-rails

I use the jdbc-river to fill my Elasticsearch instance from a PostgreSQL database. The river's record is created with the following ruby's code (since I query ES from a Rails app):
require 'elasticsearch'
client = Elasticsearch::Client.new
client.create :index => "_river", :type => "ldi", :id => "_meta", :body =>
{
:type => :jdbc,
:jdbc => {
:driver => "org.postgresql.Driver",
:url => "jdbc:postgresql://localhost:5432/" + ENV['DB_NAME'],
:user => ENV['DB_USER'],
:password => ENV['DB_PASS'],
:index => ENV['DB_NAME'],
:type => "ldi",
:sql => "select id as _id, * from ldis"
}
}
I'm using envirnoment variables for the database credentials to avoid showing the actual ones. The problem is that once the record is added to ES, actual credentials are unveiled. Thus, you can query ES and obtain something like this:
"hits": {
"total": 6,
"max_score": 1,
"hits": [
{
"_index": "_river",
"_type": "ldi",
"_id": "_meta",
"_score": 1,
"_source": {
"type": "jdbc",
"jdbc": {
"driver": "org.postgresql.Driver",
"url": "jdbc:postgresql://localhost:5432/any_dbname",
"user": "any_dbuser",
"password": "any_dbpass",
"index": "any_index",
"type": "ldi",
"sql": "select id as _id, * from ldis"
}
}
}
....
Is there any way to keep them in secret?

Related

How to set applicaion fee to zero for reccurring payments using Stripe::Plan API

I have been asked to set up an application fee of zero as per my Client's request. I was successfully able to do it using Stripe:Charge create by simply not passing the application fee.
However, with recurring payment we use a different API which is Stripe::Plan.create. This does not allow me to set the application fee to zero, so my recurring payments are been charged a certain amount of fee
Shared below is the code for recurring payment:
# on recurring retrieve application fee
def retrieve_invoice_application_fee(invoice_id)
begin
invoice_response = Stripe::Invoice.retrieve(invoice_id, :stripe_account => candidate.stripe_gateway_id, stripe_version: '2019-12-03')
application_fee = 0
rescue StandardError => e
puts "************ Invoice retrieve #{e} ************"
end
application_fee
end
def create_or_find_stripe_plan
begin
response = Stripe::Plan.retrieve("#{candidate.stripe_gateway_id}-#{amount}",{:stripe_account => candidate.stripe_gateway_id, stripe_version: '2019-12-03',})
rescue => e
response = e
end
if response.inspect.to_s.downcase.include?("no such plan")
response = Stripe::Plan.create({
:amount => amount_cents,
:interval => 'month',
:interval_count => 1,
:currency => candidate.candidate_country[candidate.country.to_s.to_sym][:currency],
product: {name: "#{candidate.stripe_gateway_id}-#{amount}", statement_descriptor: "test statement", },
},
:stripe_account => candidate.stripe_gateway_id, stripe_version: '2019-12-03',
)
end
response
end
I suppose the billing_scheme parameter can help me fix this. Shared below are the debugged logs for a test paument
=> #<Stripe::Plan:0x3fb492612ffc id=plan_LxGnco3QDxIUyR> JSON: {
"id": "plan_LxGnco3QDxIUyR",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 5000,
"amount_decimal": "5000",
"billing_scheme": "per_unit",
"created": 1656352923,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LxGnqDEOfY6Z8a",
"tiers": null,
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type"

How to post nested JSON to HTTParty in Ruby on Rails

I am trying to work out the correct way to post a nested JSON object to an API using HTTParty.
I am getting a successful response using Postman to test the call:
POST: http://service.net/api
Headers: x-api-key : apikey123
Body :
{
"VehicleRequests": [{
"Id": "Vehicle1",
"Parameters": {
"Term": 60,
"CashDeposit": 10,
"DepositType": "Percentage",
"AnnualMileage": 10000
},
"PhysicalVehicle": {
"ExternalVehicleId": "12345",
"Type": "Car",
"Status": "PreOwned",
"OnTheRoadPrice": "30000",
"Mileage": "12345",
"Registration": {
"RegistrationNumber": "REGN0",
"DateRegisteredWithDvla": "01/01/2018"
}
}
}]
}
This returns:
{
"Vehicles": [
{
"Id": "Vehicle1",
"HasError": false,
"Error": null,
"FinanceQuotations": [
{
"HasError": false,
"Error": null,
"Finance": {
"Key": "HP",
"Notifications": [],
"Quote": {
.....
}
}
}
}
]
}
But i'm struggling to replicate the call from my rails app. I have a class set up which i'm calling on create
class Monthlyprice
def initialize()
#response = HTTParty.post('http://service.net/api',
:body =>{
:VehicleRequests=> [{
:Id => "Vehicle1",
:Parameters => {
:Term => 60,
:CashDeposit => 10,
:DepositType => "Percentage",
:AnnualMileage => 10000
},
:PhysicalVehicle => {
:ExternalVehicleId => "12345",
:Type => "Car",
:Status => "PreOwned",
:OnTheRoadPrice => "30000",
:Mileage => "12345",
:Registration => {
:RegistrationNumber => "REGN0",
:DateRegisteredWithDvla => "01/01/2018"
}
}
}].to_json
},
:headers => {"x-api-key" => "apikey123"})
puts(#response)
end
end
But this is returning the following error message from the API:
{"Error"=>{"UserMessage"=>"Request is invalid.", "TechnicalMessage"=>"Request Validation failed. Request had 2 error(s). 1: request.VehicleRequests[0].Id - The Id field is required.\r\n2: request.VehicleRequests[0].Parameters - The Parameters field is required.", "Code"=>"80000"}}
This is the same error that I get from the api in postman if I remove the Id and Parameters objects which suggests the contents of my VehicleRequests object is formatted incorrectly? Any advice would be great!
Can you please change the syntax like below :-
:body => {
}.to_json
that means you have to use .to_json where the body parenthesis close I think it's only syntax error.
Syntax :-
response = HTTParty.post("your request URL",
headers: {
.....
#your header content
.....
},
body: {
......
#your body content
.....
}.to_json
)
I have just edited in your code please try below code :-
#response = HTTParty.post('http://service.net/api',
:headers => {"x-api-key" => "apikey123"},
:body =>{
:VehicleRequests=> [{
:Id => "Vehicle1",
:Parameters => {
:Term => 60,
:CashDeposit => 10,
:DepositType => "Percentage",
:AnnualMileage => 10000
},
:PhysicalVehicle => {
:ExternalVehicleId => "12345",
:Type => "Car",
:Status => "PreOwned",
:OnTheRoadPrice => "30000",
:Mileage => "12345",
:Registration => {
:RegistrationNumber => "REGN0",
:DateRegisteredWithDvla => "01/01/2018"
}
}
}]
}.to_json
)
Hope this will help you :)

Google Analytics Referral - Ruby

I am using ruby, and attempting to get referrals from google analytics api. Here is what I have set up:
sa_referral = client.execute(:api_method => analytics.data.ga.get, :parameters => {
'ids' => "ga:" + saprofileID,
'dimensions' => "ga:fullreferrer",
'metrics' => "ga:users",
'sort' => "-ga:users",
'filters' => "ga:source!=(direct);",
'start-date' => startDate,
'end-date' => endDate,
})
sa_referral_data = sa_referral do |row|
row = {
:referral => row['0'],
:members => row['1'],
}
end
send_event('sa_top_referrals', current: sa_referral_data)
This returns no data when called in the widget using sa_top_referrals. Below is the data the API is returning.
"columnHeaders": [
{
"name": "ga:fullreferrer",
"columnType": "DIMENSION",
"dataType": "STRING"
},
{
"name": "ga:users",
"columnType": "METRIC",
"dataType": "INTEGER"
}
],
"totalsForAllResults": {
"ga:users": "35638"
},
"rows": [
[
"m.facebook.com/",
"1009"
],
[
"baidu",
"912"
],
[
"usasexguide.info/forum/showthread.php",
"613"
],
Ideally the information I am looking to pull down is the URL ex: m.facebook.com/ and the user count or "613". Those are the two items I am looking to pull. My question is how do I know what row those are equal to. Above i'm sending it using: :referral => row['0'], I'd assume the issue is that its not actually row 0, is there a way I can confirm this?
This should do it:
sa_referral_data = sa_referral['rows'] do |row|
rows.map{|r| { referrals:r[0], members:r[1] }}
end

Mandrill- Attachments not sending attachment

I am attempting to send a small rtf attachment through Mandrill. I have created the following json and tried it using the API test page. The attachment is base 64 encoded. The API reports no error and the email comes through but with no attachment. What am I doing wrong?
{
"attachments": [
{
"type": "application/rtf",
"name": "test.rtf",
"content": "e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZGVmZjBcZGVmbGFuZzIwNTd7XGZvbnR0Ymx7XGYwXGZuaWxcZmNoYXJzZXQwIENhbGlicmk7fX0NCntcKlxnZW5lcmF0b3IgTXNmdGVkaXQgNS40MS4yMS4yNTEwO31cdmlld2tpbmQ0XHVjMVxwYXJkXHNhMjAwXHNsMjc2XHNsbXVsdDFcbGFuZzlcZjBcZnMyMiB0aGlzIGlzIGEgdGVzdCBzZW5kaW5nIGZpbGVccGFyDQp9DQoA"
}
],
"message": {
"html": "<html>\r\n<body>test data</body>\r\n</html>\r\n",
"subject": "Cloud Demo",
"from_email": "jklovanc#hotmail.com",
"preserve_recipients": true,
"text": "",
"to": [
{
"type": "to",
"name": "",
"email": "jklovanc#hotmail.com"
}
],
"from_name": "",
"headers": {
"reply-to": "jklovanc#hotmail.com"
}
},
"key": #mykey#,
"async": false
}
Attachments are part of the message object, so the attachments parameter should be nested under the message instead of at the same level. It should look like this instead:
{
"message": {
"attachments": [
{
"type": "application/rtf",
"name": "test.rtf",
"content": "e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZGVmZjBcZGVmbGFuZzIwNTd7XGZvbnR0Ymx7XGYwXGZuaWxcZmNoYXJzZXQwIENhbGlicmk7fX0NCntcKlxnZW5lcmF0b3IgTXNmdGVkaXQgNS40MS4yMS4yNTEwO31cdmlld2tpbmQ0XHVjMVxwYXJkXHNhMjAwXHNsMjc2XHNsbXVsdDFcbGFuZzlcZjBcZnMyMiB0aGlzIGlzIGEgdGVzdCBzZW5kaW5nIGZpbGVccGFyDQp9DQoA"
}
],
"html": "<html>\r\n<body>test data</body>\r\n</html>\r\n",
....
<?php
//It works for me! good luck
/*LIBS*/
include 'lib/mandrill-api-php/src/Mandrill.php';
$mandrill = new Mandrill('YOUR API KEY HERE');
/*ADMIN AND USER EMAIL*/
$admin_email = 'your_email#your_domain.com';
$client_email = 'the_email_of_the_client#mail.com';
/*attach PDF with base64_encode */
$attachment = file_get_contents('the_route_to_your_pdf'); // https://yourdomain/pdf_folder/mypdf.pdf
$attachment_encoded = base64_encode($attachment);
try{
$user_message = array(
'subject' => 'Your subject',
'from_email' => $admin_email,
'from_name' => 'my_domain_for_example',
'html' => '<p>HTML template</p>',
'to' => array(array('email' => $client_email, 'name' => 'Recipient 1')),
'merge_vars' => array(array(
'rcpt' => 'recipient1#domain.com',
'vars' =>
array(
array(
'name' => 'FIRSTNAME',
'content' => 'Recipient 1 first name'),
array(
'name' => 'LASTNAME',
'content' => 'Last name')
))),
'attachments' => array(
array(
'content' => $attachment_encoded,
'type' => "application/pdf",
'name' => 'the_name_of_the_attach.pdf',
))
);
$res_user_mandrill = $mandrill->messages->send($user_message, $async=false, $ip_pool=null, $send_at=null);
} catch(Mandrill_Error $e) {
}
?>

:include based on condition on included table

I have a model Client which has many :tours, association in it. I am using this code to provide all the clients with their tours,
render :json => Client.all(:include => :tours)
Now, the requirement has changed such that this object representation loads only tours of a user. Each tour is associated with an user with relation User has many :tours. I tried
render :json => Client.all(:include => :tours, :conditions => ["tours.user_id = ?", params[:user_id]])
This gives me the clients having tours of that user, but lists all the tours of these clients. But, I want only tours of that user only to be listed under each client. Can I do that using :includes?
client.rb
has_many :tours, :dependent => :destroy
user.rb
has_may :tours, :dependent => :destroy
Update
I thought it would be better to add an example to explain my problem. Suppose, there are 3 clients A, B, C.
Client A has 3 tours (all of user user1)
Client B has 4 tours (none of user user1)
Client C has 3 tours (1 of user user1, others of other users)
Now if we use my method to get the response for user1, it will be like this:
[
{
"name": "Client A",
....
"tours": [
{
"name": "Tour1",
....
},
{
"name": "Tour2",
......
},
{
"name": "Tour3",
.....
}
]
},
{
"name": "Client C",
....
"tours": [
{
"name": "Tour4",
...
},
{
"name": "Tour5",
...
},
{
"name": "Tour6",
...
}
]
}
]
You can see that Client B has been omitted, and Client A and Client C has been included, which is correct. But, for Client C, only Tour5 belongs to user user1. But, it has all its tours included. I want my response to omit Tour5 and Tour6, like this:
[
{
"name": "Client A",
....
"tours": [
{
"name": "Tour1",
....
},
{
"name": "Tour2",
......
},
{
"name": "Tour3",
.....
}
]
},
{
"name": "Client C",
....
"tours": [
{
"name": "Tour4",
...
}
]
}
]
I'm only guessing but have you tried something along the lines of:
render :json => Client.all(:include => {:tours => :user}, :conditions => ["users.id = ?", params[:user_id]])

Resources