web_api 0.2.0 → 0.3.191208

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: d90f83d3561eb3dabacf2a133c9838ddc36a3526
4
- data.tar.gz: b601168876592bf00d4b429ea39c488a63b9d66b
2
+ SHA256:
3
+ metadata.gz: d807c131626709ce9df0f7e5fa92c96fd7a0df6868fc96a823be2ec823ef26aa
4
+ data.tar.gz: a9c6ecf0c3a73b2a4d4018eb7882054c6029c85cc3decf7bca28a452f38313ee
5
5
  SHA512:
6
- metadata.gz: 4a31e2c925cd78ae2f7b107dfe8cfe02f03ae11937e526271ffd012166e5af8386b874bec29f8b3d0821c90249e6d2a48105d4a08a1e79185ba40a43f60c5bd8
7
- data.tar.gz: 0d94488470805cc8998b42b3ffefff845071b19fbb710d28c41464d22123e4ef5f6dd16674cd7e13db853a4fa8f8fd6264824f432fe8d142718b1d61cb8eb102
6
+ metadata.gz: ef7a923eee894aa4f3346ccbc5a1b89601e58a17aac35e11821ed604ebc0c1354286466a3b4dbee9e5de78642bf646dc2e5fbb7319f5a06bd04d8c8df2609454
7
+ data.tar.gz: cd959864b8da19658c3a54afb46b0bdd39bffb5a24658be664a51cfa2eb85cbe1a6da0d16085be7c6eaf354461f654de69b460a3dfa39ffce4383d6ab06f6bf4
@@ -0,0 +1,89 @@
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
+ ## SYNOPSIS:
11
+
12
+ require 'web_api'
13
+ webapi = WEB_API::WebApi.new "https://api.site.org/path-to-base/", header: {Authorization: "Bearer ABC123XYZ"}
14
+ # for a post to https://api.site.org/path-to-base/resource...
15
+ webapi.add(:resource, type: :post)
16
+ # You can pass the post's (or query's) key value pairs in a hash.
17
+ body = webapi.resource(data: {key: "value"})
18
+
19
+ ## INSTALL:
20
+
21
+ $ gem install web_api
22
+
23
+ ## MORE:
24
+
25
+ There's not that much code here...
26
+ under 200 lines in `lib/**.rb` at the time of this writting.
27
+ Take a look at the examples given at [github](https://github.com/carlosjhr64/web_api/tree/master/examples)
28
+ for use cases.
29
+
30
+ The model is that at each step...
31
+
32
+ 1. instantiation of a WebApi object
33
+ 2. addition of a WebApi method
34
+ 3. call to a WebApi method
35
+
36
+ ...one builds up the url, type, data, and header of the http request.
37
+ The WebApi methods `#new`, `#add`, and `#<method>` all have the same argument signature:
38
+
39
+ extension String, type: Symbol, data: Hash, header: Hash, dumper: Proc, Parser: Proc|Hash(String, Proc)
40
+
41
+ The extension builds up the url by concatanation.
42
+ The data and headers hashes are built up with merge.
43
+ The type, dumper, and parser can be changed at each step.
44
+
45
+ One can read the code to check the minor nuances of each method's signature,
46
+ such as default values.
47
+
48
+ Note that `#add` will assume extension is the same as the name of the method if
49
+ no extenstion is given.
50
+
51
+ Note that `#<method>` will assume the user meant to pass data if it only gets a hash, but
52
+ the hash must then not have any `Symbol` for its keys:
53
+
54
+ #<method>({"a"=>"ABC","x"=>"XYZ"}) #=> #<method>('', data: {"a"=>"ABC","x"=>"XYZ"})
55
+
56
+ The dumper to dump the data in a post request is JSON.dump by default if JSON is available.
57
+
58
+ The parser to parse the body of an "application/json" type content is JSON.parse by default if available.
59
+ You can read the code and inspect `WEB_API::WebApi::PARSER` to see the other parsers available by default.
60
+
61
+ If one does not want to parse the reponse's body,
62
+ one can set `parser: :none`. For example:
63
+
64
+ body = webapi.resourse(data: {key: "value"}, parser: :none)
65
+
66
+ ## LICENSE:
67
+
68
+ (The MIT License)
69
+
70
+ Copyright (c) 2019 CarlosJHR64
71
+
72
+ Permission is hereby granted, free of charge, to any person obtaining
73
+ a copy of this software and associated documentation files (the
74
+ 'Software'), to deal in the Software without restriction, including
75
+ without limitation the rights to use, copy, modify, merge, publish,
76
+ distribute, sublicense, and/or sell copies of the Software, and to
77
+ permit persons to whom the Software is furnished to do so, subject to
78
+ the following conditions:
79
+
80
+ The above copyright notice and this permission notice shall be
81
+ included in all copies or substantial portions of the Software.
82
+
83
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
84
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
85
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
86
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
87
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
88
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
89
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,18 +1,16 @@
1
1
  # Standard Libraries
2
- require 'uri'
2
+ require 'net/http'
3
3
  require 'cgi'
4
- require 'net/https'
5
- require 'base64'
6
4
 
7
- # WEB_API
8
- require 'web_api/version.rb'
9
- require 'web_api/web_api.rb'
10
- require 'web_api/auth.rb'
11
- require 'web_api/signed.rb'
12
- require 'web_api/signed2.rb'
13
- require 'web_api/client.rb'
14
- require 'web_api/signet.rb'
15
- require 'web_api/auto.rb'
5
+ # External Gems
6
+ require 'crstruct'
7
+
8
+ # This Gem
9
+ module WEB_API
10
+ VERSION = '0.3.191208'
11
+ require 'web_api/web_api_method.rb'
12
+ require 'web_api/web_api.rb'
13
+ end
16
14
 
17
15
  # Requires:
18
16
  #`ruby`
@@ -1,131 +1,58 @@
1
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
- #http://rosettacode.org/wiki/URL_encoding#Ruby
22
- CGI.escape(value.to_s).gsub("+", "%20")
23
- end
24
-
25
- # kv is a key, value pair
26
- # [k, v]
27
- def kv_map(kv)
28
- kv.map{|value| escape(value)}.join('=')
29
- end
30
-
31
- # arg is a hash
32
- # {k=>v,...}
33
- def arg_map(arg)
34
- arg.map{|kv| kv_map(kv) }.join('&')
35
- end
36
-
37
- # args is an array of hashes
38
- # [ {k=>v,...}, {k=>v,...}, ...]
39
- # The net result of this is a flattened query string
40
- # "k=v&k=v..."
41
- def args_map(args)
42
- string = args.select{|arg| arg.length>0}.map{|arg| arg_map(arg)}.join('&')
43
- WebApi.trace.puts "args_map: #{string}" if WebApi.trace
44
- return string
45
- end
46
- alias data args_map
47
- alias query_string args_map
48
-
49
- def headers(args)
50
- headers = {'User-Agent' => "ruby gem web_api #{VERSION}"}
51
- WebApi.trace.puts "headers:\n#{headers}" if WebApi.trace
52
- return headers
53
- end
54
-
55
- OK = 200..299
56
- def parse(response)
57
- raise ResponseError, response.message unless OK === response.code.to_i
58
- response.body
59
- end
60
-
61
- SSL = {'http' => false, 'https' => true}
62
- def http
63
- http = Net::HTTP.new(@uri.host, @uri.port)
64
- http.use_ssl = SSL[@uri.scheme]
65
- return http
66
- end
67
-
68
- def post(args)
69
- parse http.post(@uri.path, data(args), headers(args))
70
- end
71
-
72
- def pathquery(args)
73
- p, q0, q1 = @uri.path, @uri.query, query_string(args)
74
- q = [q0, q1].select{|q| q && q.length>0}.join('&')
75
- pq = (q.length>0)? p + '?' + q : p
76
- WebApi.trace.puts "pathquery #{pq}" if WebApi.trace
77
- return pq
78
- end
79
-
80
- def get(args)
81
- parse http.get(pathquery(args), headers(args))
82
- end
83
-
84
- def call(args)
85
- self.method(@type).call(args)
86
- end
87
- end
88
-
89
- class WebApi
90
-
91
- @@trace = nil
92
- def self.trace
93
- @@trace
94
- end
95
- def self.trace=(trace)
96
- @@trace=trace
97
- end
98
-
99
- attr_reader :webmethods
100
- def initialize(base=nil)
101
- @base = base
102
- @webmethods = {}
103
- end
104
-
105
- def method_missing(symbol , *args)
106
- super unless @webmethods.has_key?(symbol)
107
- @webmethods[symbol].call(args)
108
- end
2
+ class WebApi
3
+ DUMPER = (defined? JSON)? JSON.method(:dump) : :none
4
+
5
+ PARSER = Hash.new :none
6
+ PARSER['application/json'] = JSON.method(:parse) if defined? JSON
7
+ PARSER['text/csv'] = CSV.method(:parse) if defined? CSV
8
+ PARSER['text/html'] = Nokogiri::HTML.method(:parse) if defined? Nokogiri
9
+
10
+ def initialize(base = '',
11
+ type: :get,
12
+ data: {},
13
+ header: {},
14
+ dumper: DUMPER,
15
+ parser: PARSER,
16
+ &block)
17
+ @base, @type, @data, @header, @dumper, @parser, @block =
18
+ base, type, data, header, dumper, parser, block
19
+ @strict = false
20
+ @webmethods = {}
21
+ end
109
22
 
110
- def _add(method, uri, type)
111
- @webmethods[method] = WebApiMethod.new(uri, type)
112
- end
23
+ def strict!(t = true)
24
+ @strict = t
25
+ end
113
26
 
114
- def _url(path)
115
- (@base)? File.join(@base, path) : path
116
- end
27
+ def add(method, target = method.to_s,
28
+ type: @type,
29
+ data: {},
30
+ header: {},
31
+ dumper: @dumper,
32
+ parser: @parser,
33
+ &block)
34
+ @webmethods[method] = WebApiMethod.new(@base+target,
35
+ type,
36
+ @data.merge(data),
37
+ @header.merge(header),
38
+ dumper,
39
+ parser,
40
+ &(block || @block))
41
+ end
117
42
 
118
- def add(method, path, type=:get)
119
- method = method.downcase.to_sym
120
- uri = URI.parse _url(path)
121
- type = type.downcase.to_sym
122
- raise "#{method} already defined" if @webmethods.include?(method)
123
- webmethod = _add(method, uri, type)
124
- if @base
125
- webmethod.base = @base
126
- webmethod.path = path
43
+ def method_missing(symbol , extension = '',
44
+ type: nil,
45
+ data: {},
46
+ header: {},
47
+ dumper: nil,
48
+ parser: nil,
49
+ &block)
50
+ unless @webmethods.has_key? symbol
51
+ super if @strict
52
+ add symbol
53
+ end
54
+ extension, data = '', extension unless extension.is_a? String # user passed data as first argument
55
+ @webmethods[symbol].run(extension, type, data, header, dumper, parser, &block)
127
56
  end
128
57
  end
129
-
130
58
  end
131
- end # WEB_API
@@ -0,0 +1,99 @@
1
+ module WEB_API
2
+ class WebApiMethod
3
+ class ResponseError < ::RuntimeError
4
+ end
5
+ class UnsupportedMethodError < ::RuntimeError
6
+ end
7
+
8
+ OK = 200..299
9
+
10
+ # Escapes value's string representation for query string use.
11
+ def escape(value)
12
+ #http://rosettacode.org/wiki/URL_encoding#Ruby
13
+ CGI.escape(value.to_s).gsub("+", "%20")
14
+ end
15
+
16
+ def query_string(ah)
17
+ case ah
18
+ when Array
19
+ ah.select{|h| !h.empty?}.map{|h| query_string(h)}.join('&')
20
+ when Hash
21
+ ah.map do |k,v|
22
+ "#{k}=#{escape v}"
23
+ end
24
+ .join('&')
25
+ else
26
+ ah.to_s
27
+ end
28
+ end
29
+
30
+ def initialize(target, type, data, header, dumper, parser, &block)
31
+ @target, @type, @data, @header, @dumper, @parser, @block =
32
+ target, type, data, header, dumper, parser, block
33
+ end
34
+
35
+ def uri_parse(request)
36
+ uri = URI.parse(request.url)
37
+ request_uri = uri.request_uri
38
+ payload = ''
39
+ unless request.data.empty?
40
+ if request.type == :post
41
+ if dumper=request.dumper and (dumper!=:none)
42
+ payload = dumper.call(request.data)
43
+ else
44
+ payload = query_string(request.data)
45
+ end
46
+ else
47
+ payload = query_string(request.data)
48
+ request_uri += (uri.query ? '&' : '?') + payload
49
+ end
50
+ end
51
+ request.scheme, request.host, request.port, request.uri, request.payload =
52
+ uri.scheme, uri.host, uri.port, request_uri, payload
53
+ end
54
+
55
+ def run(extension, type, data, header, dumper, parser, &block)
56
+ request = CRStruct::Open.new
57
+ request.url = @target+extension
58
+ request.type = type || @type
59
+ request.data = @data.merge(data)
60
+ request.header = @header.merge(header)
61
+ request.dumper = dumper || @dumper
62
+
63
+ parser = parser || @parser
64
+ block = block || @block
65
+
66
+ uri_parse(request)
67
+ block.call(request) if block
68
+
69
+ body, content = http_response_body(request)
70
+ parser = parser[content] if parser.is_a? Hash
71
+ body = parser.call body if parser and parser!=:none
72
+ return body
73
+ end
74
+
75
+ def http_response_body(request)
76
+ response = http_response(request)
77
+ raise ResponseError, response.message unless OK === response.code.to_i
78
+ return response.body, response['content-type'].sub(/;.*$/,'')
79
+ end
80
+
81
+ def http_response(request)
82
+ http = net_http(request)
83
+ case request.type
84
+ when :get, :delete, :head, :copy, :move, :options, :trace
85
+ http.method(request.type).call request.uri, request.header
86
+ when :post, :patch, :lock, :unlock, :mkcol, :propfind, :proppatch
87
+ http.method(request.type).call request.uri, request.payload, request.header
88
+ else
89
+ raise UnsupportedMethodError, "Method type #{type}(#{type.class}) not supported by WEB_API::WebApi."
90
+ end
91
+ end
92
+
93
+ def net_http(request)
94
+ http = Net::HTTP.new(request.host, request.port)
95
+ http.use_ssl = request.scheme=='https'
96
+ return http
97
+ end
98
+ end
99
+ end
metadata CHANGED
@@ -1,117 +1,47 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.191208
5
5
  platform: ruby
6
6
  authors:
7
7
  - carlosjhr64
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-04 00:00:00.000000000 Z
11
+ date: 2019-12-08 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.2'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 1.2.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.2'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 1.2.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
19
+ version: 0.1.191207
20
+ type: :runtime
44
21
  prerelease: false
45
22
  version_requirements: !ruby/object:Gem::Requirement
46
23
  requirements:
47
24
  - - "~>"
48
25
  - !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.6'
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- version: 0.6.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.6'
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- version: 0.6.0
73
- description: |
74
- Ruby library for web api's.
26
+ version: 0.1.191207
27
+ description: 'Ruby library for web api''s.
75
28
 
76
- In Beta. Testing not done. Only GET and POST implemented.
29
+ '
77
30
  email: carlosjhr64@gmail.com
78
31
  executables: []
79
32
  extensions: []
80
- extra_rdoc_files:
81
- - README.rdoc
33
+ extra_rdoc_files: []
82
34
  files:
83
- - History.txt
84
- - Manifest.txt
85
- - README.rdoc
86
- - TODO.txt
87
- - examples/bitcoincharts
88
- - examples/blockexplorer
89
- - examples/facebook
90
- - examples/mtgox1
91
- - examples/mtgox2
92
- - examples/twitter
93
- - examples/youtube
94
- - features/main.feature
95
- - features/step_definitions/main_steps.rb
35
+ - README.md
96
36
  - lib/web_api.rb
97
- - lib/web_api/auth.rb
98
- - lib/web_api/auto.rb
99
- - lib/web_api/client.rb
100
- - lib/web_api/signed.rb
101
- - lib/web_api/signed2.rb
102
- - lib/web_api/signet.rb
103
- - lib/web_api/version.rb
104
37
  - lib/web_api/web_api.rb
105
- - test/web_api.rb
106
- - web_api.gemspec
38
+ - lib/web_api/web_api_method.rb
107
39
  homepage: https://github.com/carlosjhr64/web_api
108
40
  licenses:
109
41
  - MIT
110
42
  metadata: {}
111
43
  post_install_message:
112
- rdoc_options:
113
- - "--main"
114
- - README.rdoc
44
+ rdoc_options: []
115
45
  require_paths:
116
46
  - lib
117
47
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -125,11 +55,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
55
  - !ruby/object:Gem::Version
126
56
  version: '0'
127
57
  requirements:
128
- - 'ruby: ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]'
129
- rubyforge_project:
130
- rubygems_version: 2.4.1
58
+ - 'ruby: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]'
59
+ rubygems_version: 3.0.3
131
60
  signing_key:
132
61
  specification_version: 4
133
62
  summary: Ruby library for web api's.
134
63
  test_files: []
135
- has_rdoc:
@@ -1,7 +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
4
- === 0.1.0 / 2014-01-05 07:33:21 -0800
5
- e5c7b647f2765c7d46fd8a135d00a754 pkg/web_api-0.1.0.gem
6
- === 0.1.1 / 2015-01-04 11:22:26 -0800
7
- 624450a90595702697c4cda7c2ca75c3 pkg/web_api-0.1.1.gem
@@ -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
@@ -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,15 +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
-
7
- puts "*** BlockExplorer.com ***"
8
- print "Difficulty:\t"
9
- puts BX.getdifficulty
10
- print "Block Count:\t"
11
- puts BX.getblockcount
12
- # Etc... see http://blockexplorer.com/q/ for the API method list.
13
- # But wait,... can it do this??? (address is associated with WikiLeaks.)
14
- print "addressbalance('1HB5XMLmzFVj8ALj6mfBsbifRoD4miY36v'): "
15
- puts BX.addressbalance('1HB5XMLmzFVj8ALj6mfBsbifRoD4miY36v')
@@ -1,36 +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
-
12
- puts "Now your API Secret:"
13
- print "Secret: "
14
- SECRET = STDIN.gets.strip
15
-
16
-
17
- SITE = "http://api.twitter.com"
18
- CONSUMER = OAuth::Consumer.new(KEY, SECRET, {:site => SITE})
19
- REQUEST_TOKEN = CONSUMER.get_request_token
20
-
21
-
22
- puts "Set your set your web browser this url:"
23
- puts " " + REQUEST_TOKEN.authorize_url
24
- puts "Then follow the instructions to authorize."
25
- puts "Twitter should give you a pin number."
26
- print "Pin: "
27
- PIN = STDIN.gets.strip
28
-
29
-
30
- ACCESS = REQUEST_TOKEN.get_access_token(:oauth_verifier => PIN)
31
-
32
- WEBAPI = WEB_API::Client.new(ACCESS)
33
- WEBAPI.add('timeline', 'https://api.twitter.com/1.1/statuses/home_timeline.json', 'get')
34
- RESPONSE = JSON.parse WEBAPI.timeline
35
-
36
- puts JSON.pretty_generate RESPONSE
@@ -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'})
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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,32 +0,0 @@
1
- module WEB_API
2
-
3
- class SignetMethod < ClientMethod
4
- def parse(response)
5
- raise ResponseError, response.body unless OK === response.status
6
- response.body
7
- end
8
-
9
- def fetch_protected_resource(url, headers=nil, type='GET', body=nil)
10
- options = {:uri => url, :method => type}
11
- options[:body] = body if body
12
- options[:headers] = headers if headers
13
- parse client.fetch_protected_resource(options)
14
- end
15
-
16
- def post(args)
17
- fetch_protected_resource(uri.to_s, headers(args), 'POST', data(args))
18
- end
19
-
20
- def get(args)
21
- url = "#{uri.scheme}://#{uri.host}#{pathquery(args)}"
22
- fetch_protected_resource(url, headers(args))
23
- end
24
- end
25
-
26
- class Signet < Client
27
- def _add(method, uri, type)
28
- webmethods[method] = SignetMethod.new(client, uri, type)
29
- end
30
- end
31
-
32
- end # WEB_API
@@ -1,3 +0,0 @@
1
- module WEB_API
2
- VERSION = '0.2.0'
3
- end
@@ -1,7 +0,0 @@
1
- require 'test/unit'
2
- require 'web_api'
3
-
4
- class TestWebApi
5
- include WEB_API
6
- # ...
7
- end
@@ -1,60 +0,0 @@
1
- Gem::Specification.new do |s|
2
-
3
- s.name = 'web_api'
4
- s.version = '0.2.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 = '2015-01-04'
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/facebook
36
- examples/mtgox1
37
- examples/mtgox2
38
- examples/twitter
39
- examples/youtube
40
- features/main.feature
41
- features/step_definitions/main_steps.rb
42
- lib/web_api.rb
43
- lib/web_api/auth.rb
44
- lib/web_api/auto.rb
45
- lib/web_api/client.rb
46
- lib/web_api/signed.rb
47
- lib/web_api/signed2.rb
48
- lib/web_api/signet.rb
49
- lib/web_api/version.rb
50
- lib/web_api/web_api.rb
51
- test/web_api.rb
52
- web_api.gemspec
53
- )
54
-
55
- s.add_development_dependency 'help_parser', '~> 1.2', '>= 1.2.0'
56
- s.add_development_dependency 'oauth', '~> 0.4', '>= 0.4.7'
57
- s.add_development_dependency 'signet', '~> 0.6', '>= 0.6.0'
58
- s.requirements << 'ruby: ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]'
59
-
60
- end