rsperantito 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 25ac832e6d72f437e1be8588c238896232b8ed32eba19b13951c95171591b01d
4
+ data.tar.gz: 28fe2f4171e423bae4c261f99f65fcf345e94c49e11dfa461015267c169b7e12
5
+ SHA512:
6
+ metadata.gz: 514b3163a6025e7bfef5b90e6490a8cdfedde776dec49949abd19466c45e3422066ec8b6137e9abb2b7d2d8e4212318530181c20435badb73e0e21a87d1f8cf1
7
+ data.tar.gz: a834666cf3c310a5fad20705ddbf227263124801400fa14551fbcbe8ecc704dfa8d85944cc6945c9893eaeec48d8147513b44180e5432a24a329c971d8a8b3d9
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *DS_Store
2
+ .bundle
3
+ .yardoc
4
+ doc
5
+ pkg
6
+ Gemfile.lock
7
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LINCESE ADDED
@@ -0,0 +1,20 @@
1
+ MIT LICENSE
2
+
3
+ Copyright (c) 2019 Gerson Eufracio <gersoneufra>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN C
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,9 @@
1
+ require 'rsperantito/auth'
2
+ require 'rsperantito/version'
3
+
4
+ module RSperantito
5
+ autoload :Base, 'rsperantito/base'
6
+ autoload :Client, 'rsperantito/client'
7
+ autoload :CaptationWay, 'rsperantito/captation_way'
8
+ autoload :Project, 'rsperantito/project'
9
+ end
@@ -0,0 +1,71 @@
1
+ require 'json'
2
+ require 'restclient'
3
+ require 'addressable'
4
+
5
+ module RSperantito
6
+ class ExceptionAuthenticate < StandardError; end
7
+
8
+ API_URI = 'https://api.sperant.com/v2/'.freeze
9
+ TOKEN_URI = 'https://api.sperant.com/oauth/token'.freeze
10
+ VERBS = %w[get post put delete].freeze
11
+
12
+ class << self
13
+ attr_reader :client_token
14
+
15
+ def authenticate(client_id, client_secret)
16
+ @client_id, @client_secret = client_id, client_secret
17
+ params =
18
+ {grant_type: 'client_credentials',
19
+ client_id: @client_id,
20
+ client_secret: @client_secret}
21
+
22
+ response = RestClient::Resource.new(
23
+ TOKEN_URI,
24
+ verify_ssl: false).post(params)
25
+ @client_token = JSON.parse(response)['access_token']
26
+ true
27
+ end
28
+
29
+ VERBS.each do |verb|
30
+ define_method verb do |path, *params|
31
+ params << {'Authorization' => "Bearer #{client_token}"}
32
+ send_request(verb, path, *params)
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def send_request(verb, path, *params)
39
+ url = path.start_with?('http') ? path : API_URI + path
40
+ url, query = *url.split('?')
41
+ url = Addressable::URI.encode(url)
42
+ url << "?#{query}" if query
43
+
44
+ begin
45
+ headers = get_headers(params)
46
+
47
+ response = RestClient::Resource.new(
48
+ url,
49
+ headers: headers,
50
+ verify_ssl: false).send(verb, *params)
51
+ rescue RestClient::Unauthorized => e
52
+
53
+ raise ExceptionAuthenticate unless @client_token
54
+ authenticate(@client_id, @client_secret)
55
+ headers = get_headers(params)
56
+ headers['Authorization'] = "Bearer #{@client_token}"
57
+ response = retry_connection(verb, url, params)
58
+ end
59
+
60
+ JSON.parse(response) unless response.nil? || response.empty?
61
+ end
62
+
63
+ def retry_connection(verb, url, params)
64
+ RestClient.send(verb, url, *params)
65
+ end
66
+
67
+ def get_headers(params)
68
+ params.find{|param| param.is_a?(Hash) && param['Authorization']}
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,26 @@
1
+ module RSperantito
2
+ class Base
3
+ def self.find(id, type)
4
+ url = "#{type}s/#{id}"
5
+ response = RSperantito.get(url)
6
+ response[type]
7
+ end
8
+
9
+ def self.list(page=1, type, **options)
10
+ url = "#{type}s?page=#{page}"
11
+ options.each do |option, value|
12
+ url << "&#{option}=#{value}"
13
+ end
14
+
15
+ response = RSperantito.get(url)
16
+ response["#{type}s"]
17
+ end
18
+
19
+ def self.search(query, type, page=1)
20
+ url = "#{type}s?q=#{query}"
21
+
22
+ response = RSperantito.get(url)
23
+ response["#{type}s"][0]
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ module RSperantito
2
+ class CaptationWay < Base
3
+ def self.list(page=1)
4
+ super(page, 'captation_way')
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ module RSperantito
2
+ class Client < Base
3
+
4
+ def self.find(id)
5
+ super(id, 'client')
6
+ end
7
+
8
+ def self.list(page=1, **options)
9
+ super(page, 'client', options)
10
+ end
11
+
12
+ def self.search(q, page=1)
13
+ super(q, 'client', page)
14
+ end
15
+
16
+ def self.create(**params)
17
+ url = 'clients'
18
+
19
+ response = RSperantito.post(url, params)
20
+ response['client']
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ module RSperantito
2
+
3
+ class Project < Base
4
+
5
+ def self.find(id)
6
+ super(id, 'project')
7
+ end
8
+
9
+ def self.list(page=1, **options)
10
+ super(page, 'project', options)
11
+ end
12
+
13
+ def self.search(query, page=1)
14
+ super(query, 'project', page)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module RSperantito
2
+ VERSION = '0.0.1'.freeze
3
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rsperantito/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = %q{rsperantito}
8
+ spec.version = RSperantito::VERSION
9
+ spec.authors = ['Gerson Eufracio']
10
+ spec.email = ['gersoneufra@gmail.com']
11
+ spec.summary = %q{ruby client for the SPERANT API}
12
+ spec.homepage = 'http://rubygems.org/gems/rsperantito'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.test_files = spec.files.grep(/^spec\//)
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_dependency 'rest-client', '~> 2.0.2'
20
+
21
+ spec.add_development_dependency 'bundler'
22
+ spec.add_development_dependency 'webmock'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rspec'
25
+ spec.add_development_dependency 'yard'
26
+ spec.add_development_dependency 'vcr', '~> 3.0'
27
+
28
+ spec.required_ruby_version = '>= 2.0.0'
29
+ end
@@ -0,0 +1,12 @@
1
+ module AuthenticationHelper
2
+ TOKEN = '174647bb3907a85fbbaf189efacdcc533d6edc53b702593eaf10219678aa9eee'
3
+
4
+ def authenticate_client
5
+ client_id = '7be79214adc249df43be17a2baca54ec9d892b581e8a5edb0c021b5b53ceea7e'
6
+ client_secret = '2d1182c427e852c5cd4cc1811233abdd08a661dd95283bbeab70c3af0da351e8'
7
+
8
+ VCR.use_cassette('authenticate:client') do
9
+ RSperantito.authenticate(client_id, client_secret)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ describe RSperantito::CaptationWay do
2
+
3
+ describe 'CaptationWay::list' do
4
+ it "should get captation_ways" do
5
+ captation_ways = VCR.use_cassette('captation_ways:list') do
6
+ RSperantito::CaptationWay.list
7
+ end
8
+
9
+ expect(captation_ways.size).to eq(16)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,111 @@
1
+ describe RSperantito::Client do
2
+
3
+ before do
4
+ authenticate_client
5
+ end
6
+
7
+ describe 'Client::list' do
8
+ it "should get clients" do
9
+ clients = VCR.use_cassette('clients:list') do
10
+ RSperantito::Client.list
11
+ end
12
+
13
+ expect(clients.size).to eq(20)
14
+ end
15
+
16
+ it "should get clients:list by captation_ways_id" do
17
+ clients = VCR.use_cassette("clients:list:captation_way") do
18
+ RSperantito::Client.list(1, captation_ways_id: 5)
19
+ end
20
+
21
+ expect(clients[0]['captation_way']).to eq('facebook')
22
+ end
23
+ end
24
+
25
+ describe 'Client::find' do
26
+ it 'should client 587' do
27
+ client = VCR.use_cassette('clients:find:587') do
28
+ RSperantito::Client.find(587)
29
+ end
30
+
31
+ expect(client['full_name']).to eq('Carolina Gaitan')
32
+ expect(client['document']).to eq('auto-272684')
33
+ end
34
+ end
35
+
36
+ describe 'Client::search' do
37
+ it "should client by document" do
38
+ client = VCR.use_cassette('clients:search:document:auto-103328') do
39
+ RSperantito::Client.search('auto-103328')
40
+ end
41
+
42
+ expect(client['document']).to eq('auto-103328')
43
+ end
44
+ end
45
+
46
+ describe 'Client::create' do
47
+ let(:project) do
48
+ VCR.use_cassette('projects:find:3') do
49
+ RSperantito::Project.find(4)
50
+ end
51
+ end
52
+
53
+ let('info_basic') do
54
+ {
55
+ data: {
56
+ fname: 'maxi',
57
+ lname: 'sperant',
58
+ email: 'maxi@sperant.com',
59
+ project_related: project['id']
60
+ }
61
+ }
62
+ end
63
+
64
+ let('info_complete') do
65
+ {
66
+ data: {
67
+ fname: 'maxi compra',
68
+ lname: 'sperant',
69
+ email: 'maxi_compra@sperant.com',
70
+ project_related: project['id'],
71
+ main_telephone: '987654321',
72
+ country: 'PE',
73
+ departament: 'PAS',
74
+ document: '04023413',
75
+ address: 'Av. los aolvidados'
76
+ }
77
+ }
78
+ end
79
+
80
+ it "should info basic" do
81
+ client = VCR.use_cassette('client:create:info:basic') do
82
+ RSperantito::Client.create(info_basic)
83
+ end
84
+
85
+ expect(client['email']).to eq('maxi@sperant.com')
86
+ expect(client['projects_related'].map { |e| e['id'] }).to include(4)
87
+ #values return default
88
+ #error not return "Por Contactar"
89
+ # expect(client['interest_type_name']).to eq('Por Contactar')
90
+ expect(client['person_type_id']).to eq('natural')
91
+ expect(client['document_type_name']).to eq('DNI')
92
+ expect(client['input_channel_name']).to include('Contacto Web')
93
+ end
94
+
95
+ it "should info complete" do
96
+ client = VCR.use_cassette('client:create:info:complete') do
97
+ RSperantito::Client.create(info_complete)
98
+ end
99
+
100
+ expect(client['email']).to eq('maxi_compra@sperant.com')
101
+ expect(client['projects_related'].map { |e| e['id'] }).to include(4)
102
+ expect(client['main_telephone']).to include('987654321')
103
+ expect(client['ubication']['country']).to include('Peru')
104
+ #error not return value send return
105
+ # expect(client['ubication']['departament']).to include('Pasco')
106
+ expect(client['ubication']['address']).to include('Av. los aolvidados')
107
+ expect(client['document']).to include('04023413')
108
+ expect(client['phones']).to include('987654321')
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,9 @@
1
+ describe RSperantito do
2
+ describe '.client_token' do
3
+ it "should return .client_token" do
4
+ authenticate_client
5
+
6
+ expect(RSperantito.client_token).to eq(AuthenticationHelper::TOKEN)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,41 @@
1
+ describe RSperantito::Project do
2
+
3
+ describe 'Project::list' do
4
+ it "should get projects" do
5
+ projects = VCR.use_cassette('projects:list') do
6
+ RSperantito::Project.list
7
+ end
8
+
9
+ expect(projects.size).to eq 5
10
+ expect(projects.map{|e| e['name']}).to include('Villa Hermanda', 'Edificio Smart Life')
11
+ end
12
+ end
13
+
14
+ describe 'Project::find' do
15
+ it "should project" do
16
+ project = VCR.use_cassette('projects:find:3') do
17
+ RSperantito::Project.find(4)
18
+ end
19
+
20
+ expect(project['code']).to eq('VIHE')
21
+ end
22
+ end
23
+
24
+ describe 'Project::search' do
25
+ it "should project search by code" do
26
+ project = VCR.use_cassette('projects:search:SMART') do
27
+ RSperantito::Project.search('SMART')
28
+ end
29
+
30
+ expect(project['code']).to eq('SMART')
31
+ end
32
+
33
+ it "should project search by name" do
34
+ project = VCR.use_cassette('projects:search:Villa_Hermanda') do
35
+ RSperantito::Project.search('Villa Hermanda')
36
+ end
37
+
38
+ expect(project['name']).to eq('Villa Hermanda')
39
+ end
40
+ end
41
+ end