Consuming Non-Rest APIs in Rails with Activeresource

Consuming non-REST APIs in Rails with ActiveResource

I don't think there is a way to do this with ActiveResource, for these cases I just use Net::HTTP and Nokogiri

Consume non-Rails REST API with ActiveResource

I had the exact same problem. I finally had to put everything in the controller to get it to talk to ALM. It's not the best but it works. Here's the Index action in the ReleaseCycles controller:

 def index
conn=getAuthenticatedCurl
conn.url="#{$HPQC_REST_URL}/release-cycles"
conn.perform
results=conn.response_code
hash=Hash.from_xml(conn.body_str)
respond_to do |format|
format.html { render :xml => hash }
format.xml { render :xml => hash }
format.json { render :json => hash }
end
conn.url=$HPQC_LOGOUT_URL
conn.perform
conn.close
return results
end

I created the get "getAuthenticatedCurl" in the ApplicationController. It looks like:

  $HPQC_HOST = "http://<your_alm_server>:8080"
$HPQC_REST_URL = "#{$HPQC_HOST}/qcbin/rest/domains/<DOMAIN>/projects/<PROJECT>"
$HPQC_LOGIN_URL = "#{$HPQC_HOST}/qcbin/authentication-point/authenticate"
$HPQC_LOGOUT_URL = "#{$HPQC_HOST}/qcbin/authentication-point/logout"

def getAuthenticatedCurl
@_conn = Curl::Easy.new($HPQC_LOGIN_URL)
@_conn.verbose = true
@_conn.http_auth_types = :basic
@_conn.userpwd = '<username>:<password>'
@_conn.enable_cookies = true
@_conn.cookiejar = 'cookies.txt'
@_conn.perform #creates the first cookie instance, which allows subsequent calls to the HPQC API
puts "connected...."
return @_conn
end

It's not pretty but it works and it's fast. My next step is to do the same thing as an ActiveResource. Hope it helps and good luck!

Wrong URL when consuming existing Java REST API using rails ActiveResource

The self.element_name = "api" is fixing the pluralization when you are doing a find(:first) or similar. What you need is self.collection_name = "api". That should fix the URL it is creating.

Rails, ActiveResource, and Pagination

I did it with kaminari and some patch to activeresource,
here is gem with example
https://github.com/Fivell/activeresource-response

ActiveResource + Caching

Since you receive a new instance of a controller for each request, caching like that is not going to work. I'm presuming you have something like this:

def show
@resource ||= expensive_request
end

What are you expecting @resource to be when that method executes? The result of something from a previous call to show? Not going to happen.

What you need to do is put things into Rails.cache if you want them to persist between requests. If you use memcached as a back end, this can be very efficient. See the Caching with Rails guide for more information around section 2.5.

In general, a better approach than the ||= method of caching is to define protected methods that are wrapped with memoize to save the results. For example:

def load_resource
expensive_request
end
memoize :load_resource

Remember that this will only apply to subsequent calls to that method within the context of the same request and is not the same as using Rails.cache.

Non rails style format of XML response how to parse with ActiveResource correctly?

You should use custom formatter for your needs, something like this may be good solution.

class PrestaXMLFormatter
include ActiveResource::Formats::XmlFormat

def decode(xml)
ActiveResource::Formats::XmlFormat.decode(xml)['prestashop']
end
end

class Order < ActiveResource::Base
self.format = PrestaXMLFormatter.new
end

How do I change a XML tag when I do update_attributes to a RESTful API?

I'd actually be quite surprised if your problem will be resolved by simply renaming or wrapping inside a parent node and I'm assuming you are going to run into a more issues with your service as you get closer to a solution. I've (unfortunately) been in this boat before so I'm writing what I've learned.

In general, I've found it somewhat difficult to deal with services through ActiveResource when the service expects some complex or otherwise ugly XML. My solutions in these cases inevitably end up overriding the object's to_xml method since the create (POST) or update (PUT) should internally call the object's to_xml to serialize your data into XML. Said methods could either utilize ruby's xml builder or you could also use a 'here document' for even more tricky stuff. I think the bottom line is, you have to know exactly what your service is expecting and build that structure to send off to the service.

For example using 'here document' to force an unfriendly xml structure:

class User << ActiveResource::Base

def to_xml
xml =<<-EOS
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<platform_user>
<user xsi:type="userData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<userId>#{self.id}</userId>
<userName>#{self.name}</userName>
<addresses>
<address>
<address1>#{self.address1}</address1>
<addressZip>#{self.zip}</addressZip>
</address>
</addresses>
</user>
</platform_user>
EOS
xml.gsub(/\n|\t|\r/,"").strip #clean it up.
end
end

You could also use XML Builder it it works for your structure, for example:

def to_xml
xml = Builder::XmlMarkup.new()
xml.instruct! :xml, :version => "1.0", :encoding => "UTF-8", :standalone=>"yes"
xml.platform_user do
xml.user do
xml.userId self.id
...
end
end
end

Good luck.

Rails model talking to a REST service

Have you considered using Active Resource? It is probably meant for almost exactly this use case, if I understand correctly.



Related Topics



Leave a reply



Submit