diff --git a/.gitignore b/.gitignore index 68d6299..fc19204 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ development.log *.sqlite3 *.swp +*.swo diff --git a/config/environment.rb b/config/environment.rb index 004537f..734ca46 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -21,6 +21,8 @@ # config.gem "aws-s3", :lib => "aws/s3" config.gem "haml" config.gem "authlogic", :version => "= 2.1.6" + config.gem "typhoeus" + config.gem "quimby" # Only load the plugins named here, in the order given (default is alphabetical). # :all can be used as a placeholder for all plugins not explicitly named diff --git a/lib/foursquare/base.rb b/lib/foursquare/base.rb new file mode 100644 index 0000000..a8cc52e --- /dev/null +++ b/lib/foursquare/base.rb @@ -0,0 +1,61 @@ +module EXFoursquare + class Base + # Constants + API = "https://api.foursquare.com/v2/" + + # Constructor + def initialize(*args) + case args.size + when 1 + @access_token = args.first + when 2 + @client_id, @client_secret = args + else + raise ArgumentError, "either access_token or (client_id + client_secret) should be passed" + end + end + + def venues + EXFoursquare::Venue.new(self) + end + + def get(path, query_params={}) + require 'uri' + require 'net/http' + require 'net/https' + + add_credential_params(query_params) + + # create a URI object + param_array = [] + query_params.each {|key, value| param_array << "#{key}=#{value}"} + api_url = API << path << '?' << param_array.join("&") + uri = URI.parse(api_url) + + # create an http object with ssl (for https) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + http.set_debug_output($stdout) + + # create the GET request + request = Net::HTTP::Get.new(uri.request_uri) + + # execute the request + response = http.request(request) + + # get and return the results + #result = ActiveSupport::JSON.decode(response.body) + end + + private + + def add_credential_params(params) + if @access_token + params.merge!(:oauth_token => @access_token) + else + params.merge!(:client_id => @client_id, :client_secret => @client_secret) + end + end + end +end diff --git a/lib/foursquare/tester.rb b/lib/foursquare/tester.rb new file mode 100644 index 0000000..2de8bd8 --- /dev/null +++ b/lib/foursquare/tester.rb @@ -0,0 +1,6 @@ +require 'base' +require 'venue' + +f = EXFoursquare::Base.new('HH0W0R34BDBXBU44OOXNIWT0VF4EBR0G4I0R1CDRDY5UNSPB', 'KSWT0P3UATRZUOFZO203YX5KR24U0I3XXXFB0QXXOA2TCNAD') +#@venues = f.venues.search({:ll => "48.857,2.349"}) +@venues = f.venues.search(:near => "Chicago") diff --git a/lib/foursquare/venue.rb b/lib/foursquare/venue.rb new file mode 100644 index 0000000..cb6d2c9 --- /dev/null +++ b/lib/foursquare/venue.rb @@ -0,0 +1,11 @@ +module EXFoursquare + class Venue + def initialize(foursquare) + @foursquare = foursquare + end + + def search(options={}) + puts @foursquare.get('venues/search', options) + end + end +end