Class: Apidone::Client::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/apidone-client/connection.rb

Constant Summary collapse

API_DONE_URL =
"apidone.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subdomain) ⇒ Connection

Returns a new instance of Connection.



9
10
11
12
13
14
15
16
17
# File 'lib/apidone-client/connection.rb', line 9

def initialize(subdomain)
  @subdomain = subdomain
    
  @conn = Faraday.new(:url => self.endpoint_url) do |faraday|
    faraday.request  :url_encoded             # form-encode POST params
    #faraday.response :logger                  # log requests to STDOUT
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
  end
end

Instance Attribute Details

#connObject

Returns the value of attribute conn.



7
8
9
# File 'lib/apidone-client/connection.rb', line 7

def conn
  @conn
end

#subdomainObject

Returns the value of attribute subdomain.



7
8
9
# File 'lib/apidone-client/connection.rb', line 7

def subdomain
  @subdomain
end

Instance Method Details

#create(name, data = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/apidone-client/connection.rb', line 23

def create(name, data = {})
  response = @conn.post "/#{name}", data
    
  if response.status == 201
      json_response = JSON.parse(response.body)
      data[:id] = json_response["id"]
  end
  data
end

#delete(name, id) ⇒ Object



53
54
55
56
57
58
# File 'lib/apidone-client/connection.rb', line 53

def delete(name , id)
  response = @conn.delete "/#{name}/#{id}"
  if response.status == 204
    return true
  end
end

#endpoint_urlObject



19
20
21
# File 'lib/apidone-client/connection.rb', line 19

def endpoint_url
  "http://#{subdomain}.#{API_DONE_URL}"
end

#list(name) ⇒ Object



43
44
45
46
# File 'lib/apidone-client/connection.rb', line 43

def list(name)
  response = @conn.get "/#{name}"
  JSON.parse response.body
end

#show(name, id) ⇒ Object



48
49
50
51
# File 'lib/apidone-client/connection.rb', line 48

def show(name, id)
  response = @conn.get("/#{name}/#{id}")
  JSON.parse response.body
end

#update(name, id, data = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/apidone-client/connection.rb', line 33

def update(name, id, data = {})
  response = @conn.put "/#{name}/#{id}", data
    
  if response.status == 201
    json_response = JSON.parse(response.body)
  end
  
  data
end