web_api 0.1.0 → 1.0.210927

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
- SHA1:
3
- metadata.gz: 8765bd7c5bf244439ea4930d925e8bba2340cc0c
4
- data.tar.gz: 83059e8f9f9024398880e33869e5faeebaff4212
2
+ SHA256:
3
+ metadata.gz: 82115f267d8164697506e891e5dace6f796a69ab8923c1dd193dd5d38faeff3e
4
+ data.tar.gz: c60078933f51883cadefb158d221607e9e11bad9cd66fc47185f87d2df45adc4
5
5
  SHA512:
6
- metadata.gz: ebcade1ce08d06c82ff11b3716c2dc82aaeb32e87b675338636b8b7a053b1510e1e38f785b4d5b03004c96f0e07fb85e18c916fae9053a8ed31ec38943b1ff7b
7
- data.tar.gz: f2fda298efd7d459f78885ce8f7a8b44143c548ff900491773f343e80da727ab3b3ba66efe2d58b7624d3a933bbbc0839982e327110581b3a38fb1b73b5d3c34
6
+ metadata.gz: 32e2b5f9b5f467aee348c968a409bd7df263eeb73395929e8e3f43945bdf9ed2cf6f9d3b0c715dc6685239f56601d1ed99928a858d9d622aca285542a5539d16
7
+ data.tar.gz: ed2d94e383e02ae8c327157054b91d069131470e6b21ed107797879eaef05d6072d134eff70ea7999e7b6e989d430b082b28e936b1d0c1e9a020c03aee781e36
data/README.md ADDED
@@ -0,0 +1,101 @@
1
+ # WebApi
2
+
3
+ * [VERSION 1.0.210927](https://www.github.com/carlosjhr64/web_api)
4
+ * [github](https://www.github.com/carlosjhr64/web_api)
5
+ * [rubygems](https://rubygems.org/gems/web_api)
6
+
7
+ ## DESCRIPTION:
8
+
9
+ Ruby library for web api's.
10
+
11
+ ## SYNOPSIS:
12
+ ```ruby
13
+ require 'web_api'
14
+ webapi = WebApi.new "https://api.site.org/path-to-base/",
15
+ header: {Authorization: "Bearer ABC123XYZ"}
16
+ # for a post to https://api.site.org/path-to-base/resource...
17
+ webapi.add(:resource, type: :post)
18
+ # You can pass the post's (or query's) key value pairs in a hash.
19
+ body = webapi.resource(data: {key: "value"})
20
+ ```
21
+ ## INSTALL:
22
+ ```console
23
+ $ gem install web_api
24
+ ```
25
+ ## MORE:
26
+
27
+ There's not that much code here...
28
+ under 200 lines in `lib/**.rb` at the time of this writing.
29
+ Take a look at the examples given at [github](https://github.com/carlosjhr64/web_api/tree/master/examples)
30
+ for use cases.
31
+
32
+ The model is that at each step...
33
+
34
+ 1. instantiation of a WebApi object
35
+ 2. addition of a WebApi method
36
+ 3. call to a WebApi method
37
+
38
+ ...one builds up the url, type, data, and header of the http request.
39
+ The WebApi methods `#new`, `#add`, and `#<method>`
40
+ all have the same argument signature:
41
+
42
+ extension String,
43
+ type: Symbol,
44
+ data: Hash,
45
+ header: Hash,
46
+ dumper: Proc,
47
+ Parser: Proc|Hash(String, Proc)
48
+
49
+ The extension builds up the url by concatenation.
50
+ The data and headers hashes are built up with merge.
51
+ The type, dumper, and parser can be changed at each step.
52
+
53
+ One can read the code to check the minor nuances of each method's signature,
54
+ such as default values.
55
+
56
+ Note that `#add` will assume extension is the same as the name of the method if
57
+ no extension is given.
58
+
59
+ Note that `#<method>` will assume the user meant to pass data if
60
+ it only gets a hash:
61
+
62
+ #<method>({"a"=>"ABC","x"=>"XYZ"})
63
+ #=> #<method>('', data: {"a"=>"ABC","x"=>"XYZ"})
64
+
65
+ The dumper to dump the data in a post request is JSON.dump by default
66
+ if JSON is available.
67
+
68
+ The parser to parse the body of an "application/json" type content is
69
+ JSON.parse by default if available.
70
+ You can read the code and inspect `WebApi::PARSER`
71
+ to see the other parsers available by default.
72
+
73
+ If one does not want to parse the response's body,
74
+ one can set `parser: :none`. For example:
75
+ ```ruby
76
+ body = webapi.resourse(data: {key: "value"}, parser: :none)
77
+ ```
78
+ ## LICENSE:
79
+
80
+ (The MIT License)
81
+
82
+ Copyright (c) 2021 CarlosJHR64
83
+
84
+ Permission is hereby granted, free of charge, to any person obtaining
85
+ a copy of this software and associated documentation files (the
86
+ 'Software'), to deal in the Software without restriction, including
87
+ without limitation the rights to use, copy, modify, merge, publish,
88
+ distribute, sublicense, and/or sell copies of the Software, and to
89
+ permit persons to whom the Software is furnished to do so, subject to
90
+ the following conditions:
91
+
92
+ The above copyright notice and this permission notice shall be
93
+ included in all copies or substantial portions of the Software.
94
+
95
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
96
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
97
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
98
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
99
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
100
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
101
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,130 +1,58 @@
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
1
  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
2
+ DUMPER = (defined? JSON)? JSON.method(:dump) : :none
3
+
4
+ PARSER = Hash.new :none
5
+ PARSER['application/json'] = JSON.method(:parse) if defined? JSON
6
+ PARSER['text/csv'] = CSV.method(:parse) if defined? CSV
7
+ PARSER['text/html'] = Nokogiri::HTML.method(:parse) if defined? Nokogiri
8
+
9
+ def initialize(base = '',
10
+ type: :get,
11
+ data: {},
12
+ header: {},
13
+ dumper: DUMPER,
14
+ parser: PARSER,
15
+ &block)
16
+ @base, @type, @data, @header, @dumper, @parser, @block =
17
+ base, type, data, header, dumper, parser, block
18
+ @strict = false
101
19
  @webmethods = {}
102
20
  end
103
21
 
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
22
+ def strict!(t = true)
23
+ @strict = t
24
+ end
25
+
26
+ def add(method, target = method.to_s,
27
+ type: @type,
28
+ data: {},
29
+ header: {},
30
+ dumper: @dumper,
31
+ parser: @parser,
32
+ &block)
33
+ @webmethods[method] = WebApiMethod.new(@base+target,
34
+ type,
35
+ @data.merge(data),
36
+ @header.merge(header),
37
+ dumper,
38
+ parser,
39
+ &(block || @block))
40
+ end
41
+
42
+ def method_missing(symbol , extension = '',
43
+ type: nil,
44
+ data: {},
45
+ header: {},
46
+ dumper: nil,
47
+ parser: nil,
48
+ &block)
49
+ unless @webmethods.has_key? symbol
50
+ super if @strict
51
+ add symbol
126
52
  end
53
+ # user passed data as first argument?
54
+ extension, data = '', extension unless extension.is_a? String
55
+ @webmethods[symbol].run(extension, type, data, header, dumper, parser,
56
+ &block)
127
57
  end
128
-
129
58
  end
130
- end # WEB_API
@@ -0,0 +1,96 @@
1
+ class WebApiMethod
2
+ class ResponseError < ::RuntimeError
3
+ end
4
+ class UnsupportedMethodError < ::RuntimeError
5
+ end
6
+
7
+ OK = 200..299
8
+
9
+ # Escapes value's string representation for query string use.
10
+ def escape(value)
11
+ #http://rosettacode.org/wiki/URL_encoding#Ruby
12
+ CGI.escape(value.to_s).gsub("+", "%20")
13
+ end
14
+
15
+ def query_string(ah)
16
+ case ah
17
+ when Array
18
+ ah.select{!_1.empty?}.map{query_string _1}.join('&')
19
+ when Hash
20
+ ah.map{"#{_1}=#{escape _2}"}.join('&')
21
+ else
22
+ ah.to_s
23
+ end
24
+ end
25
+
26
+ def initialize(target, type, data, header, dumper, parser, &block)
27
+ @target, @type, @data, @header, @dumper, @parser, @block =
28
+ target, type, data, header, dumper, parser, block
29
+ end
30
+
31
+ def uri_parse(request)
32
+ uri = URI.parse(request.url)
33
+ request_uri = uri.request_uri
34
+ payload = ''
35
+ unless request.data.empty?
36
+ if request.type == :post
37
+ if dumper=request.dumper and (dumper!=:none)
38
+ payload = dumper.call(request.data)
39
+ else
40
+ payload = query_string(request.data)
41
+ end
42
+ else
43
+ payload = query_string(request.data)
44
+ request_uri += (uri.query ? '&' : '?') + payload
45
+ end
46
+ end
47
+ request.scheme, request.host, request.port, request.uri, request.payload =
48
+ uri.scheme, uri.host, uri.port, request_uri, payload
49
+ end
50
+
51
+ def run(extension, type, data, header, dumper, parser, &block)
52
+ request = CRStruct::Open.new
53
+ request.url = @target+extension
54
+ request.type = type || @type
55
+ request.data = @data.merge(data)
56
+ request.header = @header.merge(header)
57
+ request.dumper = dumper || @dumper
58
+
59
+ parser = parser || @parser
60
+ block = block || @block
61
+
62
+ uri_parse(request)
63
+ block.call(request) if block
64
+
65
+ body, content = http_response_body(request)
66
+ parser = parser[content] if parser.is_a? Hash
67
+ body = parser.call body if parser and parser!=:none
68
+ return body
69
+ end
70
+
71
+ def http_response_body(request)
72
+ response = http_response(request)
73
+ raise ResponseError, response.message unless OK === response.code.to_i
74
+ return response.body, response['content-type'].sub(/;.*$/,'')
75
+ end
76
+
77
+ def http_response(request)
78
+ http = net_http(request)
79
+ case request.type
80
+ when :get, :delete, :head, :copy, :move, :options, :trace
81
+ http.method(request.type).call request.uri, request.header
82
+ when :post, :patch, :lock, :unlock, :mkcol, :propfind, :proppatch
83
+ http.method(request.type)
84
+ .call request.uri, request.payload, request.header
85
+ else
86
+ raise UnsupportedMethodError,
87
+ "Method type #{type}(#{type.class}) not supported by WebApi."
88
+ end
89
+ end
90
+
91
+ def net_http(request)
92
+ http = Net::HTTP.new(request.host, request.port)
93
+ http.use_ssl = request.scheme=='https'
94
+ return http
95
+ end
96
+ end
data/lib/web_api.rb CHANGED
@@ -1,17 +1,15 @@
1
1
  # Standard Libraries
2
- require 'uri'
3
- require 'net/https'
4
- require 'base64'
2
+ require 'net/http'
3
+ require 'cgi'
5
4
 
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
- require 'web_api/auto.rb'
5
+ # External Gems
6
+ require 'crstruct'
15
7
 
8
+ # This Gem
9
+ class WebApi
10
+ VERSION = '1.0.210927'
11
+ require_relative 'web_api/web_api_method.rb'
12
+ require_relative 'web_api/web_api.rb'
13
+ end
16
14
  # Requires:
17
15
  #`ruby`
metadata CHANGED
@@ -1,136 +1,53 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.210927
5
5
  platform: ruby
6
6
  authors:
7
7
  - CarlosJHR64
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-05 00:00:00.000000000 Z
11
+ date: 2021-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: help_parser
14
+ name: crstruct
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.1'
19
+ version: '0.1'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 1.1.0
23
- type: :development
22
+ version: 0.1.210920
23
+ type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: '1.1'
29
+ version: '0.1'
30
30
  - - ">="
31
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.
32
+ version: 0.1.210920
33
+ description: 'Ruby library for web api''s.
95
34
 
96
- In Beta. Testing not done. Only GET and POST implemented.
35
+ '
97
36
  email: carlosjhr64@gmail.com
98
37
  executables: []
99
38
  extensions: []
100
- extra_rdoc_files:
101
- - README.rdoc
39
+ extra_rdoc_files: []
102
40
  files:
103
- - History.txt
104
- - Manifest.txt
105
- - README.rdoc
106
- - TODO.txt
107
- - examples/bitcoincharts
108
- - examples/blockexplorer
109
- - examples/mtgox1
110
- - examples/mtgox2
111
- - examples/twitter
112
- - examples/youtube
113
- - features/main.feature
114
- - features/step_definitions/main_steps.rb
41
+ - README.md
115
42
  - lib/web_api.rb
116
- - lib/web_api/auth.rb
117
- - lib/web_api/auto.rb
118
- - lib/web_api/client.rb
119
- - lib/web_api/signed.rb
120
- - lib/web_api/signed2.rb
121
- - lib/web_api/signet.rb
122
- - lib/web_api/version.rb
123
43
  - lib/web_api/web_api.rb
124
- - test/web_api.rb
125
- - web_api.gemspec
44
+ - lib/web_api/web_api_method.rb
126
45
  homepage: https://github.com/carlosjhr64/web_api
127
46
  licenses:
128
47
  - MIT
129
48
  metadata: {}
130
- post_install_message:
131
- rdoc_options:
132
- - "--main"
133
- - README.rdoc
49
+ post_install_message:
50
+ rdoc_options: []
134
51
  require_paths:
135
52
  - lib
136
53
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -144,10 +61,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
61
  - !ruby/object:Gem::Version
145
62
  version: '0'
146
63
  requirements:
147
- - 'ruby: ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]'
148
- rubyforge_project:
149
- rubygems_version: 2.2.0
150
- signing_key:
64
+ - 'ruby: ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]'
65
+ rubygems_version: 3.2.22
66
+ signing_key:
151
67
  specification_version: 4
152
68
  summary: Ruby library for web api's.
153
69
  test_files: []
data/History.txt DELETED
@@ -1,3 +0,0 @@
1
- === 0.0.0 / 2013-12-23 13:59:42 -0800
2
- === 0.0.0 / 2013-12-27 10:24:54 -0800
3
- b055a9d61efa02b85ed76a5ff0095125 pkg/web_api-0.0.0.gem
data/Manifest.txt DELETED
@@ -1,23 +0,0 @@
1
- History.txt
2
- Manifest.txt
3
- README.rdoc
4
- TODO.txt
5
- examples/bitcoincharts
6
- examples/blockexplorer
7
- examples/mtgox1
8
- examples/mtgox2
9
- examples/twitter
10
- examples/youtube
11
- features/main.feature
12
- features/step_definitions/main_steps.rb
13
- lib/web_api.rb
14
- lib/web_api/auth.rb
15
- lib/web_api/auto.rb
16
- lib/web_api/client.rb
17
- lib/web_api/signed.rb
18
- lib/web_api/signed2.rb
19
- lib/web_api/signet.rb
20
- lib/web_api/version.rb
21
- lib/web_api/web_api.rb
22
- test/web_api.rb
23
- web_api.gemspec
data/README.rdoc DELETED
@@ -1,57 +0,0 @@
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 is 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('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 DELETED
@@ -1,3 +0,0 @@
1
- == File List
2
-
3
-
@@ -1,78 +0,0 @@
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
@@ -1,16 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # This Gem:
3
- require 'web_api'
4
-
5
- BX = WEB_API::Auto.new('http://blockexplorer.com/q/')
6
- LJ = 55
7
-
8
- puts "*** BlockExplorer.com ***"
9
- print "Difficulty:\t"
10
- puts BX.getdifficulty
11
- print "Block Count:\t"
12
- puts BX.getblockcount
13
- # Etc... see http://blockexplorer.com/q/ for the API method list.
14
- # But wait,... can it do this??? (address is associated with WikiLeaks.)
15
- print "addressbalance('1HB5XMLmzFVj8ALj6mfBsbifRoD4miY36v'): ".ljust(LJ)
16
- puts BX.addressbalance('1HB5XMLmzFVj8ALj6mfBsbifRoD4miY36v')
data/examples/mtgox1 DELETED
@@ -1,13 +0,0 @@
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 DELETED
@@ -1,19 +0,0 @@
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 DELETED
@@ -1,35 +0,0 @@
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 DELETED
@@ -1,34 +0,0 @@
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
@@ -1,9 +0,0 @@
1
- @main
2
- Feature: Main Feature
3
-
4
- Background:
5
- * Given main feature
6
-
7
- Scenario:
8
- * When main feature
9
- * Then main feature
@@ -1,13 +0,0 @@
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 DELETED
@@ -1,28 +0,0 @@
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
data/lib/web_api/auto.rb DELETED
@@ -1,20 +0,0 @@
1
- module WEB_API
2
-
3
- class Auto < WebApi
4
-
5
- # base no longer optional
6
- def initialize(base)
7
- super(base)
8
- end
9
-
10
- def method_missing(symbol , *args)
11
- if @webmethods.has_key?(symbol)
12
- return @webmethods[symbol].call(args)
13
- end
14
- path = (args.length > 0)? (symbol.to_s + '/' + args.join('/')) : symbol.to_s
15
- uri = URI.parse _url(path)
16
- WebApiMethod.new(uri, :get).call([])
17
- end
18
-
19
- end
20
- end # WEB_API
@@ -1,27 +0,0 @@
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
@@ -1,31 +0,0 @@
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
@@ -1,31 +0,0 @@
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
@@ -1,33 +0,0 @@
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
@@ -1,3 +0,0 @@
1
- module WEB_API
2
- VERSION = '0.1.0'
3
- end
data/test/web_api.rb DELETED
@@ -1,7 +0,0 @@
1
- require 'test/unit'
2
- require 'web_api'
3
-
4
- class TestWebApi
5
- include WEB_API
6
- # ...
7
- end
data/web_api.gemspec DELETED
@@ -1,60 +0,0 @@
1
- Gem::Specification.new do |s|
2
-
3
- s.name = 'web_api'
4
- s.version = '0.1.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 = '2014-01-05'
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/blockexplorer
35
- examples/mtgox1
36
- examples/mtgox2
37
- examples/twitter
38
- examples/youtube
39
- features/main.feature
40
- features/step_definitions/main_steps.rb
41
- lib/web_api.rb
42
- lib/web_api/auth.rb
43
- lib/web_api/auto.rb
44
- lib/web_api/client.rb
45
- lib/web_api/signed.rb
46
- lib/web_api/signed2.rb
47
- lib/web_api/signet.rb
48
- lib/web_api/version.rb
49
- lib/web_api/web_api.rb
50
- test/web_api.rb
51
- web_api.gemspec
52
- )
53
-
54
- s.add_development_dependency 'help_parser', '~> 1.1', '>= 1.1.0'
55
- s.add_development_dependency 'oauth', '~> 0.4', '>= 0.4.7'
56
- s.add_development_dependency 'signet', '~> 0.5', '>= 0.5.0'
57
- s.add_development_dependency 'test-unit', '~> 2.5', '>= 2.5.5'
58
- s.requirements << 'ruby: ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]'
59
-
60
- end