Parsing HTTParty response
Do you have any code that is throwing an error? The parsed_response
variable from the HTTParty response is a hash, not an array. It contains one key, "data"
(the string, NOT the symbol). The value for the "data"
key in the hash is an array of hashes, so you would iterate as such:
data = ret.parsed_response["data"]
data.each do |item|
puts item["name"]
puts item["category"]
puts item["id"]
# etc
end
Parse JSON from HTTParty using Ruby on Rails
HTTParty.get
returns an encapsulated response type of class HTTParty::Response
, you need to retrieve the parsed response
from that by:
response = HTTParty.get(
'http://codekyt.in/froodle-wp/wp-json/data/v2/projects?client_key=12345',
:headers =>{'Content-Type' => 'application/json'}
)
@category = response.parsed_response # this will return the json.
Also you do not need to iterate on @category
in your case, the json object is singular and you can directly use it:
<%=@category["restaurant_name"]%>
<%=@category["country"]%>
<%=@category["currency"]%>
<%=@category["usd"]%>
<%=@category["client_key"]%>
<%=@category["client_name"]%>
<%=@category["client_email"]%>
<%=@category["client_phone"]%>
<%=@category["product_tier"]%>
<%=@category["brand_logo_large"]%>
Rails: Parsing HTTParty
This usually works for me
response = HTTParty.get(url, options)
puts response.body
HTTParty parsing sub-component of response
First of all, if possible, move the HTTParty stuff to a worker. This way, you will avoid your app crashing if the server you are hiting for the data is unavailable. Otherwise, make sure to wrap the HTTParty stuff in a begin - rescue - end
block and catch appropriate exceptions there.
Second of all, passing the entire JSON to a view and accessing it in the view is a bad practice because it drastically slows down template rendering. Instead, create a service object that would return the data model that's easy to access in a view. Give it a name that somehow describes what it relates to - MyJsonParser is probably not the best name, but you know what I mean. Implement a #call
method which returns the data for you in a format that's easy to access in the view.
my_json_parser.rb
class MyJsonPArser
def call
response = post_request
parse_structures(response)
end
private
def post_request
HTTParty.post(...)
end
def parse_structures(response)
structures = response["structures"]
# do more work with the big structures array ...
end
end
your_controller.rb
def your_method
@data = MyJsonParser.new.call
end
your_view.html.erb
<% @data.each do |item| %>
<div><%= item %></div>
...
<% end %>
Parsing XML HTTParty response
Assuming you have a response shown:
resp = ["Route", {
"RouteNo" => "016", "Name" => "29TH AVENUE STN/ARBUTUS ", "OperatingCompany" => "CMBC", "Patterns" => {
"Pattern" => [{
"PatternNo" => "E5TP", "Destination" => "29TH AVE STN", "RouteMap" => {
"Href" => "http://nb.translink.ca/geodata/trip/016-E5TP.kmz"
}, "Direction" => "EAST"
}, {
"PatternNo" => "EB1", "Destination" => "29TH AVE STN", "RouteMap" => {
"Href" => "http://nb.translink.ca/geodata/trip/016-EB1.kmz"
}, "Direction" => "EAST"
}, {
"PatternNo" => "EB5", "Destination" => "29TH AVE STN", "RouteMap" => {
"Href" => "http://nb.translink.ca/geodata/trip/016-EB5.kmz"
}, "Direction" => "EAST"
}]
}
}]
which is in turn an array of two elements, first being 'Route' and the last being a hash with all properties, to get the name use simply:
resp.last['Name']
Noob with HTTParty and parsing / accessing responses
Just put the json in a variable
example = {"success"=>true, "error"=>nil, "response"=>[{"loc"=>`{"lat"=>51.062831878662, "long" ... }`
then, for example you can do:
example['response'][0]['place']
If you need more help, please be more specific.
How get pdf file from httparty get response
You can do it like this:
filename = "tmp/test.pdf"
File.open(filename, "w") do |file|
file.binmode
HTTParty.get(URL_TO_PDF, stream_body: true) do |fragment|
file.write(fragment)
end
end
Replace URL_TO_PDF
with your pdf url
Related Topics
Carrierwave: Create 1 Uploader for Multiple Types of Files
Installing Pl/Ruby for Postgresql 8.3
Error Installing JSON 1.8.3 with Ruby 2.4
Terminal Not Executing Ruby Files
Sinatra - How to Get the Server's Domain Name
Ruby on Rails: Two References with Different Name to the Same Model
Gem Install Dm-Postgres-Adapter Build Error
Using Each_With_Index with Map
How to Programmatically Take Snapshot of Crawled Webpages (In Ruby)
Equivalent Ruby .Times in Coffeescript
How to Use Your Own MySQL Database Server with Heroku
Ruby/Rails Collection to Collection
How to Import Using Fastercsv a Row with a Name Like "CiaráN"
Naming Conventions for Boolean Attributes
Failing to Bundle Install Tiny_Tds on MAC Os X 10.8 with Homebrew Freetds
Ruby - Platform Independent Way to Determine Ips of All Network Interfaces
Retrieve All Posts Where the Given User Has Commented, Ruby on Rails