0

I want to make a simple call using an API. I tried implementing ActiveResource, which may have been deprecated (see here: Consuming REST API from Rails Application).

I already have an access token for the API, and when I step through the process from the console (see quick start guide: https://github.com/teamsnap/apiv2-docs/wiki/1.-Authentication), I can get things to work (like adding a new team). However, I can't seem to figure out how to implement it in Rails. I want to start with something simple like displaying my team names on a page - that's it!

Here is the API info: https://github.com/teamsnap/apiv2-docs/wiki/Getting-Started-Guide .

I think since I already have a token (that I got via the command line tutorial), I don't need to include my username and password every time. This could be incorrect.

If someone could please give me details on how to set this up, I would greatly appreciate it! I am fairly new to rails, so feel free to give as much info as possible. Thank you!

EDIT: Thanks to Uri, it's working. To create a simple test of the API, I created a blank show.html.erb page. In teams_controller.rb, I added Uri's code into a "show" method, and add routes, as set forth below:

teams_controller.rb

def show
RestClient.post("https://api.teamsnap.com/v2/teams", '{"team": {"team_name": "Braves", "sport_id": 1, 
  "timezone": "Mountain Time (US & Canada)", "country": "United States", "zipcode": 80302}}',content_type: :json,
   x_teamsnap_token: 'your_token_goes_here_in_quotes')
end

routes.rb

get 'teams/show'
post 'teams/show'

To test the api, navigate your browser to localhost:3000/teams/show . This will manually 'fire' the show method for you. Go back to your Teams Dashboard at Teamsnap (http://go.teamsnap.com/team/dashboard). You should now have a new team called 'Braves' (see Braves name in code above)! Now that I know it's working, I can move on to other features like creating teams in my app and sending that information to TeamSnap, instead of hard-coding it as set forth above.

Community
  • 1
  • 1
David
  • 652
  • 2
  • 12
  • 21

2 Answers2

1

You can use rest-client gem to make your calls easier (though simple net/http would work just as well)

RestClient.post("https://api.teamsnap.com/v2/teams", 
  '{"team": {"team_name": "West Nile Mosquitos", "sport_id": 1, "timezone": "Mountain Time (US & Canada)", "country": "United States", "zipcode": 80302}}',
  content_type: :json, x_teamsnap_token: my_token)
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
0

You can use wrest gem to easily query web services.

Here's what you need to do

  1. Install Wrest gem by specifying gem 'wrest' in your Gemfile.

  2. To query the web services, use a syntax like so

[ GET ]     https://api.github.com/repos/c42/wrest/issues'.to_uri.get.deserialize

[POST]   'http://my.api.com'.to_uri.post('YAML encoded body', 'Content-Type' => 'text/x-yaml')

The best part is that wrest also supports caching.

Chirag
  • 967
  • 8
  • 15