Class: ZooppaApi

Inherits:
Object
  • Object
show all
Defined in:
lib/zooppa_api.rb

Overview

DELETE destroy#companies (with auth) RestRequest.new(‘companies’, authenticate: true, cookies: cookies, method: :delete, id: “10”)

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ ZooppaApi

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



47
48
49
50
51
52
53
54
55
# File 'lib/zooppa_api.rb', line 47

def initialize(**args)
  @api_host = args.fetch(:api_host) { 'http://zooppa.com/' }
  @version = args.fetch(:version) { 'v2' }
  @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

Instance Method Details

#add_auth_tokenObject

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/zooppa_api.rb', line 92

def add_auth_token
  if @encrypt && @authenticate
    token = decrypt_token(@cookies[:auth_token])
  else
    token = @cookies[:auth_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(email, password) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/zooppa_api.rb', line 108

def authenticate(email, password)
  client = OAuth2::Client.new(
             @app_id,
             @app_secret,
             site: @api_host
            )
  token = client.password.get_token(email, password).token
  @encrypt ? encrypt_token(token) : token
rescue => e
  parse_error_message(e)
end

#build_urlObject

Builds the URL (adds an id of provided)



83
84
85
86
87
88
89
# File 'lib/zooppa_api.rb', line 83

def build_url
  url = @api_host + 'api/' + @version + '/' + @resource
  url += '/' + @id if @id
  url += '.json'
  url += '?' + @params.to_param if [:get, :delete].include?(@method) && !@params.empty?
  url
end

#decrypt_token(encrypted_token) ⇒ Object



120
121
122
# File 'lib/zooppa_api.rb', line 120

def decrypt_token(encrypted_token)
  Encryptor.decrypt(encrypted_token, :key => @app_id, :iv => @iv, :salt =>  @app_secret)
end

#do_request(resource, **args) ⇒ Object

Executes the request



58
59
60
61
62
63
64
65
# File 'lib/zooppa_api.rb', line 58

def do_request(resource, **args)
  prepare_request(resource, **args)
  if [:post, :put, :patch].include?(@method)
    JSON.parse(RestClient.send(@method, @url, @params))
  else
    JSON.parse(RestClient.send(@method, @url))
  end
end

#encrypt_token(token) ⇒ Object



124
125
126
# File 'lib/zooppa_api.rb', line 124

def encrypt_token(token)
  Encryptor.encrypt(token, :key => @app_id, :iv => @iv, :salt =>  @app_secret)
end

#parse_error_message(e) ⇒ Object



128
129
130
131
# File 'lib/zooppa_api.rb', line 128

def parse_error_message(e)
  msg = e.try(:code) == 'invalid_resource_owner' ? 'Invalid email or password.' : 'There seems to be a connection problem'
  { error: msg }
end

#prepare_request(resource, **args) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/zooppa_api.rb', line 67

def prepare_request(resource, **args)
  @resource = resource
  @method = args.fetch(:method) { :get }
  @id = args.fetch(:id) { nil }
  @params = args.fetch(:params) { {} }
  @cookies = args.fetch(:cookies) { {} }
  @authenticate = args.fetch(:authenticate) { false }
  @url = build_url

  if @authenticate
    fail Exceptions::MissingAuthToken unless @cookies.key?(:auth_token)
    add_auth_token
  end
end