I 'm working on rails app in which I need to receive array of hashes in API call with Grape like.
{
tournament_id:1
match_id: 10
[
{
team_id: 1
score: 10
},
{
team_id: 2
score: 20
}
]
}
so that I can receive score of each team in single call for a specific match and tournament instead of multiple calls for score of each team.
I have tried multiple things like
group :teams_with_scores, type: Array, desc: "An array of Teams with scores" do
requires :team_id, type: String,desc: "Team ID"
requires :score, type: String,desc: "Score"
end
But don't have a clue that how to do it.
You can send this data as a json string, and then parse this json when you get it:
params do
scores_info, type: String, desc: 'the scores info'
end
get do
scores_info = JSON.parse(params[:scores_info])
end
Related
I have a spec that expects a class to receive a method with some arguments. I only want to check for a subset of key/value pair from the arguement hash :
it 'calls Stripe::Checkout::Session#create with the correct line items' do
expect(Stripe::Checkout::Session).to receive(:create).with({
line_items: [{
price: "bla",
quantity: 1
}],
}
)
subject
end
here I only want to check that line_items is present in the argument hash with the correct value.
However the actual hash received has more values and I get the following error :
#<Stripe::Checkout::Session (class)> received :create with unexpected arguments
expected: ({:line_items=>[{:price=>"bla", :quantity=>1}]}, *(any args))
got: ({:allow_promotion_codes=>true, :automatic_tax=>{:enabled=>true}, :billing_address_collection=>"requir...vh.me/fr/thank-you?checkout_session_id={CHECKOUT_SESSION_ID}", :tax_id_collection=>{:enabled=>true}}, {:stripe_account=>"bla"})
How can I tell rspec to only check for the presence of a subset of key/value pair ?
You want hash_including, which matches subsets of hashes:
...to receive(:create).with(hash_including(
line_items: [{
price: "bla",
quantity: 1
}],
)
See the docs: https://relishapp.com/rspec/rspec-mocks/v/3-2/docs/setting-constraints/matching-arguments
I have been struggling for a few days trying to get queries to work. At the moment my model looks like this:
class Geojson
include Mongoid::Document
field :type, type: String, default: 'Point'
field :coordinates, type: Array
index({coordinates: "2dsphere"}, { bits: 12})
end
The following query returns nil:
Geojson.find(:coordinates => {"$nearSphere" => [-70.1197340629727, 4.67071244438]})
These are the current instances in my database:
[#<Geojson _id: 61b7b21a9eb0c9ef0aa5626d, type: "Point", coordinates: [-74.13041168951031, 4.6638117]>,
#<Geojson _id: 61b7b2619eb0c9ef0aa5626e, type: "Point", coordinates: [-74.1213041168951, 4.5638117]>]
I am able to query similar cases on mongosh with no issues, however I am not sure where the mistake is when doing it directly on rails.
I finally managed to make it work the following way: (for a 2d sphere index)
Geojson.where(:coordinates => {"$nearSphere" => [long, lat]}).to_a
Where longitude and latitude are the parameters received.
I've got some issues, i'm trying to implement subscription with stripe > it works when there is for exemple 3 items in my order > it create a subscription for the 3 items.
The problem is that if the customer wants to stop sub only for ONE element, i dont know how to handle this ...
So i was wondering to create a subscription for each element, this is my code
customer = Stripe::Customer.create
#order.line_items.each do |line_item|
product = Stripe::Product.create(
{
name: line_item.product.name,
metadata: {
product_id: line_item.product.id,
line_item_id: line_item.id
}
}
)
price = Stripe::Price.create(
{
product: product.id,
unit_amount: line_item.product.price_cents,
currency: 'eur',
recurring: {
interval: 'month'
}
}
)
Stripe::Subscription.create({
customer: customer.id,
items: [
{price: price.id, quantity: line_item.quantity}
]
})
but i got this error This customer has no attached payment source or default payment method.
and i dont know how to attach it, even with documentation..
any help please ? thank you
As said in comments; To fix the error in the title:
You need to first have the "payment method", if you haven't already, create one:
Maybe using Stripe.js and it's elements API.
Which nowadays has "payment" element, allowing users to choose their payment-method.
And supports Setup-Intent's "client_secret" (beside Payment-Intent's), submittable using confirmSetup(...) method.
Then (using Stripe API):
Attach said "payment method" to the Customer:
(Optionally) set it as their default for invoices (with invoice_settings.default_payment_method).
And, while creating the subscription, pass the customer (that which you atteched said "payment method" to).
I am using the grape gem version 0.19.0.
I am passing the following data:
{references: [{x: '10'}, {x: '20'}, {x: '30'}]}
The validation section on the server is:
params do
optional :references, type: Array do
optional :x, type: String
end
end
This does not enter my API call on the server and the client gets the following response:
responseText: "{"error":"references is invalid"}"
How do I pass an array of hashes?
I'm having trouble getting rspec to properly test an Active Record callback. In my model I have:
after_create :check_twitter_limit, if: Proc.new { |row| row.service == "twitter" }
Where service is the social media site being saved to the db. In essence, if the event is a twitter event, let's check the rate limit.
The method looks like this:
def check_twitter_limit
binding.pry
if EventTracker.within_fifteen_minutes.twitter.size > 12 && EventTracker.within_fifteen_minutes.twitter.size % 3 == 0
alert_notifier.ping("*[#{user}](#{ANALYTICS_URL}#{distinct_id})* (#{organization}) just _#{event}_ and has made *#{EventTracker.within_fifteen_minutes.twitter.size}* twitter requests in the last 15 minutes!")
end
end
Notice the pry binding. I have this in here as proof that the code works. Every time I run rspec, I get into the pry binding, so I know the method is being called.
Here is the spec:
it "should trigger the twitter alert limit when conditions are met" do
expect(EventTracker).to receive(:check_twitter_limit)
EventTracker.create({
event: "Added twitter users to list",
organization: "Org",
user: "Cool Guy",
event_time_stamp: Time.now.to_i - 14400,
distinct_id: "1",
media_link: "http://www.twitter.com",
is_limited: true,
service: "twitter"
})
end
Here is the error:
Failure/Error: expect(EventTracker).to receive(:check_twitter_limit)
(<EventTracker(id: integer, event: string, organization: string, user: string, event_time_stamp: integer, distinct_id: string, media_link: string, is_limited: boolean, service: string, created_at: datetime, updated_at: datetime) (class)>).check_twitter_limit(any args)
expected: 1 time with any arguments
received: 0 times with any arguments
This test fails despite the fact that I know the callback is being triggered when using pry, and I can confirm that the record is being saved to the db.
Any ideas on what is going on?
I think you want an instance of EventTracker to receive check_twitter_limit, not the class itself. You can do this in RSpec 3.0 with
expect_any_instance_of(EventTracker).to receive(:check_twitter_limit)