module AmendiaRemote::Api

Public Class Methods

authorize(email, password) click to toggle source
# File lib/amendia_remote/api.rb, line 7
def self.authorize(email, password)
    payload = {
        'email' => email,
        'password' => password,
    }.to_json   

    remote_response payload, AmendiaRemote.config.authorize[:url], 'post'
end
get_common_user_api_token() click to toggle source
# File lib/amendia_remote/api.rb, line 30
def self.get_common_user_api_token
    response = self.authorize(
        AmendiaRemote.config.api_user[:email],
        AmendiaRemote.config.api_user[:password]
    )

    if response && response.code == '200'
        remote_user = JSON.parse(response.body)
        return remote_user['api_token']
    end                   
end
get_course(id, api_token) click to toggle source
# File lib/amendia_remote/api.rb, line 74
def self.get_course(id, api_token)
    payload = {
        "api_token" => api_token,
    }.to_json                  
    remote_response payload, "#{AmendiaRemote.config.courses[:url]}/#{id}"
end
get_instrument(id, api_token) click to toggle source
# File lib/amendia_remote/api.rb, line 67
def self.get_instrument(id, api_token)
    payload = {
        "api_token" => api_token,
    }.to_json                  
    remote_response payload, "#{AmendiaRemote.config.instruments[:url]}/#{id}"
end
get_product(id, api_token) click to toggle source
# File lib/amendia_remote/api.rb, line 42
def self.get_product(id, api_token)            
    payload = {
        "api_token" => api_token,
    }.to_json   
    
    remote_response payload, "#{AmendiaRemote.config.product[:url]}/#{id}"
end
get_product_cat(id, api_token) click to toggle source
# File lib/amendia_remote/api.rb, line 59
def self.get_product_cat(id, api_token)
    payload = {
        "api_token" => api_token,
    }.to_json                  

    remote_response payload, "#{AmendiaRemote.config.product_cat[:url]}/#{id}"
end
get_products(category_id, api_token) click to toggle source
# File lib/amendia_remote/api.rb, line 50
def self.get_products(category_id, api_token)
    payload = {
        "api_token" => api_token,
        'category_id' => category_id,
    }.to_json

    remote_response payload, "#{AmendiaRemote.config.product[:url]}"
end
registration(email, password, api_role, first_name=nil, last_name=nil, phone=nil, recommender=nil) click to toggle source
# File lib/amendia_remote/api.rb, line 16
def self.registration(email, password, api_role, first_name=nil, last_name=nil, phone=nil, recommender=nil)
    payload = {'user' => {
        'email' => email.downcase,
        'password' => password,
        'api_role' => api_role,
        'first_name' => first_name,
        'last_name' => last_name,
        'phone' => phone,                    
        'recommender' => recommender,                    
    }}.to_json   

    remote_response payload, AmendiaRemote.config.registration[:url], 'post'
end

Private Class Methods

remote_response(payload, url, method='get') click to toggle source
# File lib/amendia_remote/api.rb, line 82
def self.remote_response(payload, url, method='get')
    host = AmendiaRemote.config.host
    port = AmendiaRemote.config.port

    if method == 'get'
        req = Net::HTTP::Get.new(url, initheader = {'Content-Type' =>'application/json'})
    elsif method == 'post'
        req = Net::HTTP::Post.new(url, initheader = {'Content-Type' =>'application/json'})
    end
    req.body = payload

    begin
        response = Net::HTTP.new(host, port).start {|http| http.request(req) }    
    rescue Exception => e
        raise CouldNotConnectError.new, e
        #raise CouldNotConnectError.new, message: e, error_code: AmendiaRemote::Config::COULD_NOT_CONNECT, status: 500
    end

    return response
end