Big goal: Making a Rails web client for my team’s existent API.
The API is built in Rails. We already have a working iOS client built off of the API. I want to make the Rails client do basically the same as the iOS app.
Subgoal: Making user authentication work.
I’m currently using HTTParty to call the API’s login url (which the iOS app uses to log in). The API uses Devise for authentication. The api_key needs to be passed in for every call that the iOS app makes. I’m assuming the same can be said about my Rails app.
Here is the User class:
require 'httparty'
class User
include HTTParty
base_uri 'api.otherapp.com'
default_params :output => 'json'
format :json
def initialize(email, password)
@ response = HTTParty.get('/api/v1/tokens?format=json', :query => {:email => email, :password => password})
end
def response
@response.inspect
end
def self.find_by_email(email)
HTTParty.get('/api/v1/users/', :query => {:email => email})
end
end
user = User.new("email@email.com", "password")
puts user.response
The current error:
/Users/janet/.rvm/rubies/ruby-2.0.0-rc1/lib/ruby/2.0.0/net/http.rb:1532:in `addr_port': undefined method `+' for nil:NilClass (NoMethodError)
from /Users/janet/.rvm/rubies/ruby-2.0.0-rc1/lib/ruby/2.0.0/net/http.rb:1465:in `begin_transport'
from /Users/janet/.rvm/rubies/ruby-2.0.0-rc1/lib/ruby/2.0.0/net/http.rb:1410:in `transport_request'
from /Users/janet/.rvm/rubies/ruby-2.0.0-rc1/lib/ruby/2.0.0/net/http.rb:1384:in `request'
from /Users/janet/.rvm/rubies/ruby-2.0.0-rc1/lib/ruby/2.0.0/net/http.rb:1377:in `block in request'
from /Users/janet/.rvm/rubies/ruby-2.0.0-rc1/lib/ruby/2.0.0/net/http.rb:851:in `start'
from /Users/janet/.rvm/rubies/ruby-2.0.0-rc1/lib/ruby/2.0.0/net/http.rb:1374:in `request'
from /Users/janet/.rvm/gems/ruby-2.0.0-rc1/gems/httparty-0.11.0/lib/httparty/request.rb:93:in `perform'
from /Users/janet/.rvm/gems/ruby-2.0.0-rc1/gems/httparty-0.11.0/lib/httparty.rb:463:in `perform_request'
from /Users/janet/.rvm/gems/ruby-2.0.0-rc1/gems/httparty-0.11.0/lib/httparty.rb:399:in `get'
from /Users/janet/.rvm/gems/ruby-2.0.0-rc1/gems/httparty-0.11.0/lib/httparty.rb:496:in `get'
from user.rb:23:in `find_by_email'
from user.rb:51:in `<main>'
What I’ve tried:
requiring ‘net/http’ in User class
searching all over stackoverflow- nothing relevant yet.
Other info:
This app does not use ActiveRecord or models because all the data will be retrieved (and sent to) the API.