Skip to content
Browse files

Added separate services for User, checkin and venue

  • Loading branch information...
1 parent c8a8910 commit e2cd6dc0bc0dfbab030ffa408a5b64c939fbae22 Vishal-Sahu committed Oct 5, 2012
View
27 grails-app/services/org/grails/plugin/foursquare/CheckinsService.groovy
@@ -0,0 +1,27 @@
+package org.grails.plugin.foursquare
+
+import grails.converters.JSON
+
+class CheckinsService extends FoursquareService {
+
+
+ Checkin getCheckinDetails(String oauthToken, String checkinId) {
+ Checkin checkin
+ String urlString = getCheckinDataUrl(oauthToken, checkinId)
+ try {
+ URL url = new URL(urlString)
+ def jsonString = JSON.parse(url.text)
+ checkin = new Checkin(jsonString?.response?.checkin, true)
+ } catch (Exception e) {
+ e.printStackTrace()
+ }
+ return checkin
+ }
+
+ private getCheckinDataUrl(String oauthToken, String checkinId) {
+ String date = (new Date()).format('yyyyMMdd')
+ String urlString = "https://api.foursquare.com/v2/checkins/${checkinId}?oauth_token=${oauthToken}&v=${date}&limit=100"
+ urlString
+ }
+
+}
View
184 grails-app/services/org/grails/plugin/foursquare/FoursquareService.groovy
@@ -1,12 +1,15 @@
package org.grails.plugin.foursquare
-import grails.converters.JSON
import org.codehaus.groovy.grails.commons.ConfigurationHolder
class FoursquareService {
static transactional = false
+ @Delegate UserService userService
+ @Delegate VenueService venueService
+ @Delegate CheckinsService checkinsService
+
String getResponseFromFourSquare(String urlPath) {
String responseData
try {
@@ -30,187 +33,12 @@ class FoursquareService {
return responseString
}
- public List<Venue> searchLocations(String latitude, String longitude) {
- List<Venue> venueList = []
- String urlString = getSearchFoursquareLocationUrl(latitude, longitude)
- try {
- String responseStr = getResponseOfFoursquareQuery(urlString)
- def jsonString = JSON.parse(responseStr)
- jsonString.response.venues.each {
- Venue venue = new Venue(it)
- venueList << venue
- }
- } catch (Exception e) {
- log.error(e.message)
- }
- return venueList
- }
-
- public List<User> searchUsersByPhone(String oauthToken, String phone) {
- List<User> userList = []
- String urlString = getSearchFoursquareUserUrl(oauthToken, 'phone', phone)
- try {
- String responseStr = getResponseOfFoursquareQuery(urlString)
- def jsonString = JSON.parse(responseStr)
- jsonString.response.results.each {
- User user = new User(it)
- userList << user
- }
- } catch (Exception e) {
- log.error(e.message)
- }
- return userList
- }
-
- public List<User> searchUsersByEmail(String oauthToken, String email) {
- List<User> userList = []
- String urlString = getSearchFoursquareUserUrl(oauthToken, 'email', email)
- try {
- String responseStr = getResponseOfFoursquareQuery(urlString)
- def jsonString = JSON.parse(responseStr)
- jsonString.response.results.each {
- User user = new User(it)
- userList << user
- }
- } catch (Exception e) {
- log.error(e.message)
- }
- return userList
- }
-
- public List<User> searchUsersByTwitter(String oauthToken, String twitter) {
- List<User> userList = []
- String urlString = getSearchFoursquareUserUrl(oauthToken, 'twitter', twitter)
- try {
- String responseStr = getResponseOfFoursquareQuery(urlString)
- def jsonString = JSON.parse(responseStr)
- jsonString.response.results.each {
- println '-----------it--------' + it
- User user = new User(it)
- userList << user
- }
- } catch (Exception e) {
- log.error(e.message)
- }
- return userList
- }
-
- public List<User> searchUsersByName(String oauthToken, String name) {
- List<User> userList = []
- String urlString = getSearchFoursquareUserUrl(oauthToken, 'name', name)
- try {
- String responseStr = getResponseOfFoursquareQuery(urlString)
- def jsonString = JSON.parse(responseStr)
- jsonString.response.results.each {
- User user = new User(it)
- userList << user
- }
- } catch (Exception e) {
- log.error(e.message)
- }
- return userList
- }
-
- public List<Venue> searchLocations(String address) {
- List<Venue> venueList = []
- String urlString = getSearchFoursquareLocationUrl(address)
- try {
- String responseStr = getResponseOfFoursquareQuery(urlString)
- def jsonString = JSON.parse(responseStr)
- jsonString.response.venues.each {
- Venue venue = new Venue(it)
- venueList << venue
- }
- } catch (Exception e) {
- log.error(e.message)
- }
- return venueList
- }
- public Long getFoursquareLocationCheckinsCount(String foursquareLocationId) {
- Long foursquareLocationCheckinCount = 0
- String urlString = getFoursquareLocationDataUrl(foursquareLocationId)
- try {
- URL url = new URL(urlString)
- def jsonString = JSON.parse(url.text)
- foursquareLocationCheckinCount = jsonString.response.venue.stats.checkinsCount
-
- } catch (Exception e) {
- e.printStackTrace()
- }
- return foursquareLocationCheckinCount
- }
-
- public Venue getFoursquareLocationData(String foursquareLocationId) {
- Venue venue
- String urlString = getFoursquareLocationDataUrl(foursquareLocationId)
- try {
- URL url = new URL(urlString)
- def jsonString = JSON.parse(url.text)
- venue = new Venue(jsonString.response.venue)
- jsonString.response.venue.tips.groups.each {def group ->
- group.items.each { def tipData ->
- Tip tip = new Tip(tipData)
- venue.tips << tip
- }
- }
- } catch (Exception e) {
- e.printStackTrace()
- }
- return venue
- }
-
- public List<Tip> getRecentFoursquareCheckins(String foursquareLocationId, Integer count = 1) {
- List<Tip> tipList = []
- String urlString = getFoursquareLocationDataUrl(foursquareLocationId)
- try {
- URL url = new URL(urlString)
- def jsonString = JSON.parse(url.text)
- jsonString.response.venue.tips.groups.each {def group ->
- group.items.eachWithIndex { def tipData, Integer index ->
- if (index < count) {
- Tip tip = new Tip(tipData)
- tipList << tip
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace()
- }
- return tipList
- }
-
- private getSearchFoursquareLocationUrl(String latitude, String longitude) {
- String date = (new Date()).format('yyyyMMdd')
- String urlString = "https://api.foursquare.com/v2/venues/search?ll=${latitude},${longitude}&client_id=${clientId}&client_secret=${clientSecret}&v=${date}"
- urlString
- }
-
- private getSearchFoursquareUserUrl(String oauthToken, String selector, String value) {
- String date = (new Date()).format('yyyyMMdd')
- String urlString = "https://api.foursquare.com/v2/users/search?${selector.encodeAsURL()}=${value}&oauth_token=${oauthToken}&v=${date}"
- urlString
- }
-
- private getSearchFoursquareLocationUrl(String address) {
- String date = (new Date()).format('yyyyMMdd')
- String urlString = "https://api.foursquare.com/v2/venues/search?near=${address.encodeAsURL()}&client_id=${clientId}&client_secret=${clientSecret}&v=${date}"
- urlString
- }
-
-
- private getFoursquareLocationDataUrl(String foursquareLocationId) {
- String date = (new Date()).format('yyyyMMdd')
- String urlString = "https://api.foursquare.com/v2/venues/${foursquareLocationId}?&client_id=${clientId}&client_secret=${clientSecret}&v=${date}"
- return urlString
- }
-
- private getClientSecret() {
+ public getClientSecret() {
return ConfigurationHolder.config.fourSquare.secret
}
- private getClientId() {
+ public getClientId() {
return ConfigurationHolder.config.fourSquare.clientId
}
-
}
View
140 grails-app/services/org/grails/plugin/foursquare/UserService.groovy
@@ -0,0 +1,140 @@
+package org.grails.plugin.foursquare
+
+import grails.converters.JSON
+
+class UserService extends FoursquareService {
+
+ public List<User> searchUsersByPhone(String oauthToken, String phone) {
+ List<User> userList = []
+ String urlString = getSearchUserUrl(oauthToken, 'phone', phone)
+ try {
+ String responseStr = getResponseOfFoursquareQuery(urlString)
+ def jsonString = JSON.parse(responseStr)
+ jsonString.response.results.each {
+ User user = new User(it)
+ userList << user
+ }
+ } catch (Exception e) {
+ log.error(e.message)
+ }
+ return userList
+ }
+
+ public List<User> searchUsersByEmail(String oauthToken, String email) {
+ List<User> userList = []
+ String urlString = getSearchUserUrl(oauthToken, 'email', email)
+ try {
+ String responseStr = getResponseOfFoursquareQuery(urlString)
+ def jsonString = JSON.parse(responseStr)
+ jsonString.response.results.each {
+ User user = new User(it)
+ userList << user
+ }
+ } catch (Exception e) {
+ log.error(e.message)
+ }
+ return userList
+ }
+
+ public List<User> searchUsersByTwitter(String oauthToken, String twitter) {
+ List<User> userList = []
+ String urlString = getSearchUserUrl(oauthToken, 'twitter', twitter)
+ try {
+ String responseStr = getResponseOfFoursquareQuery(urlString)
+ def jsonString = JSON.parse(responseStr)
+ jsonString.response.results.each {
+ User user = new User(it)
+ userList << user
+ }
+ } catch (Exception e) {
+ log.error(e.message)
+ }
+ return userList
+ }
+
+ public List<User> searchUsersByName(String oauthToken, String name) {
+ List<User> userList = []
+ String urlString = getSearchUserUrl(oauthToken, 'name', name)
+ try {
+ String responseStr = getResponseOfFoursquareQuery(urlString)
+ def jsonString = JSON.parse(responseStr)
+ jsonString.response.results.each {
+ User user = new User(it)
+ userList << user
+ }
+ } catch (Exception e) {
+ log.error(e.message)
+ }
+ return userList
+ }
+
+ List<Checkin> getUserCheckins(String oauthToken) {
+ List<Checkin> checkinList = []
+ String urlString = getUserCheckinsUrl(oauthToken)
+ try {
+ String responseStr = getResponseOfFoursquareQuery(urlString)
+ def jsonString = JSON.parse(responseStr)
+ jsonString.response.checkins.items.each {def checkinData ->
+ Checkin checkin = new Checkin(checkinData)
+ checkinList.add(checkin)
+ }
+ } catch (Exception e) {
+ log.error(e.message)
+ }
+ return checkinList
+ }
+
+ List<User> getOwnFriendsList(String oauthToken) {
+ List<User> userList = []
+ String urlString = getFriendsListUrl(oauthToken)
+ try {
+ String responseStr = getResponseOfFoursquareQuery(urlString)
+ def jsonString = JSON.parse(responseStr)
+ jsonString.response.friends.items.each {def userData ->
+ User user = new User(userData)
+ userList.add(user)
+ }
+ } catch (Exception e) {
+ log.error(e.message)
+ }
+ return userList
+ }
+
+ List<User> getUserFriendsList(String oauthToken, String userId) {
+ List<User> userList = []
+ String urlString = getFriendsListUrl(oauthToken)
+ try {
+ String responseStr = getResponseOfFoursquareQuery(urlString)
+ def jsonString = JSON.parse(responseStr)
+ jsonString.response.friends.items.each {def userData ->
+ User user = new User(userData)
+ userList.add(user)
+ }
+ } catch (Exception e) {
+ log.error(e.message)
+ }
+ return userList
+ }
+
+ User unFriendUser() {
+// https://api.foursquare.com/v2/users/USER_ID/unfriend
+ }
+
+ private getFriendsListUrl(String oauthToken, String userId = 'self') {
+ String date = (new Date()).format('yyyyMMdd')
+ String urlString = "https://api.foursquare.com/v2/users/${userId}/friends?oauth_token=${oauthToken}&v=${date}"
+ urlString
+ }
+
+ private getSearchUserUrl(String oauthToken, String selector, String value) {
+ String date = (new Date()).format('yyyyMMdd')
+ String urlString = "https://api.foursquare.com/v2/users/search?${selector.encodeAsURL()}=${value}&oauth_token=${oauthToken}&v=${date}"
+ urlString
+ }
+
+ private getUserCheckinsUrl(String oauthToken) {
+ String date = (new Date()).format('yyyyMMdd')
+ String urlString = "https://api.foursquare.com/v2/users/self/checkins?oauth_token=${oauthToken}&v=${date}&limit=100"
+ urlString
+ }
+}
View
91 grails-app/services/org/grails/plugin/foursquare/VenueService.groovy
@@ -0,0 +1,91 @@
+package org.grails.plugin.foursquare
+
+import grails.converters.JSON
+
+class VenueService extends FoursquareService {
+
+ public List<Venue> searchVenues(String latitude, String longitude) {
+ List<Venue> venueList = []
+ String urlString = getSearchVenueUrl(latitude, longitude)
+ try {
+ String responseStr = getResponseOfFoursquareQuery(urlString)
+ def jsonString = JSON.parse(responseStr)
+ jsonString.response.venues.each {
+ Venue venue = new Venue(it)
+ venueList << venue
+ }
+ } catch (Exception e) {
+ log.error(e.message)
+ }
+ return venueList
+ }
+
+ public List<Venue> searchVenues(String address) {
+ List<Venue> venueList = []
+ String urlString = getSearchVenueUrl(address)
+ try {
+ String responseStr = getResponseOfFoursquareQuery(urlString)
+ def jsonString = JSON.parse(responseStr)
+ jsonString.response.venues.each {
+ Venue venue = new Venue(it)
+ venueList << venue
+ }
+ } catch (Exception e) {
+ log.error(e.message)
+ }
+ return venueList
+ }
+
+
+ public Venue getVenueData(String foursquareLocationId) {
+ Venue venue
+ String urlString = getVenueDataUrl(foursquareLocationId)
+ try {
+ URL url = new URL(urlString)
+ def jsonString = JSON.parse(url.text)
+ venue = new Venue(jsonString.response.venue)
+ jsonString.response.venue.tips.groups.each {def group ->
+ group.items.each { def tipData ->
+ Tip tip = new Tip(tipData)
+ venue.tips << tip
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace()
+ }
+ return venue
+ }
+
+ public Long getFoursquareVenueCheckinsCount(String foursquareLocationId) {
+ Long foursquareLocationCheckinCount = 0
+ String urlString = getVenueDataUrl(foursquareLocationId)
+ try {
+ URL url = new URL(urlString)
+ def jsonString = JSON.parse(url.text)
+ foursquareLocationCheckinCount = jsonString.response.venue.stats.checkinsCount
+
+ } catch (Exception e) {
+ e.printStackTrace()
+ }
+ return foursquareLocationCheckinCount
+ }
+
+
+ public getSearchVenueUrl(String latitude, String longitude) {
+ String date = (new Date()).format('yyyyMMdd')
+ String urlString = "https://api.foursquare.com/v2/venues/search?ll=${latitude},${longitude}&client_id=${clientId}&client_secret=${clientSecret}&v=${date}"
+ urlString
+ }
+
+ public getSearchVenueUrl(String address) {
+ String date = (new Date()).format('yyyyMMdd')
+ String urlString = "https://api.foursquare.com/v2/venues/search?near=${address.encodeAsURL()}&client_id=${clientId}&client_secret=${clientSecret}&v=${date}"
+ urlString
+ }
+
+ public getVenueDataUrl(String foursquareLocationId) {
+ String date = (new Date()).format('yyyyMMdd')
+ String urlString = "https://api.foursquare.com/v2/venues/${foursquareLocationId}?&client_id=${clientId}&client_secret=${clientSecret}&v=${date}"
+ return urlString
+ }
+}
View
99 src/groovy/org/grails/plugin/foursquare/Checkin.groovy
@@ -0,0 +1,99 @@
+package org.grails.plugin.foursquare
+
+import org.codehaus.groovy.grails.web.json.JSONArray
+
+class Checkin {
+
+ String id
+ String type
+ Boolean isPrivate = false
+ User user
+ Long timeZoneOffset
+ Venue venue
+ Location location
+ String shout
+ Date createdAt
+ String source
+ String event
+ List<VenuePhoto> photos = []
+ Long photosCount = 0
+ List<Tip> comments = []
+ Long likesCount = 0
+ Long commentsCount
+ Long score
+ Long overlapCount
+ List<Category> categories = []
+
+ Checkin() {
+
+ }
+
+ Checkin(def checkinData) {
+ this.id = checkinData.id
+ this.source = checkinData.source?.name
+ this.shout = checkinData.shout
+ this.score = checkinData.score?.total
+ this.photosCount = checkinData?.photos?.count
+ this.timeZoneOffset = checkinData?.timeZoneOffset
+ this.createdAt = new Date(checkinData.createdAt?.toLong() * 1000)
+ this.likesCount = checkinData.linkes?.count
+ this.commentsCount = checkinData.comments?.count
+ this.type = checkinData.type
+ populateVenue(checkinData)
+ populateLocation(checkinData)
+ if (checkinData?.venue?.categories) {
+ JSONArray jsonArray = new JSONArray(checkinData?.venue?.categories)
+ jsonArray.each {def categoryData ->
+ Category category = new Category(categoryData)
+ this.categories.add(category)
+ }
+ }
+ }
+
+ Checkin(def checkinData, Boolean fromDetailsPage) {
+ this.id = checkinData.id
+ this.type = checkinData.type
+ this.createdAt = new Date(checkinData.createdAt?.toLong() * 1000)
+ this.score = checkinData.score?.total
+ this.shout = checkinData.shout
+ this.timeZoneOffset = checkinData?.timeZoneOffset
+ this.source = checkinData.source?.name
+ this.photosCount = checkinData?.photos?.count
+ this.likesCount = checkinData.linkes?.count
+ this.commentsCount = checkinData.comments?.count
+ populateVenue(checkinData)
+ populateLocation(checkinData)
+ User user = new User(checkinData?.user)
+ this.user = user
+ if (checkinData?.venue?.categories) {
+ JSONArray jsonArray = new JSONArray(checkinData?.venue?.categories)
+ jsonArray.each {def categoryData ->
+ Category category = new Category(categoryData)
+ this.categories.add(category)
+ }
+ }
+ }
+
+ private populateVenue(checkinData) {
+ Venue venue = new Venue()
+ venue.id = checkinData?.venue?.id
+ venue.name = checkinData?.venue?.name
+ venue.checkinsCount = checkinData?.venue?.checkinsCount
+ venue.userCount = checkinData?.venue?.usersCount
+ venue.tipsCount = checkinData?.venue?.tipCount
+ this.venue = venue
+ }
+
+ private populateLocation(checkinData) {
+ Location location = new Location()
+ location.address = checkinData?.venue?.location?.address
+ location.crossStreet = checkinData?.venue?.location?.crossStreet
+ location.latitude = checkinData?.venue?.location?.lat
+ location.longitude = checkinData?.venue?.location?.lng
+ location.postalCode = checkinData?.venue?.location?.postalCode
+ location.city = checkinData?.venue?.location?.city
+ location.state = checkinData?.venue?.location?.state
+ location.country = checkinData?.venue?.location?.country
+ this.location = location
+ }
+}
View
3 src/groovy/org/grails/plugin/foursquare/Location.groovy
@@ -2,10 +2,11 @@ package org.grails.plugin.foursquare
class Location {
String address
+ String crossStreet
Long latitude
Long longitude
Long distance
- Long postalCode
+ String postalCode
String city
String state
String country
View
2 src/groovy/org/grails/plugin/foursquare/Venue.groovy
@@ -75,7 +75,7 @@ class Venue {
JSONArray jsonArray = new JSONArray(venueData.photos)
jsonArray.each {def photoData ->
VenuePhoto venuePhoto = new VenuePhoto(photoData)
- this.categories.add(category)
+ this.photos.add(venuePhoto)
}
}
}

0 comments on commit e2cd6dc

Please sign in to comment.
Something went wrong with that request. Please try again.