I'm new to Ruby and trying to use the Ruby gem 'rest-client' to access the REST API of my accounting system, e-conomic.com. I am able to connect via tokens and e.g. fetch customer details - so far, so good.
However, I'm struggling to figure out how to POST and thus create a new customer entry with e.g. address, name, mail etc.. In particular, I'm looking to get the code to both include my authentication token details (i.e. content of hHeader below), while also including a payload of customer details.
Details about the customer creation via the REST API:
https://restdocs.e-conomic.com/#post-customer-groups
Details about the rest-client ruby gem:
https://github.com/rest-client/rest-client
I'm running Ruby 2.3.3 on Windows 7 in the Atom editor.
My code is as below:
Dir.chdir 'C:\Ruby23\bin'
require 'rest-client'
require 'rconomic'
require 'json'
hHeader = {"X-AppSecretToken" => 'tokenID1_sanitized', "X-AgreementGrantToken" => 'tokenID2_sanitized', "Content-Type" => 'application/json'}
hCustomer = RestClient.get("https://restapi.e-conomic.com/customers/5", hHeader) # => creates a response showing customer 5 (shown for example of GET)
Your input would be much appreciated!
Martin
You put the wrong api doc, it's POST customer, not POST customer-groups. You should send the post with:
body = {'address' => 'Example Street', 'name' => 'John Doe'}.to_json
RestClient.post "https://restapi.e-conomic.com/customers/", body, hHeader)
Related
I'm trying to find some samples in Prestashop API where I can:
Get orders
For each order, get its details
I'm trying to implement an integration but couldn't find a simple way to do that in Ruby
You can use HTTParty:
require 'httparty'
SITE_URL= 'http://example.com'
WEB_SERVICE_KEY = 'EXAMPLE'
response = HTTParty.get "#{SITE_URL}/api/orders?display=full", basic_auth: {username: WEB_SERVICE_KEY }
orders = response['prestashop']['orders']
https://github.com/fullcontact/fullcontact-api-ruby
I'm trying to use the FullContact API Wrapper for Ruby (it's a gem) instead of the pure REST API. I'm trying to figure out how to grab the person's profile pictures from email address. I know how to get them from the REST API that responds with JSON, but not sure what the example code there is doing.
person = FullContact.person(email: "brawest#gmail.com") (pulled from example in the Github linked)
So now how do I retrieve profile pictures from person? What data type is it storing?
The FullContact gem uses Hashie, and from a call it returns a Hashie::Rash object.
So if you were trying to access photos:
> person = FullContact.person(email: "email")
=> [#<Hashie::Rash contact_info=#<Hashie::Rash family_name=...
> person.photos
=> [#<Hashie::Rash is_primary=true type="facebook" type_id="facebook" type_name="Facebook"...
Hope that helps!
I'm still new to Ruby and trying to use the HTTParty gem to help me write an API Wrapper. I feed HTTParty::get a URI and it parses JSON data. From a quick glance and the way the returned result behaves, it looks like a Hash, but is it? I can't seem to find information online. Another post on StackOverflow shows to use HTTParty::get(...).parsed_response to get the Hash, but this seems outdated.
Do this in the console:
>require 'httparty'
=> true
> response = HTTParty.get( "..." )
....
> response.class
=> HTTParty::Response
So the response from HTTParty.get is an HTTParty::Response object.
See this blogpost titled "It's Time To HTTParty!" to learn more about how to work this response.
I am trying to update address/location of a facebook business page through API using koala ruby gem, so far no working solution.
page_access_token = "gw4t3434"
page_api = Koala::Facebook::API.new(page_access_token)
page_api.graph_call('me', {:location => {:street => "my street"}}, 'post') #error. Koala::Facebook::APIError: OAuthException: (#100) Parameters do not match any fields that can be updated
page_api.graph_call('me', {:location => {:address => "my street"}}, 'post') #error. Koala::Facebook::APIError: OAuthException: (#100) Parameters do not match any fields that can be updated
page_api.graph_call('me', {:address => "my street"}}, 'post')# not raise error but not working
page_api.graph_call('me', {:street => "my street"}}, 'post')# not raise error but not working
I can not find clear explanation either in facebook api reference regarding updating address in a page. I may missing something...
You can't write to the location object, only read. See "Updating Page Attributes" in the API. Also, there is no permission to request for writing to a location object.
An alternative is that you write to the Page's about section - this is allowed. Perhaps you can place an address reference here to meet the requirement of making address changes visible to the end user.
I'm trying to use the Ruby version of Mechanize to extract my employer's tickets from a ticket management system that we're moving away from that does not supply an API.
Problem is, it seems Mechanize isn't keeping the cookies between the post call and the get call shown below:
require 'rubygems'
require 'nokogiri'
require 'mechanize'
#agent = Mechanize.new
page = #agent.post('http://<url>.com/user_session', {
'authenticity_token' => '<token>',
'user_session[login]' => '<login>',
'user_session[password]' => '<password>',
'user_session[remember_me]' => '0',
'commit' => 'Login'
})
page = #agent.get 'http://<url>.com/<organization>/<repo-name>/tickets/1'
puts page.title
user_session is the URL to which the site's login page POSTs, and I've verified that this indeed logs me in. But the page that returns from the get call is the 'Oops, you're not logged in!' page.
I've verified that clicking links on the page that returns from the post call works, but I can't actually get to where I need to go without JavaScript. And of course I've done this successfully on the browser with the same login.
What am I doing wrong?
Okay this might help you - first of all what version of mechanize are you using? You need to identify, if this problem is due to the cookies being overwritten/cleaned by mechanize between the requests or if the cookies are wrong/not being set in the first place. You can do that by adding a puts #agent.cookie_jar.jar inbetween the two requests, to see what is stored.
If its a overwriting issue, you might be able to solve it by collecting the cookies from the first request, and applying them to the second. There are many ways to do this:
One way is to just do a temp_jar = agent.cookie_jar.jar an then just going through each cookie and add it again using the .add method
HOWEVER - the easiest way is by just installing the latest 2.1 pre release of mechanize (many fixes), because you will then be able to do it very simply.
To install the latest do a gem install mechanize --pre and make sure to get rid of the old version of mechanize gem uninstall mechanize 'some_version' after this, you can simply do as follows:
require 'rubygems'
require 'nokogiri'
require 'mechanize'
#agent = Mechanize.new
page = #agent.post('http://<url>.com/user_session', {
'authenticity_token' => '<token>',
'user_session[login]' => '<login>',
'user_session[password]' => '<password>',
'user_session[remember_me]' => '0',
'commit' => 'Login'
})
temp_jar = #agent.cookie_jar
#Do whatever you need an use the cookies again in a new session after that
#agent = Mechanize.new
#agent.cookie_jar = temp_jar
page = #agent.get 'http://<url>.com/<organization>/<repo-name>/tickets/1'
puts page.title
BTW the documentation is here http://mechanize.rubyforge.org/index.html
Mechanize would automatically send cookies obtained from the response in the consecutive request. You can use the same agent without re-new.
require 'mechanize'
#agent = Mechanize.new
#agent.post(create_sessions_url, params, headers)
#agent.get(ticket_url)
Tested with mechanize 2.7.6.