web_api 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/History.txt +1 -0
- data/Manifest.txt +21 -0
- data/README.rdoc +57 -0
- data/TODO.txt +3 -0
- data/examples/bitcoincharts +78 -0
- data/examples/mtgox1 +13 -0
- data/examples/mtgox2 +19 -0
- data/examples/twitter +35 -0
- data/examples/youtube +34 -0
- data/features/main.feature +9 -0
- data/features/step_definitions/main_steps.rb +13 -0
- data/lib/web_api/auth.rb +28 -0
- data/lib/web_api/client.rb +27 -0
- data/lib/web_api/signed.rb +31 -0
- data/lib/web_api/signed2.rb +31 -0
- data/lib/web_api/signet.rb +33 -0
- data/lib/web_api/version.rb +3 -0
- data/lib/web_api/web_api.rb +130 -0
- data/lib/web_api.rb +16 -0
- data/test/web_api.rb +7 -0
- data/web_api.gemspec +58 -0
- metadata +151 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f18b910d048401a2533ff77db4f3051bb31dea9e
|
4
|
+
data.tar.gz: f7fd0c7b709ab90fc58842253d94be387d07f99e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0c579598e841958f395a2246937d348476ca942e1e705e26ad2256a1fe694cb48521ebf10cf62f4cf4cc3b6184c8910b6c3c3d747bb2878055dde1ec43d7f99a
|
7
|
+
data.tar.gz: 53f7700165d07135d3481c7ef71a5b609cbb3bfc69ad6d93c95114509d5b8a1a81c8f0acb80f6ea533d7d970fc411f2f70d2cefa0b7650d2bd57ea6d7a83bb07
|
data/History.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
=== 0.0.0 / 2013-12-23 13:59:42 -0800
|
data/Manifest.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
README.rdoc
|
4
|
+
TODO.txt
|
5
|
+
examples/bitcoincharts
|
6
|
+
examples/mtgox1
|
7
|
+
examples/mtgox2
|
8
|
+
examples/twitter
|
9
|
+
examples/youtube
|
10
|
+
features/main.feature
|
11
|
+
features/step_definitions/main_steps.rb
|
12
|
+
lib/web_api.rb
|
13
|
+
lib/web_api/auth.rb
|
14
|
+
lib/web_api/client.rb
|
15
|
+
lib/web_api/signed.rb
|
16
|
+
lib/web_api/signed2.rb
|
17
|
+
lib/web_api/signet.rb
|
18
|
+
lib/web_api/version.rb
|
19
|
+
lib/web_api/web_api.rb
|
20
|
+
test/web_api.rb
|
21
|
+
web_api.gemspec
|
data/README.rdoc
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
= web_api
|
2
|
+
|
3
|
+
github :: https://www.github.com/carlosjhr64/web_api
|
4
|
+
rubygems :: https://rubygems.org/gems/web_api
|
5
|
+
|
6
|
+
== DESCRIPTION:
|
7
|
+
|
8
|
+
Ruby library for web api's.
|
9
|
+
|
10
|
+
In Beta. Testing not done. Only GET and POST implemented.
|
11
|
+
|
12
|
+
Looks like there's enough to be useful, and
|
13
|
+
wanted register the name.
|
14
|
+
I think the front end in fixed, so
|
15
|
+
the synopsis below is not likely to change.
|
16
|
+
|
17
|
+
== SYNOPSIS:
|
18
|
+
|
19
|
+
require 'web_api'
|
20
|
+
webapi = WEB_API::WebApi.new
|
21
|
+
### **Name** **Url** **Type(get or post)**
|
22
|
+
webapi.add_method('wutuwant', 'http://service.org/v1/wutuwant', 'get')
|
23
|
+
# You can pass the post's (or query's) key value pairs in a hash.
|
24
|
+
response = webapi.wutuwant({'key'=>'value' })
|
25
|
+
|
26
|
+
== FEATURES/PROBLEMS:
|
27
|
+
|
28
|
+
* Beta, still testing.
|
29
|
+
|
30
|
+
== INSTALL:
|
31
|
+
|
32
|
+
$ sudo gem install web_api
|
33
|
+
|
34
|
+
== LICENSE:
|
35
|
+
|
36
|
+
(The MIT License)
|
37
|
+
|
38
|
+
Copyright (c) 2013 CarlosJHR64
|
39
|
+
|
40
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
41
|
+
a copy of this software and associated documentation files (the
|
42
|
+
'Software'), to deal in the Software without restriction, including
|
43
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
44
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
45
|
+
permit persons to whom the Software is furnished to do so, subject to
|
46
|
+
the following conditions:
|
47
|
+
|
48
|
+
The above copyright notice and this permission notice shall be
|
49
|
+
included in all copies or substantial portions of the Software.
|
50
|
+
|
51
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
52
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
53
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
54
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
55
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
56
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
57
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/TODO.txt
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Standard Libraries:
|
4
|
+
require 'English'
|
5
|
+
require 'pp'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
# Gems:
|
9
|
+
require 'help_parser'
|
10
|
+
|
11
|
+
# This Gem:
|
12
|
+
require 'web_api'
|
13
|
+
include WEB_API
|
14
|
+
|
15
|
+
VERSION = '0.0.0'
|
16
|
+
NAME = File.basename $PROGRAM_NAME # bitcoincharts
|
17
|
+
|
18
|
+
DEFAULT_MARKET = 'mtgoxUSD'
|
19
|
+
DEFAULT_START = Time.now.to_i - 60*60 # One hour ago
|
20
|
+
|
21
|
+
MARKETS_HELP = <<MARKETS_HELP.strip
|
22
|
+
Options for "markets": None.
|
23
|
+
MARKETS_HELP
|
24
|
+
|
25
|
+
TRADES_HELP = <<TRADES_HELP.strip
|
26
|
+
Options for "trades":
|
27
|
+
--symbol=SYMBOL Defaults to #{DEFAULT_MARKET}.
|
28
|
+
--start=UNIXTIME Defaults to one hour ago.
|
29
|
+
TRADES_HELP
|
30
|
+
|
31
|
+
HELP = <<HELP.strip
|
32
|
+
Usage: #{NAME} [global options] [markets, trades] [trades options]
|
33
|
+
Global Options:
|
34
|
+
-h --help This help.
|
35
|
+
-v --version Puts version and exits.
|
36
|
+
-j --json Use json's pretty print instead of puts.
|
37
|
+
-p --pp Use pp instead of puts.
|
38
|
+
-t --trace Do some tracing to STDERR.
|
39
|
+
#{MARKETS_HELP}
|
40
|
+
#{TRADES_HELP}
|
41
|
+
HELP
|
42
|
+
|
43
|
+
begin
|
44
|
+
|
45
|
+
OPTIONS = HELP_PARSER::HelpParser.new(VERSION, HELP)
|
46
|
+
WEBAPI = WebApi.new
|
47
|
+
WebApi.trace = STDERR if OPTIONS[:trace]
|
48
|
+
[['trades', 'http://api.bitcoincharts.com/v1/trades.csv', 'get'],
|
49
|
+
['markets', 'http://api.bitcoincharts.com/v1/markets.json', 'get'],
|
50
|
+
].each{|method, url, type| WEBAPI.add(method, url, type)}
|
51
|
+
|
52
|
+
COMMAND = ARGV.shift
|
53
|
+
case COMMAND
|
54
|
+
when 'markets'
|
55
|
+
ARGUMENTS = HELP_PARSER::HelpParser.new(VERSION, MARKETS_HELP)
|
56
|
+
RESPONSE = JSON.parse WEBAPI.markets
|
57
|
+
when 'trades'
|
58
|
+
ARGUMENTS = HELP_PARSER::HelpParser.new(VERSION, TRADES_HELP)
|
59
|
+
ARGUMENTS.defaults!(:symbol, DEFAULT_MARKET, DEFAULT_MARKET,)
|
60
|
+
ARGUMENTS.defaults!(:start, DEFAULT_START, DEFAULT_START)
|
61
|
+
RESPONSE = WEBAPI.trades(ARGUMENTS)
|
62
|
+
else
|
63
|
+
STDERR.puts 'Need "markets" or "trades" argument'
|
64
|
+
exit 64
|
65
|
+
end
|
66
|
+
|
67
|
+
if OPTIONS[:json]
|
68
|
+
puts JSON.pretty_generate RESPONSE
|
69
|
+
elsif OPTIONS[:pp]
|
70
|
+
pp RESPONSE
|
71
|
+
else
|
72
|
+
puts RESPONSE
|
73
|
+
end
|
74
|
+
|
75
|
+
rescue HELP_PARSER::UsageException, HELP_PARSER::UsageError
|
76
|
+
STDERR.puts $!
|
77
|
+
exit 64
|
78
|
+
end
|
data/examples/mtgox1
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'web_api'
|
3
|
+
|
4
|
+
STDERR.print "MtGox Key: "
|
5
|
+
KEY = STDIN.gets.strip
|
6
|
+
STDERR.print "MtGox Secret: "
|
7
|
+
SECRET = STDIN.gets.strip
|
8
|
+
|
9
|
+
AUTH = {:key=>KEY, :secret=>SECRET}
|
10
|
+
WEBAPI = WEB_API::Signed.new(AUTH)
|
11
|
+
# Note that this is api/1
|
12
|
+
WEBAPI.add('history', 'https://data.mtgox.com/api/1/generic/private/wallet/history', 'post')
|
13
|
+
puts WEBAPI.history({:currency=>'USD'})
|
data/examples/mtgox2
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'web_api'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
STDERR.print "MtGox Key: "
|
6
|
+
KEY = STDIN.gets.strip
|
7
|
+
STDERR.print "MtGox Secret: "
|
8
|
+
SECRET = STDIN.gets.strip
|
9
|
+
|
10
|
+
AUTH = {:key=>KEY, :secret=>SECRET}
|
11
|
+
|
12
|
+
include WEB_API
|
13
|
+
WebApi.trace = STDERR
|
14
|
+
# Note that this is api/2, we need to properly set the base of the api:
|
15
|
+
WEBAPI = Signed2.new(AUTH, 'https://data.mtgox.com/api/2')
|
16
|
+
# Note that this is api/2, we need to properly set the path of the method:
|
17
|
+
WEBAPI.add('info', 'BTCUSD/money/info', 'post')
|
18
|
+
INFO = JSON.parse WEBAPI.info
|
19
|
+
puts JSON.pretty_generate INFO
|
data/examples/twitter
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'oauth'
|
3
|
+
require 'web_api'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
puts "You'll need to have your OAuth Key and Secret."
|
7
|
+
puts "Then you'll need to follow a link to get your pin."
|
8
|
+
puts "So first your API Key:"
|
9
|
+
print "Key: "
|
10
|
+
KEY = STDIN.gets.strip
|
11
|
+
puts "Now your API Secret:"
|
12
|
+
print "Secret: "
|
13
|
+
SECRET = STDIN.gets.strip
|
14
|
+
|
15
|
+
|
16
|
+
SITE = "http://api.twitter.com"
|
17
|
+
CONSUMER = OAuth::Consumer.new(KEY, SECRET, {:site => SITE})
|
18
|
+
REQUEST_TOKEN = CONSUMER.get_request_token
|
19
|
+
|
20
|
+
|
21
|
+
puts "Set your set your web browser this url:"
|
22
|
+
puts " " + REQUEST_TOKEN.authorize_url
|
23
|
+
puts "Then follow the instructions to authorize."
|
24
|
+
puts "Twitter should give you a pin number."
|
25
|
+
print "Pin: "
|
26
|
+
PIN = STDIN.gets.strip
|
27
|
+
|
28
|
+
|
29
|
+
ACCESS = REQUEST_TOKEN.get_access_token(:oauth_verifier => PIN)
|
30
|
+
|
31
|
+
WEBAPI = WEB_API::Client.new(ACCESS)
|
32
|
+
WEBAPI.add('timeline', 'https://api.twitter.com/1.1/statuses/home_timeline.json', 'get')
|
33
|
+
RESPONSE = JSON.parse WEBAPI.timeline
|
34
|
+
|
35
|
+
puts JSON.pretty_generate RESPONSE
|
data/examples/youtube
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'signet/oauth_2/client'
|
3
|
+
# helpful blog:
|
4
|
+
# http://blog.mkristian.tk/2013/02/getting-up-and-running-with-oauth-using.html
|
5
|
+
|
6
|
+
print "Your client id: "
|
7
|
+
CLIENT_ID = STDIN.gets.strip
|
8
|
+
print "Your client secret: "
|
9
|
+
CLIENT_SECRET = STDIN.gets.strip
|
10
|
+
|
11
|
+
SIGNET = Signet::OAuth2::Client.new(
|
12
|
+
:authorization_uri => 'https://accounts.google.com/o/oauth2/auth',
|
13
|
+
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
|
14
|
+
:client_id => CLIENT_ID,
|
15
|
+
:client_secret => CLIENT_SECRET,
|
16
|
+
:scope => 'https://gdata.youtube.com',
|
17
|
+
:redirect_uri => 'http://localhost:9292/oauth2callback'
|
18
|
+
)
|
19
|
+
|
20
|
+
puts "copy and paste authorization url to browser and follow intructions"
|
21
|
+
puts
|
22
|
+
puts SIGNET.authorization_uri
|
23
|
+
puts
|
24
|
+
puts "paste the code from redirect url from your browser into the console"
|
25
|
+
pin = gets
|
26
|
+
|
27
|
+
SIGNET.code = pin.strip
|
28
|
+
SIGNET.fetch_access_token!
|
29
|
+
|
30
|
+
require 'web_api'
|
31
|
+
|
32
|
+
WEBAPI = WEB_API::Signet.new(SIGNET)
|
33
|
+
WEBAPI.add('mysubs', 'https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&mine=true', 'get')
|
34
|
+
puts WEBAPI.mysubs
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Given /^(w+) (.*)$/ do |given, condition|
|
2
|
+
condition.strip!
|
3
|
+
case given
|
4
|
+
when 'Given'
|
5
|
+
raise "'Given' form not defined."
|
6
|
+
when 'When'
|
7
|
+
raise "'When' form not defined."
|
8
|
+
when 'Then'
|
9
|
+
raise "'Then' form not defined."
|
10
|
+
else
|
11
|
+
raise "'#{given}' form not defined."
|
12
|
+
end
|
13
|
+
end
|
data/lib/web_api/auth.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module WEB_API
|
2
|
+
|
3
|
+
class AuthMethod < WebApiMethod
|
4
|
+
attr_reader :auth
|
5
|
+
def initialize(auth, *args)
|
6
|
+
@auth = auth
|
7
|
+
super(*args)
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(args)
|
11
|
+
args.unshift(@auth)
|
12
|
+
super(args)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Auth < WebApi
|
17
|
+
attr_reader :auth
|
18
|
+
def initialize(auth, *args)
|
19
|
+
@auth = auth
|
20
|
+
super(*args)
|
21
|
+
end
|
22
|
+
|
23
|
+
def _add(method, uri, type)
|
24
|
+
webmethods[method] = AuthMethod.new(@auth, uri, type)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end # WEB_API
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module WEB_API
|
2
|
+
|
3
|
+
class ClientMethod < WebApiMethod
|
4
|
+
attr_reader :client
|
5
|
+
def initialize(client, *args)
|
6
|
+
@client = client
|
7
|
+
super(*args)
|
8
|
+
end
|
9
|
+
|
10
|
+
def http
|
11
|
+
@client
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Client < WebApi
|
16
|
+
attr_reader :client
|
17
|
+
def initialize(client, *args)
|
18
|
+
@client = client
|
19
|
+
super(*args)
|
20
|
+
end
|
21
|
+
|
22
|
+
def _add(method, uri, type)
|
23
|
+
webmethods[method] = ClientMethod.new(@client, uri, type)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end # WEB_API
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module WEB_API
|
2
|
+
|
3
|
+
class SignedMethod < AuthMethod
|
4
|
+
# This modified from sferik's mtgox gem's MtGox::Request#headers
|
5
|
+
def headers(args)
|
6
|
+
signature = Base64.strict_encode64(
|
7
|
+
OpenSSL::HMAC.digest 'sha512',
|
8
|
+
Base64.decode64(auth[:secret]),
|
9
|
+
data(args)
|
10
|
+
)
|
11
|
+
headers = super(args)
|
12
|
+
headers['Rest-Key'] = auth[:key]
|
13
|
+
headers['Rest-Sign'] = signature
|
14
|
+
Signed.trace.puts "headers:\n#{headers}" if Signed.trace
|
15
|
+
return headers
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(args)
|
19
|
+
nonce = (Time.now.to_f * 1000000).to_i
|
20
|
+
args.push( {:nonce => nonce} )
|
21
|
+
super(args)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Signed < Auth
|
26
|
+
def _add(method, uri, type)
|
27
|
+
webmethods[method] = SignedMethod.new(auth, uri, type)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end # WEB_API
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module WEB_API
|
2
|
+
|
3
|
+
class Signed2Method < AuthMethod
|
4
|
+
def headers(args)
|
5
|
+
Signed2.trace.puts "path: #{path}" if Signed2.trace
|
6
|
+
signature = Base64.strict_encode64(
|
7
|
+
OpenSSL::HMAC.digest 'sha512',
|
8
|
+
Base64.decode64(auth[:secret]),
|
9
|
+
path + "\0" + data(args)
|
10
|
+
)
|
11
|
+
headers = super(args)
|
12
|
+
headers['Rest-Key'] = auth[:key]
|
13
|
+
headers['Rest-Sign'] = signature
|
14
|
+
Signed2.trace.puts "headers:\n#{headers}" if Signed2.trace
|
15
|
+
return headers
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(args)
|
19
|
+
tonce = (Time.now.to_f * 1000000).to_i
|
20
|
+
args.push( {:tonce => tonce} )
|
21
|
+
super(args)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Signed2 < Auth
|
26
|
+
def _add(method, uri, type)
|
27
|
+
webmethods[method] = Signed2Method.new(auth, uri, type)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end # WEB_API
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module WEB_API
|
2
|
+
|
3
|
+
class SignetMethod < ClientMethod
|
4
|
+
OK = 200
|
5
|
+
def parse(response)
|
6
|
+
raise ResponseError, response.body unless response.status == OK
|
7
|
+
response.body
|
8
|
+
end
|
9
|
+
|
10
|
+
def fetch_protected_resource(url, headers=nil, type='GET', body=nil)
|
11
|
+
options = {:uri => url, :method => type}
|
12
|
+
options[:body] = body if body
|
13
|
+
options[:headers] = headers if headers
|
14
|
+
parse client.fetch_protected_resource(options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def post(args)
|
18
|
+
fetch_protected_resource(uri.to_s, headers(args), 'POST', data(args))
|
19
|
+
end
|
20
|
+
|
21
|
+
def get(args)
|
22
|
+
url = "#{uri.scheme}://#{uri.host}#{pathquery(args)}"
|
23
|
+
fetch_protected_resource(url, headers(args))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Signet < Client
|
28
|
+
def _add(method, uri, type)
|
29
|
+
webmethods[method] = SignetMethod.new(client, uri, type)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end # WEB_API
|
@@ -0,0 +1,130 @@
|
|
1
|
+
module WEB_API
|
2
|
+
class ResponseError < RuntimeError
|
3
|
+
end
|
4
|
+
|
5
|
+
class WebApiMethod
|
6
|
+
|
7
|
+
SCHEMES = ['http', 'https']
|
8
|
+
TYPES = [:post, :get]
|
9
|
+
|
10
|
+
attr_accessor :base, :path
|
11
|
+
attr_reader :uri, :type
|
12
|
+
|
13
|
+
def initialize(uri, type)
|
14
|
+
"Invalid scheme, #{uri.scheme}." unless SCHEMES.include?(uri.scheme)
|
15
|
+
"Invalid method type, #{type}." unless WebApiMethod::TYPES.include?(type)
|
16
|
+
@uri, @type, @base, @path = uri, type, "#{uri.scheme}://#{uri.host}", uri.path[1,-1]
|
17
|
+
end
|
18
|
+
|
19
|
+
# Escapes value's string representation for query string use.
|
20
|
+
def escape(value)
|
21
|
+
URI.escape(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
22
|
+
end
|
23
|
+
|
24
|
+
# kv is a key, value pair
|
25
|
+
# [k, v]
|
26
|
+
def kv_map(kv)
|
27
|
+
kv.map{|value| escape(value)}.join('=')
|
28
|
+
end
|
29
|
+
|
30
|
+
# arg is a hash
|
31
|
+
# {k=>v,...}
|
32
|
+
def arg_map(arg)
|
33
|
+
arg.map{|kv| kv_map(kv) }.join('&')
|
34
|
+
end
|
35
|
+
|
36
|
+
# args is an array of hashes
|
37
|
+
# [ {k=>v,...}, {k=>v,...}, ...]
|
38
|
+
# The net result of this is a flattened query string
|
39
|
+
# "k=v&k=v..."
|
40
|
+
def args_map(args)
|
41
|
+
string = args.select{|arg| arg.length>0}.map{|arg| arg_map(arg)}.join('&')
|
42
|
+
WebApi.trace.puts "args_map: #{string}" if WebApi.trace
|
43
|
+
return string
|
44
|
+
end
|
45
|
+
alias data args_map
|
46
|
+
alias query_string args_map
|
47
|
+
|
48
|
+
def headers(args)
|
49
|
+
headers = {'User-Agent' => "ruby gem web_api #{VERSION}"}
|
50
|
+
WebApi.trace.puts "headers:\n#{headers}" if WebApi.trace
|
51
|
+
return headers
|
52
|
+
end
|
53
|
+
|
54
|
+
OK = '200'
|
55
|
+
def parse(response)
|
56
|
+
raise ResponseError, response.message unless response.code == OK
|
57
|
+
response.body
|
58
|
+
end
|
59
|
+
|
60
|
+
SSL = {'http' => false, 'https' => true}
|
61
|
+
def http
|
62
|
+
http = Net::HTTP.new(@uri.host, @uri.port)
|
63
|
+
http.use_ssl = SSL[@uri.scheme]
|
64
|
+
return http
|
65
|
+
end
|
66
|
+
|
67
|
+
def post(args)
|
68
|
+
parse http.post(@uri.path, data(args), headers(args))
|
69
|
+
end
|
70
|
+
|
71
|
+
def pathquery(args)
|
72
|
+
p, q0, q1 = @uri.path, @uri.query, query_string(args)
|
73
|
+
q = [q0, q1].select{|q| q && q.length>0}.join('&')
|
74
|
+
pq = (q.length>0)? p + '?' + q : p
|
75
|
+
WebApi.trace.puts "pathquery #{pq}" if WebApi.trace
|
76
|
+
return pq
|
77
|
+
end
|
78
|
+
|
79
|
+
def get(args)
|
80
|
+
parse http.get(pathquery(args), headers(args))
|
81
|
+
end
|
82
|
+
|
83
|
+
def call(args)
|
84
|
+
self.method(@type).call(args)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
class WebApi
|
89
|
+
|
90
|
+
@@trace = nil
|
91
|
+
def self.trace
|
92
|
+
@@trace
|
93
|
+
end
|
94
|
+
def self.trace=(trace)
|
95
|
+
@@trace=trace
|
96
|
+
end
|
97
|
+
|
98
|
+
attr_reader :webmethods
|
99
|
+
def initialize(base=nil)
|
100
|
+
@base = base
|
101
|
+
@webmethods = {}
|
102
|
+
end
|
103
|
+
|
104
|
+
def method_missing(symbol , *args)
|
105
|
+
super unless @webmethods.has_key?(symbol)
|
106
|
+
@webmethods[symbol].call(args)
|
107
|
+
end
|
108
|
+
|
109
|
+
def _add(method, uri, type)
|
110
|
+
@webmethods[method] = WebApiMethod.new(uri, type)
|
111
|
+
end
|
112
|
+
|
113
|
+
def _url(path)
|
114
|
+
(@base)? File.join(@base, path) : path
|
115
|
+
end
|
116
|
+
|
117
|
+
def add(method, path, type=:get)
|
118
|
+
method = method.downcase.to_sym
|
119
|
+
uri = URI.parse _url(path)
|
120
|
+
type = type.downcase.to_sym
|
121
|
+
raise "#{method} already defined" if @webmethods.include?(method)
|
122
|
+
webmethod = _add(method, uri, type)
|
123
|
+
if @base
|
124
|
+
webmethod.base = @base
|
125
|
+
webmethod.path = path
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
end # WEB_API
|
data/lib/web_api.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# Standard Libraries
|
2
|
+
require 'uri'
|
3
|
+
require 'net/https'
|
4
|
+
require 'base64'
|
5
|
+
|
6
|
+
# WEB_API
|
7
|
+
require 'web_api/version.rb'
|
8
|
+
require 'web_api/web_api.rb'
|
9
|
+
require 'web_api/auth.rb'
|
10
|
+
require 'web_api/signed.rb'
|
11
|
+
require 'web_api/signed2.rb'
|
12
|
+
require 'web_api/client.rb'
|
13
|
+
require 'web_api/signet.rb'
|
14
|
+
|
15
|
+
# Requires:
|
16
|
+
#`ruby`
|
data/test/web_api.rb
ADDED
data/web_api.gemspec
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
|
3
|
+
s.name = 'web_api'
|
4
|
+
s.version = '0.0.0'
|
5
|
+
|
6
|
+
s.homepage = 'https://github.com/carlosjhr64/web_api'
|
7
|
+
|
8
|
+
s.author = 'CarlosJHR64'
|
9
|
+
s.email = 'carlosjhr64@gmail.com'
|
10
|
+
|
11
|
+
s.date = '2013-12-27'
|
12
|
+
s.licenses = ['MIT']
|
13
|
+
|
14
|
+
s.description = <<DESCRIPTION
|
15
|
+
Ruby library for web api's.
|
16
|
+
|
17
|
+
In Beta. Testing not done. Only GET and POST implemented.
|
18
|
+
DESCRIPTION
|
19
|
+
|
20
|
+
s.summary = <<SUMMARY
|
21
|
+
Ruby library for web api's.
|
22
|
+
SUMMARY
|
23
|
+
|
24
|
+
s.extra_rdoc_files = ['README.rdoc']
|
25
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
26
|
+
|
27
|
+
s.require_paths = ["lib"]
|
28
|
+
s.files = %w(
|
29
|
+
History.txt
|
30
|
+
Manifest.txt
|
31
|
+
README.rdoc
|
32
|
+
TODO.txt
|
33
|
+
examples/bitcoincharts
|
34
|
+
examples/mtgox1
|
35
|
+
examples/mtgox2
|
36
|
+
examples/twitter
|
37
|
+
examples/youtube
|
38
|
+
features/main.feature
|
39
|
+
features/step_definitions/main_steps.rb
|
40
|
+
lib/web_api.rb
|
41
|
+
lib/web_api/auth.rb
|
42
|
+
lib/web_api/client.rb
|
43
|
+
lib/web_api/signed.rb
|
44
|
+
lib/web_api/signed2.rb
|
45
|
+
lib/web_api/signet.rb
|
46
|
+
lib/web_api/version.rb
|
47
|
+
lib/web_api/web_api.rb
|
48
|
+
test/web_api.rb
|
49
|
+
web_api.gemspec
|
50
|
+
)
|
51
|
+
|
52
|
+
s.add_development_dependency 'help_parser', '~> 1.1', '>= 1.1.0'
|
53
|
+
s.add_development_dependency 'oauth', '~> 0.4', '>= 0.4.7'
|
54
|
+
s.add_development_dependency 'signet', '~> 0.5', '>= 0.5.0'
|
55
|
+
s.add_development_dependency 'test-unit', '~> 2.5', '>= 2.5.5'
|
56
|
+
s.requirements << 'ruby: ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]'
|
57
|
+
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: web_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- CarlosJHR64
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: help_parser
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.1'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.1.0
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.1'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.1.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: oauth
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0.4'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 0.4.7
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0.4'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 0.4.7
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: signet
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0.5'
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 0.5.0
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0.5'
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 0.5.0
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: test-unit
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - "~>"
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '2.5'
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 2.5.5
|
83
|
+
type: :development
|
84
|
+
prerelease: false
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.5'
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 2.5.5
|
93
|
+
description: |
|
94
|
+
Ruby library for web api's.
|
95
|
+
|
96
|
+
In Beta. Testing not done. Only GET and POST implemented.
|
97
|
+
email: carlosjhr64@gmail.com
|
98
|
+
executables: []
|
99
|
+
extensions: []
|
100
|
+
extra_rdoc_files:
|
101
|
+
- README.rdoc
|
102
|
+
files:
|
103
|
+
- History.txt
|
104
|
+
- Manifest.txt
|
105
|
+
- README.rdoc
|
106
|
+
- TODO.txt
|
107
|
+
- examples/bitcoincharts
|
108
|
+
- examples/mtgox1
|
109
|
+
- examples/mtgox2
|
110
|
+
- examples/twitter
|
111
|
+
- examples/youtube
|
112
|
+
- features/main.feature
|
113
|
+
- features/step_definitions/main_steps.rb
|
114
|
+
- lib/web_api.rb
|
115
|
+
- lib/web_api/auth.rb
|
116
|
+
- lib/web_api/client.rb
|
117
|
+
- lib/web_api/signed.rb
|
118
|
+
- lib/web_api/signed2.rb
|
119
|
+
- lib/web_api/signet.rb
|
120
|
+
- lib/web_api/version.rb
|
121
|
+
- lib/web_api/web_api.rb
|
122
|
+
- test/web_api.rb
|
123
|
+
- web_api.gemspec
|
124
|
+
homepage: https://github.com/carlosjhr64/web_api
|
125
|
+
licenses:
|
126
|
+
- MIT
|
127
|
+
metadata: {}
|
128
|
+
post_install_message:
|
129
|
+
rdoc_options:
|
130
|
+
- "--main"
|
131
|
+
- README.rdoc
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
requirements:
|
145
|
+
- 'ruby: ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]'
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 2.0.3
|
148
|
+
signing_key:
|
149
|
+
specification_version: 4
|
150
|
+
summary: Ruby library for web api's.
|
151
|
+
test_files: []
|