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
# 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
  show(name, data[:id])
end

#delete(name, id) ⇒ Object



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

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



40
41
42
43
44
# File 'lib/apidone-client/connection.rb', line 40

def list(name)
  response = @conn.get "/#{name}"
  collection = JSON.parse response.body
  collection.collect{|o| Apidone::Client::Resource.new(o)}
end

#show(name, id) ⇒ Object



46
47
48
49
# File 'lib/apidone-client/connection.rb', line 46

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

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



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

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