restfull_oauth 0.1.0 → 0.2.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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d07bc8ce524b6ff845ca7d4a2c14764df810a69f
4
- data.tar.gz: b370b311fe27646786a0f3d39c3e3d841d051221
3
+ metadata.gz: adff2d769488a9b7c5d78441db5a39a6ca03e0b5
4
+ data.tar.gz: df0c13f39fe659544008d71bdc22b088fb6307e9
5
5
  SHA512:
6
- metadata.gz: 7bd1e9af73ba16c2090379a18308227a513f12240f13af1ecbf07c7064cc77a54172fa40a5f0a9feb748cc3ae170793858fa1a680c0960db3c84ceceed750f73
7
- data.tar.gz: 85010fc80385afd99a30210c6b1251c93a1fe2b140b501c14ff456026de53cc21ce247ecb99f851e9ab5f4c8cd0db6a4b0dc662206c78fbfa94f375d622b87ba
6
+ metadata.gz: 0a3b23e46e9d8c1dd363052b00a92354ef818c9863d18e21b7816def9d47e5ec5bcf860d8eeda50853cbaf0a036572acf3528df3f1c205ffd7a79321cb443ab3
7
+ data.tar.gz: c9310088344850749e4c4fe13be33cb2fbd0430435c4297bd1c67ad5294b8564aded8e15e8ae622509a1133d229dc15770ed022436cf0179ce7e5b767abace91
data/README.md CHANGED
@@ -30,19 +30,23 @@ GET request (get product information):
30
30
 
31
31
  ```irb
32
32
  % irb
33
- >> keys = {"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"}
34
- >> json_response = RestfullOauth::connect('GET','https://your server/rest/api', keys)
33
+ >> require 'restfull_oauth'
34
+ >> service = RestfullOauth::Connection.new({"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"})
35
+ >> response = service.connect('GET','https://your server/rest/api', keys)
35
36
  => #<Net::HTTPOK 200 OK readbody=true>
37
+ >> puts JSON.parse(response.body).to_yaml
36
38
  ```
37
39
 
38
40
  POST request (assign product to website):
39
41
 
40
42
  ```irb
41
43
  % irb
42
- >> keys = {"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"}
43
- >> post_data = {'website_id' => 1 }.to_json
44
- >> json_response = RestfullOauth::connect('POST','https://your server/rest/api', keys, post_data)
44
+ >> require 'restfull_oauth'
45
+ >> service = RestfullOauth::Connection.new({"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"})
46
+ >> post_data = {'foo' => 'bar' }.to_json
47
+ >> response = RestfullOauth::connect('POST','https://your server/rest/api', post_data)
45
48
  => #<Net::HTTPOK 200 OK readbody=true>
49
+ >> puts JSON.parse(response.body).to_yaml
46
50
  ```
47
51
 
48
52
  PUT requests work in the same way as POST requests
@@ -1,3 +1,3 @@
1
1
  module RestfullOauth
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -1,4 +1,4 @@
1
- require "restfull_oauth/version"
1
+ require 'restfull_oauth/version'
2
2
  require 'oauth'
3
3
  require 'uri'
4
4
  require 'cgi'
@@ -16,79 +16,101 @@ module RestfullOauth
16
16
  # % authenticate
17
17
  #
18
18
  # Example:
19
- # % irb
20
- # >> keys = {"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"}
21
- # >> json_response = RestfullOauth::connect('GET','https://your domain/restfull/api/', keys)
22
- # => #<Net::HTTPOK 200 OK readbody=true>
23
- # >> post_data = { "test" => "test" }.to_json
24
- # >> json_response = RestfullOauth::connect('POST','https://your domain/restfull/api/', keys, post_data)
25
- # => #<Net::HTTPOK 200 OK readbody=true>
19
+ #
20
+ #% irb
21
+ # >> require 'restfull_oauth'
22
+ # >> service = RestfullOauth::Connection.new({"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"})
23
+ # >> post_data = {'foo' => 'bar' }.to_json
24
+ # >> response = RestfullOauth::connect('POST','https://your server/rest/api', post_data)
25
+ # => #<Net::HTTPOK 200 OK readbody=true>
26
+ # >> puts JSON.parse(response.body).to_yaml
26
27
 
27
- def self.generate_nonce(size=6)
28
- Base64.encode64(OpenSSL::Random.random_bytes(size)).gsub(/\W/, '')
29
- end
28
+ class Connection
29
+ attr_accessor :session, :config, :logger
30
30
 
31
- def self.url_encode(string)
32
- CGI::escape(string)
33
- end
31
+ def initialize(config = {})
32
+ @config = config
33
+ self
34
+ end
34
35
 
35
- def self.params(consumer_key, token)
36
- params = {
37
- 'oauth_consumer_key' => consumer_key,
38
- 'oauth_nonce' => generate_nonce,
39
- 'oauth_signature_method' => 'HMAC-SHA1',
40
- 'oauth_token' => token,
41
- 'oauth_version' => '1.0',
42
- 'oauth_timestamp' => Time.now.to_i.to_s
43
- }
44
- end
36
+ def connect(method, uri, post_data=nil)
37
+ params = params(@config['consumer_key'], @config['token'] )
38
+ signature_base_string = signature_base_string(method, uri.to_s, params)
39
+ signing_key = @config['consumer_secret'] + '&' + @config['token_secret']
40
+ params['oauth_signature'] = url_encode(sign(signing_key, signature_base_string))
41
+ header_string = create_header(params)
42
+ json_response = request_data(header_string, uri, method, post_data)
43
+ end
45
44
 
46
- def self.signature_base_string(method, uri, params)
47
- encoded_params = url_encode("oauth_consumer_key=#{params['oauth_consumer_key']}&oauth_nonce=#{params['oauth_nonce']}&oauth_signature_method=#{params['oauth_signature_method']}&oauth_timestamp=#{params['oauth_timestamp']}&oauth_token=#{params['oauth_token']}&oauth_version=#{params['oauth_version']}")
48
- encoded = method + '&' + url_encode(uri) + '&' + encoded_params
49
- end
45
+ private
50
46
 
51
- def self.sign(key, base_string)
52
- digest = OpenSSL::Digest.new('sha1')
53
- hmac = OpenSSL::HMAC.digest(digest, key, base_string)
54
- Base64.encode64(hmac).chomp.gsub(/\n/, '')
55
- end
47
+ def generate_nonce(size=6)
48
+ Base64.encode64(OpenSSL::Random.random_bytes(size)).gsub(/\W/, '')
49
+ end
56
50
 
57
- def self.create_header(params)
58
- header = "OAuth "
59
- params.each do |k, v|
60
- header += "#{k}=\"#{v}\","
51
+ def url_encode(string)
52
+ CGI::escape(string)
61
53
  end
62
- header.slice(0..-2)
63
- end
64
54
 
65
- def self.request_data(header, uri, method, post_data=nil)
66
- uri = URI(uri)
67
- http = Net::HTTP.new(uri.host, uri.port)
68
- http.use_ssl = true
69
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
70
- if method == 'POST'
71
- resp, data = http.post(uri.path, post_data, { 'Authorization' => header, "Content-Type" => "application/json; charset=utf-8" })
72
- elsif method == 'PUT'
73
- request = Net::HTTP::Put.new(uri.path)
74
- headers = { 'Authorization' => header, "Content-Type" => "application/json" }
75
- headers.keys.each do |key|
76
- request[key] = headers[key]
55
+ def params(consumer_key, token)
56
+ params = {'oauth_consumer_key' => consumer_key,
57
+ 'oauth_nonce' => generate_nonce,
58
+ 'oauth_signature_method' => 'HMAC-SHA1',
59
+ 'oauth_token' => token,
60
+ 'oauth_version' => '1.0',
61
+ 'oauth_timestamp' => Time.now.to_i.to_s
62
+ }
63
+ end
64
+
65
+ def signature_base_string(method, uri, params)
66
+ encoded_params = url_encode("oauth_consumer_key=#{params['oauth_consumer_key']}&oauth_nonce=#{params['oauth_nonce']}&oauth_signature_method=#{params['oauth_signature_method']}&oauth_timestamp=#{params['oauth_timestamp']}&oauth_token=#{params['oauth_token']}&oauth_version=#{params['oauth_version']}")
67
+ encoded = method + '&' + url_encode(uri) + '&' + encoded_params
68
+ end
69
+
70
+ def sign(key, base_string)
71
+ digest = OpenSSL::Digest.new('sha1')
72
+ hmac = OpenSSL::HMAC.digest(digest, key, base_string)
73
+ Base64.encode64(hmac).chomp.gsub(/\n/, '')
74
+ end
75
+
76
+ def create_header(params)
77
+ header = "OAuth "
78
+ params.each do |k, v|
79
+ header += "#{k}=\"#{v}\","
77
80
  end
78
- request.body = post_data
79
- resp, data = http.request(request)
80
- else
81
- resp, data = http.get(uri.path, { 'Authorization' => header, "Content-Type" => "application/json; charset=utf-8" })
81
+ header.slice(0..-2)
82
82
  end
83
- resp
84
- end
85
83
 
86
- def self.connect(method, uri, keys, post_data=nil)
87
- params = params(keys['consumer_key'], keys['token'] )
88
- signature_base_string = signature_base_string(method, uri.to_s, params)
89
- signing_key = keys['consumer_secret'] + '&' + keys['token_secret']
90
- params['oauth_signature'] = url_encode(sign(signing_key, signature_base_string))
91
- header_string = create_header(params)
92
- json_response = request_data(header_string, uri, method, post_data)
84
+ def request_data(header, uri, method, post_data=nil)
85
+ uri = URI(uri)
86
+ http = Net::HTTP.new(uri.host, uri.port)
87
+ if (uri.port == 443)
88
+ http.use_ssl = true
89
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
90
+ end
91
+ if method == 'POST'
92
+ resp, data = http.post(uri.path,
93
+ post_data,
94
+ { 'Authorization' => header,
95
+ 'Content-Type' => 'application/json; charset=utf-8'
96
+ })
97
+ elsif method == 'PUT'
98
+ request = Net::HTTP::Put.new(uri.path)
99
+ headers = { 'Authorization' => header,
100
+ 'Content-Type' => 'application/json'
101
+ }
102
+ headers.each do |key|
103
+ request[key] = headers[key]
104
+ end
105
+ request.body = post_data
106
+ resp, data = http.request(request)
107
+ else
108
+ resp, data = http.get(uri.path,
109
+ { 'Authorization' => header,
110
+ 'Content-Type' => 'application/json; charset=utf-8'
111
+ })
112
+ end
113
+ resp
114
+ end
93
115
  end
94
116
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restfull_oauth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel.van.den.Oord
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-13 00:00:00.000000000 Z
11
+ date: 2016-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler