Is there a Gem for simplifying POST to API via curl - ruby-on-rails

I have seen API's such as https://stripe.com/ or the new https://pin.net.au/ that use a very simple and convenient POST approach. Instead of sending the object through JSON it send it as post attributes and values and the return is json.
curl https://api.pin.net.au/1/charges \
-u your-api-key: \
-d "amount=400" \
-d "description=test charge" \
-d "email=roland#pin.net.au" \
-d "ip_address=203.192.1.172" \
-d "card[number]=5123456789012346" \
My question there any Gem available to do this on Rails? I can understand what's going on underneath this, but I wanted to make sure if there's a Gem that do this seamless without further changing my current default implementation.

Are you asking how to create or consume such APIs?
To consume:
require 'rest-client'
require 'json'
resp = RestClient.post(
'https://vtUQeOtUnYr7PGCLQ96Ul4zqpDUO4sOE:#api.stripe.com/v1/charges',
{
:amount => 400,
:currency => 'usd',
:description => "charge for site#stripe.com",
:card => {
:number => '4242424242424242',
:exp_month => 12,
:exp_year => 2012,
:cvc => 123
}
})
JSON.parse(resp)
For creating these APIs you can use something like:
https://github.com/intridea/grape
https://github.com/filtersquad/rocket_pants

Related

Sending the parent object to the Box File Upload API in Ruby on Rails

I'm attempting to use the Box File Upload API to send a file to Box.com from a Ruby on Rails application. However, I keep getting a bad request response that says parent is a missing parameter.
Here is their documentation:
https://developer.box.com/v2.0/reference#upload-a-file
Here is their curl example:
curl https://upload.box.com/api/2.0/files/content \
-H "Authorization: Bearer ACCESS_TOKEN" -X POST \
-F attributes='{"name":"tigers.jpeg", "parent":{"id":"11446498"}}' \
-F file=#myfile.jpg
I'm attempting to use RailsClient to do this in my app and here are the two ways I've tried:
RestClient.post(BASE_CONTENT_URL,{ :name => "randompdf.pdf", :parent => {:id => 0}, :myfile => file }, { :Authorization => "Bearer #{NEW_TOKEN}" })
&
#parent = Struct.new(:id)
#parent.id = 0
RestClient.post(BASE_CONTENT_URL,{ :name => "randompdf.pdf", :parent => #parent, :myfile => file }, { :Authorization => "Bearer #{NEW_TOKEN}" })
I get the same error attempting to do it via Postman as well. Box's Community Forum and Support resources haven't gotten back to me so I'm a little lost.
attributes = {name: #{unique_name}, parent: { id: 0 }}
box_response = RestClient::Request.execute(method: :post,
url: BASE_UPLOAD_URL + "/content",
payload: { attributes: attributes.to_json,
file: file},
headers: {Authorization: "Bearer #{NEW_TOKEN}"})

Passing a curl argumet -T to rest-client

I have an API call for uploading a file and I came across -T option which does the same in curl call.
curl -X PUT "assetUrl" -H "authorization" -H
"x-amz-acl:amzAcl" -H "contentType" -H "x-amz-date:
amzDate" -T path/to/local/file
How can I pass that -T option to rest-client library?
Either of below should work fine
RestClient.post '/data', :myfile => File.new("/path/to/image.jpg",
'rb')
RestClient.post( url,
{
:transfer => {
:path => '/foo/bar',
:owner => 'that_guy',
:group => 'those_guys'
},
:upload => {
:file => File.new(path, 'rb')
}
})

Convert curl command to httparty

I am trying to add merge field in Mailchimp V3 list with HTTParty but not able to convert curl to HTTParty format.
Curl Request format which is working fine :
curl --request POST \
--url 'https://usxx.api.mailchimp.com/3.0/lists/17efad7sd4/merge-fields' \
--user '12:d1c1d99dr5000c63f0f73f64b88e852e-xx' \
--header 'content-type: application/json' \
--data '{"name":"FAVORITEJOKE", "type":"text"}' \
--include
Httparty format with error API key missing
response = HTTParty.post("https://us12.api.mailchimp.com/3.0/lists/17efad7sde/merge-fields",
:body => {
:user => '12:d1c1d99dr5000c63f0f73f64b88e852e-xx',
:data => '{"name":"FAVORITEJOKE", "type":"text"}',
:include => ''
}.to_json,
:headers => { 'Content-Type' => 'application/json' } )
I also try it without include option but not working
There are several errors in your code.
curl user is the basic auth user, but you are passing it in the payload of the request
data is the payload, instead you are passing it as a node in your payload and then you double serialize it
include makes no sense there, it's not a payload item
This should be the correct version. Please take a moment to read the HTTParty and curl documentation and understand the differences.
HTTParty.post(
"https://us12.api.mailchimp.com/3.0/lists/17efad7sde/merge-fields",
basic_auth: { username: "12", password: "d1c1d99dr5000c63f0f73f64b88e852e-xx" },
headers: { 'Content-Type' => 'application/json' },
body: {
name: "FAVORITEJOKE",
type: "text",
}.to_json
)

Convert Curl command line in Rails

This is my curl command which works nicely in Command line :
curl --data #order_new.json \
-H "X-Augury-Token:My_token_goes_here" \
-H "Content-Type:application/json" \
http://staging.hub.spreecommerce.com/api/stores/store_id_goes_here/messages
I need to implement the same in rails using any sort of Gem, Tried with HTTParty /rest_client / spree-api-client, but something wrong here :
require 'httparty'
result = HTTParty.post(
"http://staging.hub.spreecommerce.com/api/stores/52eb347f755b1c97e900001e/messages",
:body => JSON.parse(File.read("order_new.json")),
:header => {
"X-Augury-Token" => "UjjKsdxbrwjpAPx9Hiw4",
"Content-Type" => "application/json"
}
)
But I am getting Error,
"The page you were looking for doesn't exist (404)"
I need rails equivalent of above curl command, use of spree-api-client gem will be much helpful.
If you prefer to use the Spree::API::Client, could you post the results of your findings? Could you evaluate the output of the following commands and post back:
client = Spree::API::Client.new('http://staging.hub.spreecommerce.com/api/', 'UjjKsdxbrwjpAPx9Hiw4')
client.products.inspect
require 'httparty'
result = HTTParty.post(
"http://staging.hub.spreecommerce.com/api/stores/52eb347f755b1c97e900001e/messages",
:body => #order.to_json,
:header => {
"X-Augury-Token" => "UjjKsdxbrwjpAPx9Hiw4",
"Content-Type" => "application/json"
}
)
Don't parse json while passing to HTTParty body

which is the best way to work RAILS with Hbase?

Already install https://github.com/greglu/hbase-stargate
, but it seems, that some methods is not working appropriate,
for example: row = #client.create_row('terms', 'book', Time.now.to_i, {:name => 'data:fr', :value => 2})
In Hbase shell I got empty value:
scan 'terms', {CACHE_BLOCKS => false} ROW COLUMN+CELL book column=data:fr, timestamp=1325880415, value=
Also, some rows does not created at all.
This is my schema for table:
table_options = { :name => 'data', \
:max_versions => 1, \
:compression => Stargate::Model::CompressionType::NONE, \
:in_memory => false, \
:block_cache => false, \
:ttl => -1, \
:max_cell_size => 2147483647
}
client.create_table('terms', table_options)
Whats wrong with hbase-stargate gem?
Which is the best way(fastest) to integrate rails with hbase?
Thank you!
Looks like massive_record might be a better option.

Resources