/* * Sample code to demonstrate the BlipADeal API * * Written By By: Sido.B JULY 14th 2012 * * For further information please visit : www.blipadeal.com/dev-api * For Online working demo please visit : webapi.blipadeal.com/webapi * For support or help please email : support@blipadeal.com * * PLEASE NOTE: ALL THE HTML AND OTHER CODE HAS BEEN TAKE OUT OF THIS * EXAMPLE TO GET THE * BEST VIEW OF THE ACTUAL API CORE CODE. IF YOU WOULD LIKE TO SEE THE FULL * RUNNING DEMO PLEASE GO TO * http://www.blipadeal.com/api-dev/blipadeal_api_javascript_sample_program.html * * YOU CAN VIEW SOURCE AND CLICK ON THE blipadeal_api_sample.js script * TO SEE ALL THE ADDITIONAL CODE * * */ /* * "Generic function to send the request to the API" * * */ function _call_function(remote_func_name, local_callback_func ,json_object) { json_object[ 'message_status' ] = 'SUCCESS'; json_object[ 'api_key_token' ] = ''; json_object[ 'api_secret_token' ] = '' ; var url_uri = 'https://webapi.blipadeal.com/' + remote_func_name + '?request=' + JSON.stringify( json_object ); // // Send the request , doing jsonpCallback is the same as doing // &callback=local_callback_func // // SO if you are not using jquery and the jsonpCallback option didnt // exist you would do something like // http://webapi.blipadeal.com/func_name?reqest=JSON_OBJ&callback=callback_func // $.ajax( { type : 'GET', url : url_uri, dataType : 'jsonp', jsonpCallback : local_callback_func } ); } /* * This function will get all the deals based on a phrase in the city of * sydney. Then it will look for deals near by sydney given the same phrase * * */ function _get_deals_of_category_type_in_given_location(free_text) { var json_object = { 'free_text': encodeURIComponent( free_text ) }; _call_function( 'api_get_category_type', '_get_deals_of_category_type_in_given_location_callback', json_object ); } /* * Callback for the above function * * */ function _get_deals_of_category_type_in_given_location_callback(response) { var message_status = response.message_action; if ( message_status == 'GET_CATEGORY_TYPE_SUCCESS' ) { var category_array = response.api_category_type for (var idx = 0 ; idx < category_array.length ; idx++) { // DO SOME WORK HERE } } } /* * This function will retrieve all of the countries currently active within * the blipADeal * * */ function _get_all_countries() { var json_object = {}; _call_function( 'api_get_country', '_get_all_countries_callback', json_object ); } function _get_all_countries_callback(response) { var message_status = response.message_action; if ( message_status == 'COUNTRY_RETRIEVE_SUCCESS' ) { var country_list = response.country_list; for (var idx=0; idx < country_list.length; idx++) { // DO SOME WORK HERE } // GET THE CITIES BASED ON THE FIRST COUNTRY IN THE LIST // AS DEFAULT _get_all_cities_from_a_country( country_list[0].blp_main_deal_detail_country_location ) } } /* * This function will retrieve all the cities in a given country. You may want to use this * to get deals from multiple cities * * */ function _get_all_cities_from_a_country(country) { var json_object = { "param_country" : country }; _call_function( 'api_get_location', '_get_all_cities_from_a_country_callback', json_object ); } function _get_all_cities_from_a_country_callback(response) { var message_status = response.message_action; $('#blp_get_city_result_id').html(''); if (message_status == 'CITY_RETRIEVE_SUCCESS') { var city_list = response.location_list; for (var idx=0; idx < city_list.length; idx++) { // DO SOME WORK HERE } } } /* * This function will get the location of all the places where there are deals within * a 200km region * * */ function _get_deals_near_by_text(country, city, distance) { json_request = { "param_city" : city, "param_country" : country, "param_distance": distance } _call_function( 'api_get_deal_location_near_by_text', '_get_deals_near_by_text_callback', json_request ); } function _get_deals_near_by_text_callback(response) { var message_status = response.message_action; if ( message_status == 'GET_DEAL_LOCATION_NEAR_BY_SUCCESSFUL' ) { for (var idx=0; idx < poi_list.length; idx++) { // DO SOME WORK HERE } } } /* * * This will get deals based on a given category * * */ function _get_deals_based_on_category(country, city) { var category_selected = $('#blp_get_free_text_result_id').val(); json_request = { "city_select" : city, "country_select" : country, "category_list" : [category_selected], "sort_method" : "bought" } _call_function( 'api_get_category_deals', '_get_deals_based_on_category_callback', json_request ); } function _get_deals_based_on_category_callback(response) { var message_status = response.message_action; $('#blp_main_deals_list_id').html(''); if (message_status == 'DEAL_RETRIEVE_SUCCESS') { var country_select = response.country_select; var city_select = response.deal_location; for (var idx=0;idx < deal_list.length; idx++) { // DO SOME WORK HERE } } }