Class: ZooppaApiV3

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

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ ZooppaApiV3

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



20
21
22
23
24
25
26
27
28
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# 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

Instance Method Details

#add_auth_token(token) ⇒ Object

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



167
168
169
170
171
172
173
174
175
176
177
178
179
# 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_applicationObject



181
182
183
184
185
186
187
188
189
190
191
# 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) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
# 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_urlObject

Builds the URL (adds an id of provided)



116
117
118
119
120
121
122
123
124
125
126
# 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_upObject

TODO: find a better solution for that



109
110
111
112
113
# 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) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# 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) ⇒ Object



47
48
49
50
51
# File 'lib/zooppa_api_v3.rb', line 47

def create(params)
  @method = :post
  @params = params
  self
end

#decrypt_token(encrypted_token) ⇒ Object



205
206
207
# 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

#deleteObject



92
93
94
95
# File 'lib/zooppa_api_v3.rb', line 92

def delete
  @method = :delete
  self
end

#encrypt_token(token) ⇒ Object



209
210
211
# 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) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# 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) ⇒ Object



74
75
76
77
78
# File 'lib/zooppa_api_v3.rb', line 74

def find(id)
  @method = :get
  @id = id.to_s
  self
end

#page(page) ⇒ Object



80
81
82
83
84
# File 'lib/zooppa_api_v3.rb', line 80

def page(page)
  @method = :get
  @page_params = "page=#{page.to_s}"
  self
end

#pagination_paramsObject



153
154
155
156
157
158
159
160
161
162
163
164
# 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) ⇒ Object



213
214
215
216
# 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) ⇒ Object



86
87
88
89
90
# 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_paramsObject



140
141
142
143
144
145
146
147
148
149
150
151
# 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) ⇒ Object



68
69
70
71
72
# 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) ⇒ Object



41
42
43
44
45
# File 'lib/zooppa_api_v3.rb', line 41

def update_attributes(params)
  @method = :patch
  @params = params
  self
end

#where(*args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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