incentivale-sdk-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9830befd921edbdb4fe46511326c992d8844e9b4cf7aac93f4e498736ee20726
4
+ data.tar.gz: f4f758abfcac2f00dd30b5193e0ef502521215de76629cf9c8465cad32fec5b2
5
+ SHA512:
6
+ metadata.gz: 14bd2827633f38ce67fc74c82f333488faaa4ca6e32eeaee1c1a4e110584dc73876d58cf8842ce978ecbabc83891dd575a1ebd36434c887b95755f1428e64b0e
7
+ data.tar.gz: 60054b985c93ec1665495914577ff0e6680bd1c160932debdc5018452d94e689dbe0f5082aa22bf5fd0043b1ed3928cc9892d3fd8ccd60603980ea7c56d4910f
@@ -0,0 +1,41 @@
1
+ require 'time'
2
+ require 'json'
3
+ require 'faraday'
4
+ require 'awrence'
5
+ require 'plissken'
6
+ require 'forwardable'
7
+ require 'incentivale/api'
8
+ require 'incentivale/auth'
9
+ require 'incentivale/token'
10
+ require 'incentivale/client'
11
+ require 'incentivale/awrence'
12
+ require 'incentivale/request'
13
+ require 'incentivale/version'
14
+ require 'incentivale/response'
15
+ require 'recursive-open-struct'
16
+ require 'incentivale/configuration'
17
+ require 'incentivale/resources/resource'
18
+ require 'incentivale/resources/egift'
19
+ require 'incentivale/resources/product'
20
+ require 'incentivale/resources/tracking'
21
+ require 'incentivale/resources/redemption'
22
+
23
+ module Incentivale
24
+ class << self
25
+ extend Forwardable
26
+ attr_accessor :configuration
27
+
28
+ def configure
29
+ self.configuration ||= Configuration.new
30
+ yield(configuration)
31
+ build_api
32
+ end
33
+
34
+ def build_api
35
+ token = Token.new
36
+ @api = Api.new(Client.new(token))
37
+ end
38
+
39
+ def_delegators :@api, :products, :redemption, :tracking, :egift
40
+ end
41
+ end
@@ -0,0 +1,25 @@
1
+ module Incentivale
2
+ class Api
3
+ attr_reader :client
4
+
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ def products
10
+ Incentivale::Resources::Product.new(client)
11
+ end
12
+
13
+ def redemption
14
+ Incentivale::Resources::Redemption.new(client)
15
+ end
16
+
17
+ def tracking
18
+ Incentivale::Resources::Tracking.new(client)
19
+ end
20
+
21
+ def egift
22
+ Incentivale::Resources::Egift.new(client)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ module Incentivale
2
+ class Auth
3
+ attr_accessor :client
4
+
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ def header
10
+ "Bearer #{client.current_token.access_token}"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Incentivale
2
+ Awrence.acronyms = { 'cpf' => 'CPF_', 'cnpj' => 'CNPJ', 'cep' => 'CEP', 'sku' => 'SKU' }
3
+ end
@@ -0,0 +1,38 @@
1
+ module Incentivale
2
+ class Client
3
+ HOST = 'https://central-incentivale.herokuapp.com' # FIXME 'https://central.incentivale.com.br'
4
+
5
+ attr_accessor :token, :auth
6
+
7
+ def initialize(token)
8
+ @token = token
9
+ @auth = Auth.new(self)
10
+ end
11
+
12
+ def get(path, params={})
13
+ request.get path, params.merge(campaign_token)
14
+ end
15
+
16
+ def post(path, resource={})
17
+ request.post path, resource.merge(campaign_token)
18
+ end
19
+
20
+ def current_token
21
+ @token.expired? ? new_token : token
22
+ end
23
+
24
+ private
25
+
26
+ def request
27
+ Request.new(self)
28
+ end
29
+
30
+ def campaign_token
31
+ @campaign_token ||= { token: Incentivale.configuration.campaign }
32
+ end
33
+
34
+ def new_token
35
+ self.token = token.refresh
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,11 @@
1
+ module Incentivale
2
+ class Configuration
3
+ attr_accessor :username
4
+ attr_accessor :password
5
+ attr_accessor :campaign
6
+
7
+ def inspect
8
+ "#<#{self.class.name}:#{object_id}>"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,38 @@
1
+ module Incentivale
2
+ class Request
3
+ API_ENDPOINT = '/api/v3'
4
+
5
+ attr_accessor :connection
6
+
7
+ def initialize(client)
8
+ @connection = Faraday.new(url: Client::HOST) do |conn|
9
+ conn.token_auth(client.auth.header)
10
+ conn.headers['Content-Type'] = 'application/x-www-form-urlencoded'
11
+ conn.request :url_encoded
12
+ conn.adapter Faraday.default_adapter
13
+ end
14
+ end
15
+
16
+ def get(url, params = {})
17
+ Response.new connection.get(request_path(url), params.to_camel_keys)
18
+ end
19
+
20
+ def post(url, resource = {})
21
+ Response.new connection.post(request_path(url), resource_to_post(resource))
22
+ end
23
+
24
+ private
25
+
26
+ def resource_to_post(resource)
27
+ URI.encode_www_form(serialize_resource_keys(resource))
28
+ end
29
+
30
+ def serialize_resource_keys(hash)
31
+ hash.to_snake_keys.to_camel_keys.map { |k, v| [k.to_s,v] }
32
+ end
33
+
34
+ def request_path(url)
35
+ API_ENDPOINT + url
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,16 @@
1
+ module Incentivale
2
+ module Resources
3
+ class Egift < Resource
4
+ ENDPOINTS = { availability: '/availabilitystockegift', create: '/transactionegift' }
5
+
6
+ def available?(sku, amount_premium = 1)
7
+ resp = client.get(ENDPOINTS[:availability], { sku: sku, amount_premium: amount_premium })
8
+ resp.availability
9
+ end
10
+
11
+ def create(resource)
12
+ client.post(ENDPOINTS[:create], resource)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ module Incentivale
2
+ module Resources
3
+ class Product < Resource
4
+ ENDPOINTS = { index: '/products' }
5
+
6
+ def all
7
+ response = client.get(ENDPOINTS[:index])
8
+ response.success ? response.products : response
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module Incentivale
2
+ module Resources
3
+ class Redemption < Resource
4
+ ENDPOINTS = { create: '/addresquest', find: '/existrequest' }
5
+
6
+ def create(resource)
7
+ client.post(ENDPOINTS[:create], resource)
8
+ end
9
+
10
+ def find(cod_request)
11
+ client.get(ENDPOINTS[:find], { cod_request: cod_request })
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module Incentivale
2
+ module Resources
3
+ class Resource
4
+ attr_reader :client
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Incentivale
2
+ module Resources
3
+ class Tracking < Resource
4
+ ENDPOINTS = { find: '/findtracking' }
5
+
6
+ def find(cod_request)
7
+ client.get(ENDPOINTS[:find], { cod_request: cod_request })
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module Incentivale
2
+ class Response < SimpleDelegator
3
+ def initialize(response)
4
+ @response = response
5
+ if json.is_a?(Hash)
6
+ super(RecursiveOpenStruct.new(json, recurse_over_arrays: true))
7
+ else
8
+ super(@response.body)
9
+ end
10
+ @response
11
+ end
12
+
13
+ def json
14
+ @json ||= JSON.parse(@response.body).to_snake_keys
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,44 @@
1
+ module Incentivale
2
+ class Token
3
+ attr_accessor :valid_token
4
+
5
+ def initialize
6
+ @valid_token = generate_new_token
7
+ end
8
+
9
+ def inspect
10
+ "#<#{self.class.name}:#{object_id}>"
11
+ end
12
+
13
+ def expires_at
14
+ Time.parse(valid_token['.expires'])
15
+ end
16
+
17
+ def access_token
18
+ valid_token['access_token']
19
+ end
20
+
21
+ def refresh
22
+ self.valid_token = generate_new_token
23
+ self
24
+ end
25
+
26
+ def expired?
27
+ (Time.now.utc - expires_at) >= 3600
28
+ end
29
+
30
+ private
31
+
32
+ def token_host
33
+ Client::HOST + '/oauth/token'
34
+ end
35
+
36
+ def generate_new_token
37
+ Response.new Faraday.new.post(token_host, {
38
+ grant_type: :password,
39
+ username: Incentivale.configuration.username,
40
+ password: Incentivale.configuration.password
41
+ })
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Incentivale
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: incentivale-sdk-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Leonardo Santos
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: json
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: recursive-open-struct
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: plissken
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: awrence
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
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.16'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.16'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ description:
112
+ email:
113
+ - aleotory@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - lib/incentivale.rb
119
+ - lib/incentivale/api.rb
120
+ - lib/incentivale/auth.rb
121
+ - lib/incentivale/awrence.rb
122
+ - lib/incentivale/client.rb
123
+ - lib/incentivale/configuration.rb
124
+ - lib/incentivale/request.rb
125
+ - lib/incentivale/resources/egift.rb
126
+ - lib/incentivale/resources/product.rb
127
+ - lib/incentivale/resources/redemption.rb
128
+ - lib/incentivale/resources/resource.rb
129
+ - lib/incentivale/resources/tracking.rb
130
+ - lib/incentivale/response.rb
131
+ - lib/incentivale/token.rb
132
+ - lib/incentivale/version.rb
133
+ homepage: ''
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.7.6
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Gem to wrap incentivale.com.br API v3
157
+ test_files: []