Skip to content

Commit

Permalink
extending API with first revision of Smashie
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardSchneeman committed Jul 7, 2010
1 parent d5568f5 commit fdce43e
Show file tree
Hide file tree
Showing 19 changed files with 4,153 additions and 4,499 deletions.
11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source :rubygems

gem 'oauth2'
gem 'faraday', '~> 0.4.5'
gem 'hashie', '~> 0.2.0'
gem 'multi_json', '~> 0.0.4'
gem 'faraday-middleware'

group :development do
gem 'shoulda', '~> 2.10.0'
end
103 changes: 88 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,108 @@ Be sure and get your API key: [http://gowalla.com/api/keys](http://gowalla.com/a

## Usage

### Instantiate a client
### Instantiate a client (Basic Auth)

>> gowalla = Gowalla::Client.new(:username => 'pengwynn', :password => 'somepassword', :api_key => 'your_api_key')
gowalla = Gowalla::Client.new(:username => 'pengwynn', :password => 'somepassword', :api_key => 'your_api_key')

### or configure once

>> Gowalla.configure do |config|
>> config.api_key = 'your_api_key'
>> config.username = 'pengwynn'
>> config.password = 'somepassword'
>> end
>> gowalla = Gowalla::Client.new
Gowalla.configure do |config|
config.api_key = 'your_api_key'
config.username = 'pengwynn'
config.password = 'somepassword'
end
gowalla = Gowalla::Client.new

### Instantiate a client (OAuth2)

For OAuth2, you'll need to specify a callback URL when you [set up your app and get your API keys](http://gowalla.com/api/keys).

Let's create the client in a protected method in our controller:

protected
def client
Gowalla::Client.new (
:api_key => "your_api_key",
:api_secret => "your_api_secret",
:access_token => session[:access_token]
)
end

We need to get an access token. Perhaps in an a before filter where you need to access Gowalla:

redirect(@client.web_server.authorize_url(:redirect_uri => redirect_uri, :state => 1))

You'll need a callback route to catch the code coming back from Gowalla after a user grants you access and this must match what you specified when you created your app on Gowalla:

get '/auth/gowalla/callback' do
session[:access_token] = client.web_server.get_access_token(params[:code], :redirect_uri => redirect_uri).token

if session[:access_token]
redirect '/auth/gowalla/test'
else
"Error retrieving access token."
end
end

Now that we have an access token we can use the client to make calls, just like we would with Basic Auth. You can checkout a basic [Sinatra example](http://gist.github.com/454283).

#### Examples

>> gowalla.user('pengwynn')
=> <#Hashie::Mash accept_url="/friendships/accept?user_id=1707" activity_url="/users/1707/events" bio="Web designer and Ruby developer." events_url="/users/1707/events" fb_id=605681706 first_name="Wynn" friends_count=27 friends_only=false friends_url="/users/1707/friends" hometown="Aubrey, TX" image_url="http://s3.amazonaws.com/static.gowalla.com/users/1707-standard.jpg?1262011383" is_friend=false items_count=5 items_url="/users/1707/items" last_name="Netherland" last_visit=<#Hashie::Mash comment="Closing every account I have " created_at="2010/01/26 15:31:46 +0000" spot=<#Hashie::Mash image_url="http://static.gowalla.com/categories/186-standard.png" name="Bank Of America" small_image_url="http://static.gowalla.com/categories/186-small-standard.png" url="/spots/164052">> name="Wynn Netherland" pins_count=3 pins_url="/users/1707/pins" reject_url="/friendships/reject?user_id=1707" request_url="/friendships/request?user_id=1707" stamps_count=15 stamps_url="/users/1707/stamps" top_spots_url="/users/1707/top_spots" twitter_username="pengwynn" url="/users/1707" username="pengwynn" vaulted_kinds_count=0 visited_spots_count=15 website="http://wynnnetherland.com">
gowalla.user('pengwynn')
=> <#Hashie::Mash accept_url="/friendships/accept?user_id=1707" activity_url="/users/1707/events" bio="Web designer and Ruby developer." events_url="/users/1707/events" fb_id=605681706 first_name="Wynn" friends_count=27 friends_only=false friends_url="/users/1707/friends" hometown="Aubrey, TX" image_url="http://s3.amazonaws.com/static.gowalla.com/users/1707-standard.jpg?1262011383" is_friend=false items_count=5 items_url="/users/1707/items" last_name="Netherland" last_visit=<#Hashie::Mash comment="Closing every account I have " created_at="2010/01/26 15:31:46 +0000" spot=<#Hashie::Mash image_url="http://static.gowalla.com/categories/186-standard.png" name="Bank Of America" small_image_url="http://static.gowalla.com/categories/186-small-standard.png" url="/spots/164052"name="Wynn Netherland" pins_count=3 pins_url="/users/1707/pins" reject_url="/friendships/reject?user_id=1707" request_url="/friendships/request?user_id=1707" stamps_count=15 stamps_url="/users/1707/stamps" top_spots_url="/users/1707/top_spots" twitter_username="pengwynn" url="/users/1707" username="pengwynn" vaulted_kinds_count=0 visited_spots_count=15 website="http://wynnnetherland.com">


#### Details for the current user
Details for the current user

>> gowalla.user
gowalla.user

#### Details for another user
Details for another user

>> gowalla.user('bradleyjoyce')
>> gowalla.user(1707)
gowalla.user('bradleyjoyce')
gowalla.user(1707)


#### This Branch Specific Info - Smashie

I'm working on a new library i'm calling Smashie. Use it to extend the API by easily extending your requests.

Smashie extends any property ending in "_url" or "url". For example we can call.

user.get_pins

to get a users pins

user.get_stamps

to a get a users stamps or if you wish to get the data from a user's url you could issue.

user.get

Smashie simplifies navigating data structure, but excels at stringing together methods. For example if you wanted to get all of the last_checkins at the most recent checkin of a specific user you could call:

gowalla.user(username).last_checkins.first.spot.get.last_checkins

Smashie will automatically propagate the connection object through any return values. So as long as you have an object that was returned from a gowalla connection you can make an API call off of it.

So instead of:
user1 = gowalla.user(username)
user2 = gowalla.user(some_other_username)


You can use:
user1 = gowalla.user(username)
user2 = user1.connection.user(some_other_username)




Let me know if you have any feedback on Smashie, it is still very much a work in progress.


There are some sanitize methods if you're worried about any security issues, though I haven't gotten the chance to test them well.



## Note on Patches/Pull Requests

Expand Down
71 changes: 18 additions & 53 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,58 +1,23 @@
require 'rubygems'
require 'rake'
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
require "bundler/version"
require "shoulda/tasks"
require 'lib/gowalla'

begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "gowalla"
gem.summary = %Q{Ruby wrapper for the Gowalla API}
gem.description = %Q{Ruby wrapper for the Gowalla API}
gem.email = "wynn.netherland@gmail.com"
gem.homepage = "http://github.com/pengwynn/gowalla"
gem.authors = ["Wynn Netherland"]

gem.add_dependency('hashie', '>= 0.1.3')
gem.add_dependency('httparty', '>= 0.5.0')

gem.add_development_dependency('shoulda', '>= 2.10.1')
gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
gem.add_development_dependency('fakeweb', '>= 1.2.5')
gem.add_development_dependency "yard", ">= 0"
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end

require 'rake/testtask'
require "rake/testtask"
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
test.ruby_opts = ["-rubygems"] if defined? Gem
test.libs << "lib" << "test"
test.pattern = "test/**/*_test.rb"
end

begin
require 'rcov/rcovtask'
Rcov::RcovTask.new do |test|
test.libs << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
end
rescue LoadError
task :rcov do
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
end

desc "Build the gem"
task :build do
system "gem build gowalla.gemspec"
end

task :test => :check_dependencies

task :default => :test

begin
require 'yard'
YARD::Rake::YardocTask.new
rescue LoadError
task :yardoc do
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
end

desc "Build and release the gem"
task :release => :build do
system "gem push gowalla-#{Gowalla::VERSION}.gem"
end

task :default => :test
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Changelog
## 0.2.1 June 28, 2010
* Use Faraday middleware for JSON parsing, Mash creation
## 0.2.0 June 27, 2010
* Refactored to use Faraday
* Now supports OAuth2
## 0.1.4 May 6, 2010
* Updated accepts header
* Added patch from [Ruben Fonseca](http://github.com/rubenfonseca) to check in at a spot
Expand Down
97 changes: 12 additions & 85 deletions gowalla.gemspec
Original file line number Diff line number Diff line change
@@ -1,98 +1,25 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# -*- encoding: utf-8 -*-
require 'bundler'
require 'bundler/version'
require 'lib/gowalla'

Gem::Specification.new do |s|
s.name = %q{gowalla}
s.version = "0.1.4"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.version = Gowalla::VERSION
s.platform = Gem::Platform::RUBY
s.required_rubygems_version = ">= 1.3.6"
s.authors = ["Wynn Netherland"]
s.date = %q{2010-05-06}
s.date = %q{2010-06-26}
s.description = %q{Ruby wrapper for the Gowalla API}
s.email = %q{wynn.netherland@gmail.com}
s.extra_rdoc_files = [
"LICENSE",
"README.md"
]
s.files = [
".document",
".gitignore",
"LICENSE",
"README.md",
"Rakefile",
"VERSION",
"changelog.md",
"gowalla.gemspec",
"lib/gowalla.rb",
"lib/gowalla/client.rb",
"test/fixtures/categories.json",
"test/fixtures/category.json",
"test/fixtures/challenges.json",
"test/fixtures/checkin.json",
"test/fixtures/events.json",
"test/fixtures/find_spots.json",
"test/fixtures/find_trips.json",
"test/fixtures/friend_requests.json",
"test/fixtures/friends.json",
"test/fixtures/friends_recent.json",
"test/fixtures/item.json",
"test/fixtures/items.json",
"test/fixtures/me.json",
"test/fixtures/missing_items.json",
"test/fixtures/new_spot.json",
"test/fixtures/pins.json",
"test/fixtures/potential_twitter_friends.json",
"test/fixtures/spot.json",
"test/fixtures/spots.json",
"test/fixtures/spots_by_category.json",
"test/fixtures/stamps.json",
"test/fixtures/top_spots.json",
"test/fixtures/trip.json",
"test/fixtures/trips.json",
"test/fixtures/user.json",
"test/fixtures/vaulted_items.json",
"test/fixtures/visited_spots.json",
"test/helper.rb",
"test/test_gowalla.rb"
]
s.homepage = %q{http://github.com/pengwynn/gowalla}
s.rdoc_options = ["--charset=UTF-8"]
s.files = Dir.glob("{lib}/**/*")
s.homepage = %q{http://wynnnetherland.com/projects/gowalla/}
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.6}
s.summary = %q{Ruby wrapper for the Gowalla API}
s.summary = %q{Wrapper for the Gowalla API}
s.test_files = [
"test/helper.rb",
"test/test_gowalla.rb"
"test/gowalla_test.rb"
]

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<hashie>, [">= 0.1.3"])
s.add_runtime_dependency(%q<httparty>, [">= 0.5.0"])
s.add_development_dependency(%q<shoulda>, [">= 2.10.1"])
s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
s.add_development_dependency(%q<fakeweb>, [">= 1.2.5"])
s.add_development_dependency(%q<yard>, [">= 0"])
else
s.add_dependency(%q<hashie>, [">= 0.1.3"])
s.add_dependency(%q<httparty>, [">= 0.5.0"])
s.add_dependency(%q<shoulda>, [">= 2.10.1"])
s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
s.add_dependency(%q<fakeweb>, [">= 1.2.5"])
s.add_dependency(%q<yard>, [">= 0"])
end
else
s.add_dependency(%q<hashie>, [">= 0.1.3"])
s.add_dependency(%q<httparty>, [">= 0.5.0"])
s.add_dependency(%q<shoulda>, [">= 2.10.1"])
s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
s.add_dependency(%q<fakeweb>, [">= 1.2.5"])
s.add_dependency(%q<yard>, [">= 0"])
end
s.add_bundler_dependencies
end

13 changes: 12 additions & 1 deletion lib/gowalla.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
puts "requireing gowalla awesomeness"

require 'hashie'
require 'httparty'
require 'faraday'
require 'multi_json'
require 'oauth2'
require 'faraday_middleware'

directory = File.expand_path(File.dirname(__FILE__))

Hash.send :include, Hashie::HashExtensions

module Gowalla

VERSION = "0.2.1".freeze

# config/initializers/gowalla.rb (for instance)
#
# Gowalla.configure do |config|
Expand All @@ -27,8 +34,12 @@ class << self
attr_accessor :api_key
attr_accessor :username
attr_accessor :password
attr_accessor :api_secret
end

end


require File.join(directory, 'smashie', 'smashie')
require File.join(directory, 'gowalla', 'client')

Loading

0 comments on commit fdce43e

Please sign in to comment.