Closed NathanielGiron closed 7 years ago
Hi Nathaniel, have you configured the API client as shown in the top of the Usage Examples section?
I see you've put your key and secret in a config file, but it's unclear from your code snippet if you've used those to set up the library. If you haven't, you could create an initializer (e.g. config/initializers/spark_api.rb
) that loads the settings from the yaml file and configures the client with your key and secret as shown in the readme.
Thanks for your reply @bhornseth ! It worked!
I have another question about the photos. So in the JSON, the photos attribute is a blank array but when I do:
SparkApi.client.get "/listings/#{listing_id}/photos/"
It returns an object that has photo URLs of that specific listing.
What I did to my listings_controller.rb(in show method) is:
@photos = SparkApi.client.get "/listings/#{listing_id}/photos"
then use that @photos object to my view but the problem is, I am unable to grab individual key/value of that object.
What I mean is, when I do:
<% @photos.each do |photo| %>
<ul>
<li><%= photo %></li>
</ul>
<% end %>
here's a sample of the displayed @photos object:
{"Uri2048"=>"https://cdn.resize.sparkplatform.com/gar/2048x1600/true/60227200316946095000000-o.jpg", "UriLarge"=>"https://cdn.photos.sparkplatform.com/gar/60227200316946095000000-o.jpg", "Primary"=>true}
My problem is when I try to access that UriLarge property using photo.UriLarge
, it gives me a method error.
Is this the right way to do this or Is there a better way?
You can tell the API to return the photos with the listing request using the Photos
expansion e.g.:
listings = Listing.find(:all, _expand: 'Photos')
The photos array in the each of the listing instances will then have values, and you can work with those as you're expecting (e.g. listings.first.photos.first.UriLarge
will return the UriLarge
value)
Quick note about that: when you use the classes in SparkApi::Models to retrieve data, you can use methods like photo.UriLarge
. When you retrieve data using the client request helper (e.g. SparkApi.client.get "/listings/#{listing_id}/photos"
, the return value is a plain ruby Hash, so you can would need to access data in those as you would any other hash in ruby (e.g. photo['UriLarge'] would work,
photo.UriLarge` will not)
@bhornseth Thanks again! I don't like to open another issue so hopefully, I can still ask on this thread. Please correct me if I'm wrong.
I would like to know why the API only displays 10 listing right now and how can I implement pagination on this and pagination links? Thanks again
10 is the default number of listings per page (_limit
parameter). You can find documentation on how to paginate over a set of API resources on our API documentation site
Okay, so pagination is working, however, I'm not sure how to deal with the views:
Controller:
page = params[:page]
@listings = Listing.find(:all, _expand: 'Photos', _page: page)
Views:
<ul class="pagination">
<li><a href="?page=1">1</a></li>
<li><a href="?page=2">2</a></li>
<li><a href="?page=3">3</a></li>
</ul>
I can easily display all the page numbers by dividing total count & 10 and looping through each number. But I don't wanna do that because that isn't ideal. The result I want is something like:
"< 1 2 3 4 5 ... >"
Also, can I use gems like will_paginate or kamari? or is there a better way to do this?
How you handle the views is going to be specific to your application. Note that if you include the _pagination
param with your listing request, the API will return a hash telling you the total number of listings, current page, total pages, etc:
>> Listing.find(:all, _pagination: 1).pagination
{"TotalRows"=>19078, "PageSize"=>10, "CurrentPage"=>1, "TotalPages"=>1908}
thanks @bhornseth! I made a custom pagination and the code you sent me helped!
I have a question regarding search:
I am trying to filter listings by Agent name or contact or their license but how come It's returning all of the listings?
I am not sure if there is something wrong with my query syntax but I'm pretty sure this is correct:
:_filter => "ListAgentStateLicense Eq 'AB-123'"
*AB-123 is just a sample
@NathanielGiron I'm going have you direct that question to our support personnel since that's most likely specific to the MLS you're accessing. They can be reached via email: api-support@flexmls.com
thanks @bhornseth
Hi,
I am new to spark api and I am just trying to generate all listings in a new rails app.
Here are my steps:
Add to gemfile
gem 'spark_api'
bundle install
Create a listings controller
class ListingsController < ApplicationController
def index
@listings = Listing.find(:all)
end
end
Create a view
<h2>Listings</h2>
<ul>
<% @listings.each do |listing| %>
<li><%= listing.ListingKey %></li>
<% end %>
</ul>
Create settings.yml in config/spark_api/settings.yml
development:
api_key: 'my_api_key'
api_secret: 'my_secret_key'
So what am I missing here? Thank you for your response!