Class: NinjaBlocks::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ninja_blocks/base.rb

Direct Known Subclasses

Device, Rule, User

Instance Method Summary collapse

Instance Method Details

#connectionObject



17
18
19
20
21
22
# File 'lib/ninja_blocks/base.rb', line 17

def connection
  @connection = Faraday.new(:url => 'https://api.ninja.is')
  @connection.headers['accept'] = 'application/json'
  @connection.headers['Content-Type'] = 'application/json'
  @connection
end

#delete(url, options = {}) ⇒ Object



13
14
15
# File 'lib/ninja_blocks/base.rb', line 13

def delete(url,options={})
  execute(:delete,url,options)
end

#execute(method, url, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/ninja_blocks/base.rb', line 28

def execute(method,url,options={})

  unless options.empty?
    response = connection.send(method,"#{url}?#{options}&user_access_token=#{self.token}")
  else
    response = connection.send(method,"#{url}?user_access_token=#{self.token}")
  end
  JSON.parse(response.body)
end

#get(url, options = {}) ⇒ Object



3
4
5
# File 'lib/ninja_blocks/base.rb', line 3

def get(url,options={})
  execute(:get,url,options)
end

#post(url, options = {}) ⇒ Object



9
10
11
# File 'lib/ninja_blocks/base.rb', line 9

def post(url,options={})
  execute(:post,url,options)
end

#put(url, options = {}) ⇒ Object



6
7
8
# File 'lib/ninja_blocks/base.rb', line 6

def put(url,options={})
  execute(url, json)
end

#put_json(url, json) ⇒ Object

ugly hack, but I’m too tired to think



38
39
40
41
42
43
# File 'lib/ninja_blocks/base.rb', line 38

def put_json(url, json)
   puts url
   puts json
   response = connection.send(:put, "#{url}?user_access_token=#{self.token}", json)
   puts response
end

#tokenObject



24
25
26
# File 'lib/ninja_blocks/base.rb', line 24

def token
  @token || NinjaBlocks.token
end