Class: Appfront::API

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ API

Returns a new instance of API.



7
8
9
10
11
12
13
14
15
# File 'lib/appfront/api.rb', line 7

def initialize(opts = {})
  @host  = opts[:host] || 'https://api.appfront.io/v1'

  if opts[:email] and opts[:password]
    @email, @pass = opts[:email], opts[:password]
  else
    @email, @pass = Appfront::Command::Auth.credentials
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/appfront/api.rb', line 41

def method_missing(m, *args, &block)
  unless ['get', 'post', 'put', 'delete'].include? m.to_s
    raise NoMethodError, "undefined method: #{m}"
  end

  res = self.send('req', m, args)
  #TODO Da capire JSON.parse res rescue ""
  JSON.parse res rescue res
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



5
6
7
# File 'lib/appfront/api.rb', line 5

def email
  @email
end

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/appfront/api.rb', line 5

def host
  @host
end

Instance Method Details

#loginObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/appfront/api.rb', line 17

def 
  return unless (@email && @pass)

  @pass = RestClient.post(
    "#{@host}/auth",
    email: @email, password: @pass
  )
rescue => e
  puts e.response
  exit 1
end

#signupObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/appfront/api.rb', line 29

def 
  return unless (@email && @pass)

  @pass = RestClient.post(
    "#{@host}/signup",
    email: @email, password: @pass
  )
rescue => e
  puts e.response
  exit 1
end