zooppa_api 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/zooppa_api.rb +131 -0
  3. metadata +114 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4d1955b69eae804f624281b446291d70addabf30
4
+ data.tar.gz: cf07034a60e450100af697fc22cd7997d3f19fe2
5
+ SHA512:
6
+ metadata.gz: b11ee07eb513bac401a384a11ab3ca05d384612953ab56d95928e2dbba9e3894394e1cd2f79037e91337a2e35df961b00203a42516f2dba5daa9c61e70852089
7
+ data.tar.gz: 418450a1544fc58bad188afe10ea3c7e18d7c205499d850f1b31dce9772f1c854478194baed732e59d7b1ff2dada78829156543ed7eb5a1e6ea21782f8d4f585
data/lib/zooppa_api.rb ADDED
@@ -0,0 +1,131 @@
1
+ require 'rest_client'
2
+ require 'json'
3
+ require 'active_support/core_ext'
4
+ require 'oauth2'
5
+ require 'encryptor'
6
+
7
+ # Builds a Rest Request
8
+ # Handles authentication which should slim down logic inside our controllers
9
+ #
10
+ # Each resource controller of Zooppa's API V2 has the following endpoints:
11
+ # GET /api/v1/resources.json resources#index
12
+ # POST /api/v1/resources.json resources#create
13
+ # GET /api/v1/resources/:id.json resources#show
14
+ # PATCH/PUT /api/v1/resources/:id.json resources#update
15
+ # DELETE /api/v1/resources/:id.json resources#destroy
16
+
17
+ ## USAGE examples (from the controller)
18
+
19
+ # GET index#companies (with auth)
20
+ # RestRequest.new('companies', authenticate: true, cookies: cookies).do_request
21
+
22
+ # GET show#companies (with auth)
23
+ # RestRequest.new('companies', authenticate: true, cookies: cookies, id: '8').do_request
24
+
25
+ # POST create#companies (with auth)
26
+ # RestRequest.new('companies', authenticate: true, cookies: cookies, method: :post, params: {company: { name: 'company name'}})
27
+
28
+ # PUT update#companies (with auth)
29
+ # RestRequest.new('companies', authenticate: true, cookies: cookies, method: :put, id: "10", params: {company: { name: 'new company name'}})
30
+
31
+ # DELETE destroy#companies (with auth)
32
+ # RestRequest.new('companies', authenticate: true, cookies: cookies, method: :delete, id: "10")
33
+
34
+ class ZooppaApi
35
+
36
+ # Initializes the Rest Request
37
+ # resource: string - resource name - must be plural (i.e. 'invitations', 'companies')
38
+ # method: sym - HTTP VERB (:post, :put, :delete, :get)
39
+ # cookies: Rails cookies Object - from controller - is required when 'authenticate' is set to true
40
+ # params: hash - params from controller
41
+ # id: integer - resource id - required for show, update & destroy action only
42
+ # authenticate: boolean - API call requires authentication (true) or not (false)?
43
+ # api_version: string - specifies version of API - default: v2
44
+ #
45
+ # Here we use KEYWORD ARGUMENTS (new in ruby 2.0.0): http://magazine.rubyist.net/?Ruby200SpecialEn-kwarg
46
+ def initialize(**args)
47
+ @api_host = args.fetch(:api_host) { 'http://zooppa.com/' }
48
+ @version = args.fetch(:version) { 'v2' }
49
+ @app_secret = args.fetch(:app_secret) { 'app_secret' }
50
+ @app_id = args.fetch(:app_id) { 'app_id' }
51
+ @encrypt = args.fetch(:encrypt) { true }
52
+ # needed for encrypting the access token
53
+ @iv = OpenSSL::Cipher::Cipher.new('aes-128-cbc').random_iv
54
+ end
55
+
56
+ # Executes the request
57
+ def do_request(resource, **args)
58
+ prepare_request(resource, **args)
59
+ if [:post, :put, :patch].include?(@method)
60
+ JSON.parse(RestClient.send(@method, @url, @params))
61
+ else
62
+ JSON.parse(RestClient.send(@method, @url))
63
+ end
64
+ end
65
+
66
+ def prepare_request(resource, **args)
67
+ @resource = resource
68
+ @method = args.fetch(:method) { :get }
69
+ @id = args.fetch(:id) { nil }
70
+ @params = args.fetch(:params) { {} }
71
+ @cookies = args.fetch(:cookies) { {} }
72
+ @authenticate = args.fetch(:authenticate) { false }
73
+ @url = build_url
74
+
75
+ if @authenticate
76
+ fail Exceptions::MissingAuthToken unless @cookies.key?(:auth_token)
77
+ add_auth_token
78
+ end
79
+ end
80
+
81
+ # Builds the URL (adds an id of provided)
82
+ def build_url
83
+ url = @api_host + 'api/' + @version + '/' + @resource
84
+ url += '/' + @id if @id
85
+ url += '.json'
86
+ url += '?' + @params.to_param if [:get, :delete].include?(@method) && !@params.empty?
87
+ url
88
+ end
89
+
90
+ # Adds the auth_token to the params hash or url (depending on the HTTP verb)
91
+ def add_auth_token
92
+ if @encrypt && @authenticate
93
+ token = decrypt_token(@cookies[:auth_token])
94
+ else
95
+ token = @cookies[:auth_token]
96
+ end
97
+
98
+ if [:get, :delete].include?(@method) && !@params.empty?
99
+ @url += '&access_token=' + token
100
+ elsif [:get, :delete].include?(@method) && @params.empty?
101
+ @url += '?access_token=' + token
102
+ else
103
+ @params.merge!(access_token: token)
104
+ end
105
+ end
106
+
107
+ def authenticate(email, password)
108
+ client = OAuth2::Client.new(
109
+ @app_id,
110
+ @app_secret,
111
+ site: @api_host
112
+ )
113
+ token = client.password.get_token(email, password).token
114
+ @encrypt ? encrypt_token(token) : token
115
+ rescue => e
116
+ parse_error_message(e)
117
+ end
118
+
119
+ def decrypt_token(encrypted_token)
120
+ Encryptor.decrypt(encrypted_token, :key => @app_id, :iv => @iv, :salt => @app_secret)
121
+ end
122
+
123
+ def encrypt_token(token)
124
+ Encryptor.encrypt(token, :key => @app_id, :iv => @iv, :salt => @app_secret)
125
+ end
126
+
127
+ def parse_error_message(e)
128
+ msg = e.try(:code) == 'invalid_resource_owner' ? 'Invalid email or password.' : 'There seems to be a connection problem'
129
+ { error: msg }
130
+ end
131
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zooppa_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Ulrich Soeffing
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: oauth2
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: encryptor
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Easy peasy communication for the API of Zooppa
84
+ email: ulrich.soeffing@staunchrobots.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - lib/zooppa_api.rb
90
+ homepage: http://github.com/soeffing/zooppa_api
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.1.11
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Ruby Wrapper for Zooppa's API
114
+ test_files: []