Rails 4.2 dev environment
I have a controller "Searches" with:
def index
@search_term = params[:s] || 'shoes'
@listings = Class_name.for(@search_term)
end
a model that the controller uses for the "Class_name" class object & the "for" method used upon the object:
class Class_name
include HTTParty
base_uri 'http://svcs.ebay.com/services/search/FindingService/v1?SERVICE-NAME=FindingService&OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.13.0&SECURITY-APPNAME=xxxx&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD&GLOBAL-ID=EBAY-US&paginationInput.entriesPerPage=25&paginationInput.pageNumber=1'
format :json
def self.for term
get("", query: { keywords: term})["findItemsByKeywordsResponse"]
end
end
and lastly, a view - where I create a table to show the parsed response from eBay's API.
<table border="1">
<tr>
<th>Image</th>
<th>Name</th>
</tr>
<% @listings.each do |product| %>
<tr>
<td><%= image_tag(product["searchResult"][0]["item"][0]["galleryURL"])%></td>
<td><%= product["searchResult"][0]["item"].sample(25) %></td>
</tr>
<% end %>
</table>
My questions are for the model and for the view.
For the model, I tried to add a "default_params" HTTParty method - to not have my base_uri so long and easier to manage - but when i would write
default_params SERVICE-NAME: "FindingService", OPERATION-NAME: "#{operation}" ,etc ,etc
it would give me errors basically saying symbols can't be constants "SERVICE-NAME" but the params for the api must be/should be in caps.
Why can't constants be considered a hash/symbol when they have an assigned value? I guess because they're expected to already have a pre-assigned value by convention?
How would I then assign these values in a safer way than I'm doing now - not being able to use them as symbols
Also, is there any merit in hiding my App ID considering if people followed a redirect - wouldn't it display the App ID within the URL anyhow? If not, wouldn't they need it to follow a redirect?
What about an affiliate Id instead? I'd surely need to have the affiliate Id stay within the URL of redirecting someone to the traffic'd link to get credit.
Onto the view questions.. This is the part that has been really giving me trouble..
So in the model code, you see the "self.for(term)" method, it takes in the param you give it to query for the keywords param required for a search & it results in a JSON formatted table of values named "findItemsByKeywordsResponse" that I want to access.
Searching for a chair, the begiing code looks like this:
{
findItemsByKeywordsResponse: [
{
ack: [
"Success"
],
version: [
"1.13.0"
],
timestamp: [
"2016-07-11T01:33:40.002Z"
],
searchResult: [
{
@count: "25",
item: [
{
itemId: [
"351198621254"
],
title: [
"Black PU Leather High Back Office Chair Executive Task Ergonomic Computer Desk"
],
globalId: [
"EBAY-US"
],
subtitle: [
"Ship from IN & CA ! Delivery in 1-3 Days US 48 States !"
],
primaryCategory: [
{
categoryId: [
"61677"
],
categoryName: [
"Chairs"
]
}
],
secondaryCategory: [
{
categoryId: [
"54235"
],
categoryName: [
"Chairs"
]
}
],
galleryURL: [
"http://thumbs3.ebaystatic.com/m/mBdjVHSC7hWaSBl-4Ku69aA/140.jpg"
],
viewItemURL: [
"http://www.ebay.com/itm/Black-PU-Leather-High-Back-Office-Chair-Executive- Task-Ergonomic-Computer-Desk-/351198621254"
],
etc, etc
There is some more code, but all the nesting is similar to this portion. This code so far is for only one individual item & as seen in the model's base_uri params & view, each page is being paginated with 25 items & 25 items are being sampled to display.
I realize to access this information individually, you must access the item array ["item"][0], ["item"][1],etc to get to information such as ["title"] for each product.
& I have even tested my code using the above way and upon doing so ( product["searchResult"][0]["item"][0]["title"] ) I can put the title value in my table on a per product basis - but it doesn't format and show correctly. the actual value shows up from the array as this:
["Black PU Leather High Back Office Chair Executive Task Ergonomic Computer Desk"]
with the array blocks and quotes around the value and everything - rendering inside the table coded in my view file.
How do I extract the "true" value out of the array/hashes? - getting rid of the array block code and quotes
Also - again - as seen in the model file's base_uri code, the listings are automatically getting paginated at a result of 25 products per page upon a get request.
Knowing this & pre-defining this, how do I iterate over and access the value of all 25 of the ["searchResult"][0]["item"][1..25] "title" values & "galleryURL" values? & Once again.. while getting rid of the
[ ] and ""
around each value
The code I am using now, sampling the 25 ["item"] array values, is basically just rendering the exact code I posted earlier from the findItemsKeywordsResponse JSON table while I've been trying to figure all of this out.
Help on this will be very very greatly appreciated..
