mashape 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/authentication/authentication.rb +18 -0
- data/lib/authentication/authentication_utils.rb +24 -0
- data/lib/authentication/basic_authentication.rb +14 -0
- data/lib/authentication/custom_header_authentication.rb +12 -0
- data/lib/authentication/header_authentication.rb +11 -0
- data/lib/authentication/mashape_authentication.rb +15 -0
- data/lib/authentication/oauth10a_authentication.rb +9 -0
- data/lib/authentication/oauth2_authentication.rb +9 -0
- data/lib/authentication/oauth_authentication.rb +13 -0
- data/lib/authentication/query_authentication.rb +11 -0
- data/lib/http_client.rb +96 -0
- data/lib/http_utils.rb +47 -0
- data/lib/mashape.rb +1 -0
- data/lib/mashape_exception.rb +9 -0
- metadata +122 -0
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'base64'
|
3
|
+
require 'hmac-sha1'
|
4
|
+
|
5
|
+
module Mashape
|
6
|
+
class AuthenticationUtils
|
7
|
+
|
8
|
+
def AuthenticationUtils.generateMashapeAuthHeader(public_key, private_key)
|
9
|
+
unless public_key.empty? || private_key.empty?
|
10
|
+
hash = HMAC::SHA1.hexdigest(private_key, public_key)
|
11
|
+
auth = {"X-Mashape-Authorization" => Base64.encode64(public_key + ":" + hash).chomp.gsub(/\n/,'')}
|
12
|
+
end
|
13
|
+
return auth
|
14
|
+
end
|
15
|
+
|
16
|
+
def AuthenticationUtils.generateBasicAuthHeader(username, password)
|
17
|
+
unless username.empty? || password.empty?
|
18
|
+
auth = {"Authorization" => "Basic " + Base64.encode64(username + ":" + password).chomp.gsub(/\n/,'')}
|
19
|
+
end
|
20
|
+
return auth
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/header_authentication.rb")
|
2
|
+
require File.join(File.dirname(__FILE__), "/authentication_utils.rb")
|
3
|
+
|
4
|
+
module Mashape
|
5
|
+
class BasicAuthentication < Mashape::HeaderAuthentication
|
6
|
+
|
7
|
+
def initialize(username, password)
|
8
|
+
super()
|
9
|
+
@header = @header.merge(Mashape::AuthenticationUtils.generateBasicAuthHeader(username, password))
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/header_authentication.rb")
|
2
|
+
|
3
|
+
module Mashape
|
4
|
+
class CustomHeaderAuthentication < HeaderAuthentication
|
5
|
+
|
6
|
+
def initialize(header_name, header_value)
|
7
|
+
super()
|
8
|
+
@header[header_name] = header_value
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/header_authentication.rb")
|
2
|
+
require File.join(File.dirname(__FILE__), "/authentication_utils.rb")
|
3
|
+
|
4
|
+
module Mashape
|
5
|
+
class MashapeAuthentication < HeaderAuthentication
|
6
|
+
|
7
|
+
def initialize(public_key, private_key)
|
8
|
+
super()
|
9
|
+
@header = @header.merge(Mashape::AuthenticationUtils.generateMashapeAuthHeader(public_key, private_key))
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/oauth_authentication.rb")
|
2
|
+
|
3
|
+
module Mashape
|
4
|
+
class OAuth10aAuthentication < OAuthAuthentication
|
5
|
+
def initialize(consumer_key, consumer_secret, redirect_url)
|
6
|
+
super(consumer_key, consumer_secret, redirect_url)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/oauth_authentication.rb")
|
2
|
+
|
3
|
+
module Mashape
|
4
|
+
class OAuth2Authentication < OAuthAuthentication
|
5
|
+
def initialize(consumer_key, consumer_secret, redirect_url)
|
6
|
+
super(consumer_key, consumer_secret, redirect_url)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/authentication.rb")
|
2
|
+
|
3
|
+
module Mashape
|
4
|
+
class OAuthAuthentication < Authentication
|
5
|
+
|
6
|
+
def initialize(consumer_key, consumer_secret, redirect_url)
|
7
|
+
super()
|
8
|
+
@params[:consumer_key] = consumer_key
|
9
|
+
@params[:consumer_secret] = consumer_secret
|
10
|
+
@params[:redirect_url] = redirect_url
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/http_client.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'addressable/uri'
|
3
|
+
require 'rest-client'
|
4
|
+
require File.join(File.dirname(__FILE__), "/authentication/mashape_authentication.rb")
|
5
|
+
require File.join(File.dirname(__FILE__), "/authentication/custom_header_authentication.rb")
|
6
|
+
require File.join(File.dirname(__FILE__), "/authentication/query_authentication.rb")
|
7
|
+
require File.join(File.dirname(__FILE__), "/authentication/basic_authentication.rb")
|
8
|
+
require File.join(File.dirname(__FILE__), "/http_utils.rb")
|
9
|
+
require File.join(File.dirname(__FILE__), "/mashape_exception.rb")
|
10
|
+
|
11
|
+
module Mashape
|
12
|
+
class HttpResponse
|
13
|
+
attr :code, true
|
14
|
+
attr :raw_body, true
|
15
|
+
attr :body, true
|
16
|
+
attr :headers, true
|
17
|
+
end
|
18
|
+
|
19
|
+
class HttpClient
|
20
|
+
|
21
|
+
def HttpClient.do_request(method, url, parameters = nil, content_type = nil, response_type = nil, authentication_handlers = nil, &callback)
|
22
|
+
if callback
|
23
|
+
return Thread.new do
|
24
|
+
callback.call(internal_do_request(method, url, parameters, content_type, response_type, authentication_handlers))
|
25
|
+
end
|
26
|
+
else
|
27
|
+
return internal_do_request(method, url, parameters, content_type, response_type, authentication_handlers)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def HttpClient.internal_do_request(method, url, parameters = nil, content_type = nil, response_type = nil, authentication_handlers = nil)
|
32
|
+
httpResponse = nil;
|
33
|
+
|
34
|
+
headers = {}
|
35
|
+
if parameters == nil
|
36
|
+
case content_type
|
37
|
+
when :form || :binary
|
38
|
+
parameters = {}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# figure out what kind of auth we have and where to put it
|
43
|
+
authentication_handlers.each do |handler|
|
44
|
+
if handler.kind_of? Mashape::HeaderAuthentication
|
45
|
+
headers = headers.merge(handler.handleHeader)
|
46
|
+
elsif handler.kind_of? Mashape::QueryAuthentication
|
47
|
+
parameters = parameters.merge(handler.handleParams)
|
48
|
+
# elsif handler.kind_of? Mashape::OAuth10aAuthentication
|
49
|
+
# if handler.handleParams[:access_token] == nil || handler.handleParams[:access_secret] == nil
|
50
|
+
# raise Mashape::JsonException.new("Before consuming OAuth endpoint, invoke authenticate_oauth('access_token','access_secret') with not null values")
|
51
|
+
# end
|
52
|
+
# These headers will be processed by the proxy to sign the request
|
53
|
+
# headers["X-Mashape-OAuth-ConsumerKey"] = handler.handleParams[:consumer_key]
|
54
|
+
# headers["X-Mashape-OAuth-ConsumerSecret"] = handler.handleParams[:consumer_secret]
|
55
|
+
# headers["X-Mashape-OAuth-AccessToken"] = handler.handleParams[:access_token]
|
56
|
+
# headers["X-Mashape-OAuth-AccessSecret"] = handler.handleParams[:access_secret]
|
57
|
+
# elsif handler.kind_of? Mashape::OAuth2Authentication
|
58
|
+
# if handler.handleParams[:access_token] == nil
|
59
|
+
# raise Mashape::JsonException.new("Before consuming OAuth endpoint, invoke authenticate_oauth('access_token') with a not null value")
|
60
|
+
# end
|
61
|
+
# parameters = parameters.merge({"access_token" => handler.handleParams[:access_token]})
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
Mashape::HttpUtils.setRequestHeaders(content_type, response_type, headers)
|
66
|
+
|
67
|
+
begin
|
68
|
+
case method
|
69
|
+
when :get
|
70
|
+
uri = Addressable::URI.new
|
71
|
+
uri.query_values = parameters
|
72
|
+
httpResponse = RestClient.get url + "?" + uri.query, headers
|
73
|
+
when :post
|
74
|
+
httpResponse = RestClient.post url, parameters, headers
|
75
|
+
when :put
|
76
|
+
httpResponse = RestClient.put url, parameters, headers
|
77
|
+
when :delete
|
78
|
+
httpResponse = RestClient.delete url, parameters, headers
|
79
|
+
when :patch
|
80
|
+
httpResponse = RestClient.patch url, parameters, headers
|
81
|
+
end
|
82
|
+
rescue => e
|
83
|
+
httpResponse = e.response
|
84
|
+
end
|
85
|
+
|
86
|
+
response = HttpResponse.new
|
87
|
+
response.code = httpResponse.code
|
88
|
+
response.headers = httpResponse.headers
|
89
|
+
response.raw_body = httpResponse
|
90
|
+
|
91
|
+
Mashape::HttpUtils.setResponse(response_type, response)
|
92
|
+
|
93
|
+
return response
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/lib/http_utils.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/mashape_exception.rb")
|
2
|
+
require 'rubygems'
|
3
|
+
require 'json'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
module Mashape
|
7
|
+
|
8
|
+
class HttpUtils
|
9
|
+
|
10
|
+
def HttpUtils.uriEncode(value)
|
11
|
+
return URI.escape(value)
|
12
|
+
end
|
13
|
+
|
14
|
+
def HttpUtils.setRequestHeaders(content_type, response_type, headers)
|
15
|
+
headers["User-Agent"] = "mashape-ruby/2.0"
|
16
|
+
|
17
|
+
case content_type
|
18
|
+
when :json
|
19
|
+
headers["Content-Type"] = "application/json"
|
20
|
+
when :form
|
21
|
+
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
22
|
+
when :binary
|
23
|
+
headers["Content-Type"] = "multipart/form-data"
|
24
|
+
end
|
25
|
+
|
26
|
+
case response_type
|
27
|
+
when :json
|
28
|
+
headers["Accept"] = "application/json"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def HttpUtils.setResponse(response_type, output_response)
|
33
|
+
case response_type
|
34
|
+
when :json
|
35
|
+
begin
|
36
|
+
output_response.body = JSON.parse(output_response.raw_body)
|
37
|
+
rescue StandardError
|
38
|
+
raise Mashape::JsonException.new("Can't parse the following response into JSON: " + output_response.raw_body)
|
39
|
+
end
|
40
|
+
else
|
41
|
+
output_response.body = output_response.raw_body
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/lib/mashape.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Dir[File.dirname(__FILE__) + '/**/*.rb'].each {|file| require file }
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mashape
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 2
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 2.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Mashape
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2012-09-21 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rest-client
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: json
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
version: "0"
|
42
|
+
type: :runtime
|
43
|
+
version_requirements: *id002
|
44
|
+
- !ruby/object:Gem::Dependency
|
45
|
+
name: addressable
|
46
|
+
prerelease: false
|
47
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
type: :runtime
|
55
|
+
version_requirements: *id003
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: ruby-hmac
|
58
|
+
prerelease: false
|
59
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
type: :runtime
|
67
|
+
version_requirements: *id004
|
68
|
+
description: The Mashape Ruby client library needed to consume APIs on Mashape
|
69
|
+
email: support@mashape.com
|
70
|
+
executables: []
|
71
|
+
|
72
|
+
extensions: []
|
73
|
+
|
74
|
+
extra_rdoc_files: []
|
75
|
+
|
76
|
+
files:
|
77
|
+
- lib/authentication/authentication.rb
|
78
|
+
- lib/authentication/authentication_utils.rb
|
79
|
+
- lib/authentication/basic_authentication.rb
|
80
|
+
- lib/authentication/custom_header_authentication.rb
|
81
|
+
- lib/authentication/header_authentication.rb
|
82
|
+
- lib/authentication/mashape_authentication.rb
|
83
|
+
- lib/authentication/oauth10a_authentication.rb
|
84
|
+
- lib/authentication/oauth2_authentication.rb
|
85
|
+
- lib/authentication/oauth_authentication.rb
|
86
|
+
- lib/authentication/query_authentication.rb
|
87
|
+
- lib/http_client.rb
|
88
|
+
- lib/http_utils.rb
|
89
|
+
- lib/mashape.rb
|
90
|
+
- lib/mashape_exception.rb
|
91
|
+
has_rdoc: true
|
92
|
+
homepage: https://www.mashape.com
|
93
|
+
licenses: []
|
94
|
+
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
version: "0"
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
version: "0"
|
114
|
+
requirements: []
|
115
|
+
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.3.6
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: The Mashape Ruby client library
|
121
|
+
test_files: []
|
122
|
+
|