class ZooppaApiV3

Public Class Methods

new(**args) click to toggle source

Initializes the Rest Request resource: string - resource name - must be plural (i.e. 'invitations', 'companies') method: sym - HTTP VERB (:post, :put, :delete, :get) cookies: Rails cookies Object - from controller - is required when 'authenticate' is set to true params: hash - params from controller id: integer - resource id - required for show, update & destroy action only authenticate: boolean - API call requires authentication (true) or not (false)? api_version: string - specifies version of API - default: v2

Here we use KEYWORD ARGUMENTS (new in ruby 2.0.0): magazine.rubyist.net/?Ruby200SpecialEn-kwarg

# File lib/zooppa_api_v3.rb, line 20
def initialize(**args)
  @api_host = args.fetch(:api_host) { 'http://localhost:3000/' }
  @version = args.fetch(:version) { 'v3' }
  @app_secret = args.fetch(:app_secret) { 'app_secret' }
  @app_id = args.fetch(:app_id) { 'app_id' }
  @encrypt = args.fetch(:encrypt) { true }
  # needed for encrypting the access token
  @iv = args.fetch(:iv) { '' }
end

Public Instance Methods

add_auth_token(token) click to toggle source

Adds the auth_token to the params hash or url (depending on the HTTP verb)

# File lib/zooppa_api_v3.rb, line 167
def add_auth_token(token)
  if @encrypt
    token = decrypt_token(token)
  end

  if [:get, :delete].include?(@method) && !@params.empty?
    @url += '&access_token=' + token
  elsif [:get, :delete].include?(@method) && @params.empty?
    @url += '?access_token=' + token
  else
    @params.merge!(access_token: token)
  end
end
authenticate_application() click to toggle source
# File lib/zooppa_api_v3.rb, line 181
def authenticate_application
  client = OAuth2::Client.new(
             @app_id,
             @app_secret,
             site: @api_host
            )
  token = client.client_credentials.get_token.token
  @encrypt ? encrypt_token(token) : token
rescue => e
  parse_error_message(e)
end
authenticate_user(email, password) click to toggle source
# File lib/zooppa_api_v3.rb, line 193
def authenticate_user(email, password)
  client = OAuth2::Client.new(
             @app_id,
             @app_secret,
             site: @api_host + '.json'
            )
  token = client.password.get_token(email, password).token
  @encrypt ? encrypt_token(token) : token
rescue => e
  parse_error_message(e)
end
build_url() click to toggle source

Builds the URL (adds an id of provided)

# File lib/zooppa_api_v3.rb, line 116
def build_url
  url = @api_host + 'api/' + @version + '/' + @resource
  url += '/' + @id if @id
  url += '.json'
  if [:delete, :get].include?(@method)
    @params = complete_parameters(query_params, pagination_params) if [:delete, :get].include?(@method)
    url += '?' + @params unless @params == ''
  end
  clean_up
  url
end
clean_up() click to toggle source

TODO: find a better solution for that

# File lib/zooppa_api_v3.rb, line 109
def clean_up
  @filter_params = nil
  @id = nil
  @sort_params = nil
end
complete_parameters(query_params, pagination_params) click to toggle source
# File lib/zooppa_api_v3.rb, line 128
def complete_parameters(query_params, pagination_params)
  if query_params != '' && pagination_params != ''
    return [query_params, pagination_params].join('&')
  elsif query_params == ''
    return pagination_params
  elsif pagination_params == ''
    return query_params
  else
    return ''
  end
end
create(params) click to toggle source
# File lib/zooppa_api_v3.rb, line 47
def create(params)
  @method = :post
  @params = params
  self
end
decrypt_token(encrypted_token) click to toggle source
# File lib/zooppa_api_v3.rb, line 205
def decrypt_token(encrypted_token)
  Encryptor.decrypt(encrypted_token, :key => @app_id, :iv => @iv, :salt =>  @app_secret)
end
delete() click to toggle source
# File lib/zooppa_api_v3.rb, line 92
def delete
  @method = :delete
  self
end
encrypt_token(token) click to toggle source
# File lib/zooppa_api_v3.rb, line 209
def encrypt_token(token)
  Encryptor.encrypt(token, :key => @app_id, :iv => @iv, :salt =>  @app_secret)
end
execute(token = nil) click to toggle source
# File lib/zooppa_api_v3.rb, line 97
def execute(token = nil)
  @url = build_url

  add_auth_token(token) if token
  if [:post, :put, :patch].include?(@method)
    JSON.parse(RestClient.send(@method, @url, @params))
  else
    JSON.parse(RestClient.send(@method, @url))
  end
end
find(id) click to toggle source
# File lib/zooppa_api_v3.rb, line 74
def find(id)
  @method = :get
  @id = id.to_s
  self
end
method_missing(method,*args,&block) click to toggle source
# File lib/zooppa_api_v3.rb, line 30
def method_missing(method,*args,&block)
  eigenclass = class << self; self; end
  eigenclass.class_eval do
    define_method(method) do
      @resource = method.to_s
      return self
    end
  end
  send(method, *args, &block)
end
page(page) click to toggle source
# File lib/zooppa_api_v3.rb, line 80
def page(page)
  @method = :get
  @page_params = "page=#{page.to_s}"
  self
end
pagination_params() click to toggle source
# File lib/zooppa_api_v3.rb, line 153
def pagination_params
  pagination_params = ''
  if @page_params
    pagination_params += @page_params
  end

  if @per_page_params
    pagination_params += '&' unless pagination_params == ''
    pagination_params += @per_page_params
  end
  return pagination_params
end
parse_error_message(e) click to toggle source
# File lib/zooppa_api_v3.rb, line 213
def parse_error_message(e)
  msg = e.try(:code) == 'invalid_grant' ? 'Invalid email or password.' : 'There seems to be a connection problem'
  { error: msg }.as_json
end
per_page(per_page) click to toggle source
# File lib/zooppa_api_v3.rb, line 86
def per_page(per_page)
  @method = :get
  @per_page_params = "per_page=#{per_page.to_s}"
  self
end
query_params() click to toggle source
# File lib/zooppa_api_v3.rb, line 140
def query_params
  query_params = ''
  if @filter_params
    query_params += @filter_params
  end

  if @sort_params
    query_params += '&' unless query_params == ''
    query_params += @sort_params
  end
  return query_params
end
sort(sort_by) click to toggle source
# File lib/zooppa_api_v3.rb, line 68
def sort(sort_by)
  @method = :get
  @sort_params = "sort_by#{sort_by.to_query('')}"
  self
end
update_attributes(params) click to toggle source
# File lib/zooppa_api_v3.rb, line 41
def update_attributes(params)
  @method = :patch
  @params = params
  self
end
where(*args) click to toggle source
# File lib/zooppa_api_v3.rb, line 53
def where(*args)
  @method = :get
  @filter_params = ''
  args.each_with_index do |filter, index|
    @filter_params += '&' unless @filter_params == ''
    if filter.split(':')[0] == 'custom'
      # TODO: arguments need to be supported
      @filter_params += URI.encode("custom_filters[][method]=#{filter.split(':')[1]}")
    else
      @filter_params += URI.encode("filters[]=#{filter}")
    end
  end
  self
end