Class: Mozzn::Api

Inherits:
Object show all
Defined in:
lib/mozzn/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token = nil) ⇒ Api

Returns a new instance of Api.



10
11
12
13
14
15
16
17
# 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

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



7
8
9
# File 'lib/mozzn/api.rb', line 7

def connection
  @connection
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/mozzn/api.rb', line 8

def token
  @token
end

Instance Method Details

#get(path, parms) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# 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