Class: Yoolinkpro::Api

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

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Yoolinkpro::Api

Yoolinkpro::Api

client = Yoolinkpro::Client.new
api = Yoolinkpro::Api.new client

Parameters:



15
16
17
18
19
20
# File 'lib/yoolinkpro/api.rb', line 15

def initialize(client)
  super
  @millitime = Time.now.to_i*1000
  @int = rand(100000)
  @key_scopes = { :private => private_key, :admin => admin_key }
end

Instance Method Details

#authenticate(email, password) ⇒ RestClient::Response

User authentication

api.authenticate "[email protected]", "password"

Parameters:

  • email (String)
  • password (String)

Returns:

  • (RestClient::Response)


29
30
31
32
33
34
# File 'lib/yoolinkpro/api.rb', line 29

def authenticate(email, password)
  params = { :email => email, :password => Base64.strict_encode64(crypted_password(password)) }
  build_uri("/user/authenticate.json", :query => params.to_query)
  build_key(:post, private_key, params)
  RestClient.post @uri.to_s, params.to_query, http_headers
end

#create(obj, params = {}, scope = :admin) ⇒ RestClient::Response

Create object such as :user, :team, :link, :comment

api.create :group, :name => "Api", :description => "Everything about Api"

Parameters:

  • obj (Symbol)
  • params (Hash) (defaults to: {})
  • scope (Symbol) (defaults to: :admin)

    for this request

Returns:

  • (RestClient::Response)


98
99
100
101
102
# File 'lib/yoolinkpro/api.rb', line 98

def create(obj, params = {}, scope = :admin)
  build_uri("/#{obj}.json", :query => params.to_query)
  build_key(:post, scope, params)
  RestClient.post @uri.to_s, params.to_query, http_headers
end

#delete(obj, id, params = {}, scope = :admin) ⇒ RestClient::Response

Delete object

api.delete :comment, 1, { :identity_token => auth[:identity_token] } :private

Parameters:

  • obj (Symbol)
  • id (Fixnum)
  • params (Hash) (defaults to: {})
  • scope (Symbol) (defaults to: :admin)

    for this request

Returns:

  • (RestClient::Response)


128
129
130
131
132
# File 'lib/yoolinkpro/api.rb', line 128

def delete(obj, id, params = {}, scope = :admin)
  build_uri("/#{obj}/#{id}.json", :query => params.to_query)
  build_key(:delete, scope, params)
  RestClient.delete @uri.to_s, http_headers
end

#find(obj, id, scope = :private) ⇒ RestClient::Response

Find object such as user, team, link

api.find :user, 1
api.find :team, 1234

Parameters:

  • obj (Symbol)
  • id (Fixnum)
  • scope (Symbol) (defaults to: :private)

    for this request

Returns:

  • (RestClient::Response)


56
57
58
59
60
# File 'lib/yoolinkpro/api.rb', line 56

def find(obj, id, scope = :private)
  build_uri("/#{obj}/#{id}.json")
  build_key(:get, scope)
  RestClient.get @uri.to_s, http_headers
end

#find_all(obj, scope = :admin) ⇒ RestClient::Response

Find all object such as users, teams, groups

api.find_all :users
api.find_all :groups

Parameters:

  • obj (Symbol)

    pluralize

  • scope (Symbol) (defaults to: :admin)

    for this request

Returns:

  • (RestClient::Response)


70
71
72
73
74
# File 'lib/yoolinkpro/api.rb', line 70

def find_all(obj, scope = :admin)
  build_uri("/#{obj}.json")
  build_key(:get, scope)
  RestClient.get @uri.to_s, http_headers
end

#open_session(email) ⇒ RestClient::Response

Create an url to open a new session

api.open_session "[email protected]"

Parameters:

  • email (String)

    of an existing YoolinkPro user

Returns:

  • (RestClient::Response)


42
43
44
45
# File 'lib/yoolinkpro/api.rb', line 42

def open_session(email)
  @key = "get/open_sessionemail=#{email}#{admin_key}#{@millitime}#{@int}"
  RestClient::Response.create "https://#{Yoolinkpro::API_SERVER}/open_session?email=#{CGI.escape(email)}&X-YP-AppKey=#{public_key}&X-YP-Signature=#{CGI.escape(signature)}&X-YP-MilliTime=#{@millitime}&X-YP-Int=#{@int}".to_json, nil, nil
end

#search(obj, params = {}, scope = :private) ⇒ RestClient::Response

Search object such as :user

api.search :user, :email => "[email protected]"

Parameters:

  • obj (Symbol)
  • params (Fixnum) (defaults to: {})
  • scope (Symbol) (defaults to: :private)

    for this request

Returns:

  • (RestClient::Response)


84
85
86
87
88
# File 'lib/yoolinkpro/api.rb', line 84

def search(obj, params = {}, scope = :private)
  build_uri("/#{obj}/search.json", :query => params.to_query)
  build_key(:get, scope, params)
  RestClient.get @uri.to_s, http_headers
end

#update(obj, id, params = {}, scope = :admin) ⇒ RestClient::Response

Update object such as :user, :link

api.update :user, 1, :firstname => "Vincent"

Parameters:

  • obj (Symbol)
  • id (Fixnum)
  • params (Hash) (defaults to: {})
  • scope (Symbol) (defaults to: :admin)

    for this request

Returns:

  • (RestClient::Response)


113
114
115
116
117
# File 'lib/yoolinkpro/api.rb', line 113

def update(obj, id, params = {}, scope = :admin)
  build_uri("/#{obj}/#{id}.json", :query => params.to_query)
  build_key(:put, scope, params)
  RestClient.put @uri.to_s, params.to_query, http_headers
end