class Mozzn::Api

Attributes

connection[RW]
token[RW]

Public Class Methods

new(token = nil) click to toggle source
# File lib/mozzn/api.rb, line 10
def initialize token = nil
  if ENV['GEM_ENV'] == 'test'
    @connection = Faraday.new('http://localhost:3000/api/v1/')
  else
    @connection = Faraday.new('http://mozzn.com/api/v1/')
  end
  @token = token
end

Public Instance Methods

get(path, parms) click to toggle source
# File lib/mozzn/api.rb, line 19
def get path, parms
  begin
    response = @connection.get uri(path), parms
  rescue Faraday::Error::ConnectionFailed => e
    raise Mozzn::Disconnected
  end
  begin
    JSON.parse(response.body)
  rescue JSON::ParserError => e
    raise Mozzn::UnexpectedOutput
  end
  
end
post(path, parms) click to toggle source
# File lib/mozzn/api.rb, line 33
def post path, parms
  begin
    response = @connection.post uri(path), parms
  rescue Faraday::Error::ConnectionFailed => e
    raise Mozzn::Disconnected
  end
  begin
    JSON.parse(response.body)
  rescue JSON::ParserError => e
    raise Mozzn::UnexpectedOutput
  end
  
end

Private Instance Methods

uri(path) click to toggle source
# File lib/mozzn/api.rb, line 49
def uri path
  uri = []
  uri << path.to_s
  uri << "auth_token=#{@token}" unless @token.nil?
  uri.join('?')
end