contaazul 0.1.0dev → 0.1.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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZWM5YWE0NjhhYWM1OThkMTM5OTM2MzNiYjQ2NzA2NzlhZWZhZDMxYw==
4
+ MDFjMThlMDJlY2ZjMTBhZGI3MTkzY2JjODRmNmE3NzAxNjY0NmZiZg==
5
5
  data.tar.gz: !binary |-
6
- NDFmODRiODhjNWI0MzMwMjNmYmNmMGE3MWRkNWE4ZWU5YjhjMTQ5Yg==
6
+ ZGU0YzU1ZTdhYzE0NjJhZjg3OTUyNWI4NzQ1NmZkNmYwMDEwOGQ4ZA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- OTEwMzJlM2E2Y2VkY2ZlMmEzOTdmM2U3MWE4YmM5ZDYwNDBmOTdkMmQ5OGU1
10
- MmVjZDlhM2I1MWFiNzI0NTYxNGY4N2M2MWY2MGRhM2FlY2ZhMjI5MDU2OTgx
11
- MGIyM2U2MzM5NWY2ZDcyZjAyNTBjMzUyMDA3YjAwYzUxZDhkMGM=
9
+ ZDNmM2VjOTZmY2ZhMWRlNGNkN2FkZTBjMTcxOTlmNGMwYzk2YmQ4ZmY1YzJl
10
+ ZDJiNzg3MDllYTE5OTA5MWMwOWQzMDFiNDQxMWE2NTU5NGE5NmE2MTZkYWIy
11
+ MDE3ZjZmNWRlMTlmMWU4NWNiZmYxMWRlNTZiZTY2OWY5ODRjM2E=
12
12
  data.tar.gz: !binary |-
13
- NDQzM2JkYjgyZmRkYTZhMTZmMTMzZTg5MWM2YjFlZWY3MWZhYzM4OGZiNTJi
14
- YjAwYWNiY2M3NWE5YTBkYTBlZGIwZDE1NThiOGQ2ODBiZDYyZDEwYTFlZWZh
15
- ZDYwYWE1YjJlMzc0NDA4ODhmNjk1OGEzOGRmNTJkNGRmZDMxMzM=
13
+ Y2Q5YWMwOGE3ZjEyMGJkZmJiNjhjMDdjMWQ2OTdkZDdiMTdjNjU2MjA5MWVk
14
+ YTI4OGZhNGI3OTk3YzE5ZjQyYTBkNzYyZTQ0ZGI2MDFhMmEwNjc0MDA4ZTJm
15
+ Y2Y5N2Y4ZmFiOGIwMDRiOTc0ZjNhZGNmZWNmMzVlOGJmNTZjYmU=
@@ -1,9 +1,7 @@
1
1
  language: ruby
2
2
  bundler_args: --without development
3
3
  rvm:
4
- - jruby-18mode # JRuby in 1.8 mode
5
4
  - jruby-19mode # JRuby in 1.9 mode
6
- - 1.8.7
7
5
  - 1.9.2
8
6
  - 1.9.3
9
7
  - 2.0.0
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # contaazul
2
- [![Dependency Status](https://gemnasium.com/jnettome/contaazul.png)](https://gemnasium.com/jnettome/contaazul) [![Build Status](https://travis-ci.org/jnettome/contaazul.png?branch=master)](https://travis-ci.org/jnettome/contaazul)
2
+ [![Dependency Status](https://gemnasium.com/ateliware/contaazul.png)](https://gemnasium.com/ateliware/contaazul) [![Build Status](https://travis-ci.org/ateliware/contaazul.png?branch=master)](https://travis-ci.org/ateliware/contaazul)
3
3
 
4
4
  A tiny *non-official* wrapper for [ContaAzul](http://contaazul.com.br)'s API.
5
5
 
@@ -39,6 +39,7 @@ dependency. Please require it explicitly if you're running Ruby 1.8
39
39
  4. Push to the branch (`git push origin my-new-feature`)
40
40
  5. Create new Pull Request
41
41
 
42
+
42
43
  ## Copyright
43
44
 
44
45
  DO WHATEVER YOU WANT TO!
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
 
24
24
  spec.add_dependency 'addressable', '~> 2.3.4'
25
25
  spec.add_dependency 'faraday', '~> 0.8.7'
26
+ spec.add_dependency 'faraday_middleware', '~> 0.8'
26
27
  spec.add_dependency 'multi_json', '~> 1.7.6'
27
28
 
28
29
  spec.required_rubygems_version = '>= 1.8'
@@ -1,30 +1,11 @@
1
1
  module Contaazul
2
2
  module Authentication
3
- def authentication
4
- if login && password
5
- {:login => login, :password => password}
6
- else
7
- {}
8
- end
9
- end
10
-
11
3
  def authenticated?
12
- !authentication.empty?
4
+ !company_token.empty?
13
5
  end
14
6
 
15
7
  def oauthed?
16
8
  !oauth_token.nil?
17
9
  end
18
-
19
- def unauthed_rate_limited?
20
- client_id && client_secret
21
- end
22
-
23
- def unauthed_rate_limit_params
24
- {
25
- :client_id => client_id,
26
- :client_secret => client_secret
27
- }
28
- end
29
10
  end
30
11
  end
@@ -1,9 +1,6 @@
1
1
  require 'contaazul/authentication'
2
- # require 'contaazul/connection'
3
- # require 'contaazul/repository'
4
- # require 'contaazul/request'
5
-
6
- # require 'contaazul/client/github'
2
+ require 'contaazul/connection'
3
+ require 'contaazul/request'
7
4
 
8
5
  module Contaazul
9
6
  class Client
@@ -15,12 +12,70 @@ module Contaazul
15
12
  send("#{key}=", options[key])
16
13
  end
17
14
 
15
+ Contaazul.company_token = options[:company_token] if options[:company_token]
18
16
  end
19
17
 
20
18
  include Contaazul::Authentication
21
- # include Contaazul::Connection
22
- # include Contaazul::Request
19
+ include Contaazul::Connection
20
+ include Contaazul::Request
21
+
22
+ def to_s
23
+ return create_link unless false #authenticated?
24
+ super
25
+ end
26
+
27
+ def create_link
28
+ response = Contaazul.get("pub/oauth/requestkey/#{self.external_token}")
29
+
30
+ "https://app.contaazul.com.br/authorization/request/#{response}?redirectTo=#{self.return_url}"
31
+ end
32
+
33
+ # Get all clients
34
+ #
35
+ # @return [Array(Hash)] Clients information
36
+ def clients
37
+ records = Contaazul.get("pub/contact/customer")
38
+ records
39
+ end
23
40
 
24
- # include Contaazul::Client::GitHub
41
+ # Get a single client
42
+ #
43
+ # @param id [String] ID of client to fetch
44
+ # @return [Hash::Mash] Client information
45
+ def client(id, options={})
46
+ record = Contaazul.get("pub/contact/customer/#{id}")
47
+ record
48
+ end
49
+
50
+ # Create a client
51
+ #
52
+ # @param options [Hash] client information.
53
+ # @option options [String] :description
54
+ # @return [Hashie::Mash] Newly created client info
55
+ def create_client(options={})
56
+ record = Contaazul.post("pub/contact/customer", options)
57
+ record
58
+ end
59
+
60
+ # Edit a client
61
+ #
62
+ # @param id [String] Client ID
63
+ # @param options [Hash] Client information.
64
+ # @option options [String] :description
65
+ # @return
66
+ # [Hashie::Mash] Newly updated client info
67
+ def edit_client(id, options={})
68
+ record = Contaazul.patch("pub/contact/customer/#{id}", options)
69
+ record
70
+ end
71
+
72
+ # Delete a client
73
+ #
74
+ # @param id [String] Client ID
75
+ # @return [Boolean] Indicating success of deletion
76
+ def delete_client(id, options={})
77
+ record = Contaazul.delete("pub/contact/customer/#{id}")
78
+ record
79
+ end
25
80
  end
26
81
  end
@@ -8,22 +8,24 @@ module Contaazul
8
8
  :faraday_config_block,
9
9
  :api_version,
10
10
  :api_endpoint,
11
- :login,
12
- :password,
13
11
  :proxy,
14
12
  :oauth_token,
13
+ :company_token,
14
+ :external_token,
15
15
  :client_id,
16
16
  :client_secret,
17
17
  :user_agent,
18
18
  :request_host,
19
- :netrc,
20
- :auto_traversal,
19
+ :return_url,
21
20
  :per_page].freeze
22
21
 
23
22
  DEFAULT_ADAPTER = Faraday.default_adapter
24
- DEFAULT_API_VERSION = 3
25
- DEFAULT_API_ENDPOINT = ENV['CONTAAZUL_API_ENDPOINT'] || 'http://api.contaazul.com.br/'
23
+ DEFAULT_API_VERSION = 1
24
+ DEFAULT_API_ENDPOINT = ENV['CONTAAZUL_API_ENDPOINT'] || 'https://api.contaazul.com.br/'
25
+ DEFAULT_COMPANY_TOKEN = ENV['CONTAAZUL_API_COMPANY_TOKEN']
26
+ DEFAULT_EXTERNAL_TOKEN = ENV['CONTAAZUL_API_EXTERNAL_TOKEN'] || "0000013f-1fa8-cd09-0000-004000000000"
26
27
  DEFAULT_USER_AGENT = "Contaazul Non-Official Ruby Gem #{Contaazul::VERSION}".freeze
28
+ DEFAULT_RETURN_URL = ENV['CONTAAZUL_API_RETURN_URL'] || "/"
27
29
  DEFAULT_AUTO_TRAVERSAL = false
28
30
 
29
31
  attr_accessor(*VALID_OPTIONS_KEYS)
@@ -44,6 +46,18 @@ module Contaazul
44
46
  @api_endpoint = File.join(value, "")
45
47
  end
46
48
 
49
+ def company_token=(value)
50
+ @company_token = value
51
+ end
52
+
53
+ def return_url=(value)
54
+ @return_url = value
55
+ end
56
+
57
+ def external_token=(value)
58
+ @external_token = value
59
+ end
60
+
47
61
  def faraday_config(&block)
48
62
  @faraday_config_block = block
49
63
  end
@@ -52,16 +66,15 @@ module Contaazul
52
66
  self.adapter = DEFAULT_ADAPTER
53
67
  self.api_version = DEFAULT_API_VERSION
54
68
  self.api_endpoint = DEFAULT_API_ENDPOINT
55
- self.login = nil
56
- self.password = nil
69
+ self.company_token = DEFAULT_COMPANY_TOKEN
70
+ self.external_token = DEFAULT_EXTERNAL_TOKEN
57
71
  self.proxy = nil
58
72
  self.oauth_token = nil
59
73
  self.client_id = nil
60
74
  self.client_secret = nil
61
75
  self.request_host = nil
62
- self.netrc = false
63
76
  self.user_agent = DEFAULT_USER_AGENT
64
- self.auto_traversal = DEFAULT_AUTO_TRAVERSAL
77
+ self.return_url = DEFAULT_RETURN_URL
65
78
  end
66
79
  end
67
80
  end
@@ -0,0 +1,41 @@
1
+ require 'faraday_middleware'
2
+
3
+ module Contaazul
4
+ # @private
5
+ module Connection
6
+ private
7
+
8
+ def connection(options={})
9
+ options = {
10
+ :authenticate => true,
11
+ :force_urlencoded => false,
12
+ :raw => false
13
+ }.merge(options)
14
+
15
+ if !proxy.nil?
16
+ options.merge!(:proxy => proxy)
17
+ end
18
+
19
+ connection = Faraday.new(options) do |builder|
20
+
21
+ if options[:force_urlencoded]
22
+ builder.request :url_encoded
23
+ else
24
+ builder.request :json
25
+ end
26
+
27
+ builder.use FaradayMiddleware::FollowRedirects
28
+
29
+ builder.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
30
+
31
+ faraday_config_block.call(builder) if faraday_config_block
32
+
33
+ builder.adapter *adapter
34
+ end
35
+
36
+ connection.headers[:user_agent] = user_agent
37
+
38
+ connection
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,73 @@
1
+ require 'multi_json'
2
+
3
+ module Contaazul
4
+ module Request
5
+ def get(path, options={})
6
+ conn = Faraday.new(:url => self.api_endpoint) do |faraday|
7
+ faraday.adapter Faraday.default_adapter
8
+ end
9
+ conn.headers["CompanyToken"] = self.company_token || "";
10
+ conn.headers["ExternalApplicationToken"] = self.external_token || "";
11
+
12
+ puts ""
13
+ puts "[ContaAzul] Endpoint/Path: #{self.api_endpoint}/#{path}"
14
+ puts "[ContaAzul] CompanyToken: #{self.company_token}"
15
+ puts "[ContaAzul] External: #{self.external_token}"
16
+ puts ""
17
+
18
+ response = conn.get(path)
19
+ response.body
20
+ end
21
+
22
+ def patch(path, options={})
23
+ conn = Faraday.new(:url => self.api_endpoint) do |faraday|
24
+ faraday.adapter Faraday.default_adapter
25
+ end
26
+ conn.headers["CompanyToken"] = self.company_token || "";
27
+ conn.headers["ExternalApplicationToken"] = self.external_token || "";
28
+
29
+ puts ""
30
+ puts "[ContaAzul] Endpoint/Path: #{self.api_endpoint}/#{path}"
31
+ puts "[ContaAzul] CompanyToken: #{self.company_token}"
32
+ puts "[ContaAzul] External: #{self.external_token}"
33
+ puts ""
34
+
35
+ response = conn.patch(path)
36
+ response.body
37
+ end
38
+
39
+ def post(path, options={})
40
+ conn = Faraday.new(:url => self.api_endpoint) do |faraday|
41
+ faraday.adapter Faraday.default_adapter
42
+ end
43
+ conn.headers["CompanyToken"] = self.company_token || "";
44
+ conn.headers["ExternalApplicationToken"] = self.external_token || "";
45
+
46
+ puts ""
47
+ puts "[ContaAzul] Endpoint/Path: #{self.api_endpoint}/#{path}"
48
+ puts "[ContaAzul] CompanyToken: #{self.company_token}"
49
+ puts "[ContaAzul] External: #{self.external_token}"
50
+ puts ""
51
+
52
+ response = conn.post(path, options)
53
+ response.body
54
+ end
55
+
56
+ def delete(path, options={})
57
+ conn = Faraday.new(:url => self.api_endpoint) do |faraday|
58
+ faraday.adapter Faraday.default_adapter
59
+ end
60
+ conn.headers["CompanyToken"] = self.company_token || "";
61
+ conn.headers["ExternalApplicationToken"] = self.external_token || "";
62
+
63
+ puts ""
64
+ puts "[ContaAzul] Endpoint/Path: #{self.api_endpoint}/#{path}"
65
+ puts "[ContaAzul] CompanyToken: #{self.company_token}"
66
+ puts "[ContaAzul] External: #{self.external_token}"
67
+ puts ""
68
+
69
+ response = conn.delete(path)
70
+ response.body
71
+ end
72
+ end
73
+ end
@@ -1,3 +1,3 @@
1
1
  module Contaazul
2
- VERSION = "0.1.0dev"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,62 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'json'
4
+
5
+ describe Contaazul::Client do
6
+
7
+ before do
8
+ Contaazul.reset
9
+ end
10
+
11
+ describe "api_endpoint" do
12
+
13
+ after(:each) do
14
+ Contaazul.reset
15
+ end
16
+
17
+ it "defaults to https://api.contaazul.com.br/" do
18
+ client = Contaazul::Client.new
19
+ expect(client.api_endpoint).to eq('https://api.contaazul.com.br/')
20
+ end
21
+
22
+ it "is set" do
23
+ Contaazul.api_endpoint = 'http://lvh.me/'
24
+ client = Contaazul::Client.new
25
+ expect(client.api_endpoint).to eq('http://lvh.me/')
26
+ end
27
+
28
+ end
29
+
30
+ context "when working with oauth tokens" do
31
+
32
+ before(:each) do
33
+ Contaazul.reset
34
+ @client = Contaazul::Client.new
35
+ end
36
+
37
+ # it "should request and return a public token" do
38
+ # stub_request(:get, "#{Contaazul.api_endpoint}oauth/requestkey/#{Contaazul.external_token}").
39
+ # with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Contaazul Non-Official Ruby Gem 0.1.0dev'}).
40
+ # to_return(:status => 200, :body => '{ "publicToken" : "1f74b13f1e5c6c69cb5d7fbaabb1e2cb" }', :headers => {
41
+ # :content_type => "application/json; charset=utf-8"
42
+ # })
43
+
44
+ # response = Contaazul.get("oauth/requestkey/#{Contaazul.external_token}")
45
+ # expect(response["publicToken"]).to eq("1f74b13f1e5c6c69cb5d7fbaabb1e2cb")
46
+ # end
47
+
48
+ # it "should get a valid oauth2 instance" do
49
+ # stub_request(:get, "#{Contaazul.api_endpoint}oauth/requestkey/#{Contaazul.external_token}").
50
+ # with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Contaazul Non-Official Ruby Gem 0.1.0dev'}).
51
+ # to_return(:status => 200, :body => '{ "publicToken" : "1f74b13f1e5c6c69cb5d7fbaabb1e2cb" }', :headers => {
52
+ # :content_type => "application/json; charset=utf-8"
53
+ # })
54
+
55
+ # response = Contaazul.get "oauth/requestkey/#{Contaazul.external_token}"
56
+ # # @client.to_s
57
+ # # client = Contaazul::Client.new(:oauth_token => response)
58
+ # end
59
+
60
+ end
61
+
62
+ end
@@ -0,0 +1,3 @@
1
+ {
2
+ "publicToken" : "1f74b13f1e5c6c69cb5d7fbaabb1e2cb"
3
+ }
@@ -74,9 +74,7 @@ end
74
74
  def contaazul_url(url)
75
75
  if url =~ /^http/
76
76
  url
77
- elsif @client && @client.authenticated?
78
- "http://api.contaazul.com.br/pub#{url}"
79
77
  else
80
- "http://api.contaazul.com.br#{url}"
78
+ "http://api.contaazul.com.br/pub/#{url}"
81
79
  end
82
80
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contaazul
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0dev
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Netto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-06 00:00:00.000000000 Z
11
+ date: 2013-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.8.7
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday_middleware
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '0.8'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '0.8'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: multi_json
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -98,9 +112,13 @@ files:
98
112
  - lib/contaazul/authentication.rb
99
113
  - lib/contaazul/client.rb
100
114
  - lib/contaazul/configuration.rb
115
+ - lib/contaazul/connection.rb
101
116
  - lib/contaazul/error.rb
117
+ - lib/contaazul/request.rb
102
118
  - lib/contaazul/version.rb
119
+ - spec/contaazul/client_spec.rb
103
120
  - spec/contaazul_spec.rb
121
+ - spec/fixtures/requestkey.json
104
122
  - spec/spec_helper.rb
105
123
  homepage: https://github.com/jnettome/contaazul
106
124
  licenses:
@@ -127,6 +145,7 @@ signing_key:
127
145
  specification_version: 4
128
146
  summary: Integrate your ruby app with ContaAzul's API (contaazul.com.br)
129
147
  test_files:
148
+ - spec/contaazul/client_spec.rb
130
149
  - spec/contaazul_spec.rb
150
+ - spec/fixtures/requestkey.json
131
151
  - spec/spec_helper.rb
132
- has_rdoc: