gorillapi 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e00828a6ad86bbed30b71d068dacf3c337b1d55
4
+ data.tar.gz: 9ea2749700b081aae453a019f630d91792e65d81
5
+ SHA512:
6
+ metadata.gz: a2f154b2b7e242d58fa45ed992d8abcafc7ca80693d51ea01f11cf1307aca27677a9d6c427645f7b324d3bc43da494eb59c68052b6eb59b66269feae8908561d
7
+ data.tar.gz: 3dd5383679c559a13e8116374dd44145fe167d00e0c83a7cb473c4edd40863e49040e0e4607823083a3646a69fb47af28ac6fa9b031acc288db2ca6418be431b
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Robert Evans
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,68 @@
1
+ # Gorilla Api
2
+
3
+ ### Usage in your Rails app
4
+
5
+ ````
6
+ require 'gorillapi'
7
+
8
+ module Api
9
+ extend Gorillapi
10
+ extend self
11
+
12
+ test_api url: 'staging.domain.com'
13
+
14
+ header header_name: 'codewranglers.gorillapi',
15
+ api_key: 'ABC123',
16
+ format: 'json',
17
+ version: 1
18
+
19
+ def ping
20
+ get "/api/v1/ping"
21
+ end
22
+
23
+ def add_task
24
+ post "/api/v1/tasks", params: { task: { name: 'Test Api' } }
25
+ end
26
+ end
27
+
28
+ Api.add_task # =>
29
+
30
+
31
+ Request Details: Add Task
32
+ ---------------------------------------------
33
+ Url: staging.domain.com
34
+ Request: POST
35
+ Endpoint: /api/v1/tasks
36
+ Content-Type application/json
37
+ Accept: application/vnd.codewranglers.gorillapi-v1+json
38
+ Api Token: ABC123
39
+ Params: { 'task' => { 'name' => 'Test Api' } }
40
+
41
+
42
+ cURL Request:
43
+ ---------------------------------------------
44
+ curl -X POST -k -i -v -H "Content-Type: application/json" -H "Accept: application/vnd.codewranglers.gorillapi-v1+json" -H "AUTHORIZATION: ABC123" http://staging.domain.com/api/v1/tasks
45
+
46
+
47
+ Api Call Results:
48
+ ---------------------------------------------
49
+ HTTP/1.1 201 Created
50
+ Server: nginx/1.4.7
51
+ Date: Wed, 23 Apr 2014 23:33:42 GMT
52
+ Content-Type: application/json; charset=utf-8
53
+ Transfer-Encoding: chunked
54
+ Connection: keep-alive
55
+ Status: 201 Created
56
+ X-Frame-Options: SAMEORIGIN
57
+ X-XSS-Protection: 1; mode=block
58
+ X-Content-Type-Options: nosniff
59
+ Vary: Accept-Encoding
60
+ ETag: "260ca9dd8a4577fc00b7bd5810298076"
61
+ Cache-Control: max-age=0, private, must-revalidate
62
+ Set-Cookie: request_method=POST; path=/
63
+ X-Request-Id: 75af1009-cd70-4304-9b22-0d7e263ca161
64
+ X-Runtime: 0.026264
65
+
66
+ success
67
+ ````
68
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gorillapi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gorillapi"
8
+ spec.version = Gorillapi::VERSION
9
+ spec.authors = ["Robert Evans"]
10
+ spec.email = ["robert@codewranglers.org"]
11
+ spec.summary = %q{A simple interface to test an Api}
12
+ spec.description = %q{A simple interface to test an Api}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+
25
+ spec.add_dependency 'activesupport'
26
+ end
@@ -0,0 +1,24 @@
1
+ require 'active_support/inflector'
2
+ require 'json'
3
+
4
+ require_relative "gorillapi/version"
5
+
6
+ require_relative "gorillapi/curl_builder"
7
+ require_relative 'gorillapi/request_builder'
8
+ require_relative 'gorillapi/request_methods'
9
+
10
+ module Gorillapi
11
+ include RequestMethods
12
+
13
+ def test_api(url: '')
14
+ self.class_eval("def url;'#{url}';end")
15
+ end
16
+
17
+ def header(header_name: '', api_key: '', format: 'json', version: 1)
18
+ self.class_eval("def content_type;'application/#{format}';end")
19
+ self.class_eval("def format;'#{format}';end")
20
+ self.class_eval("def version;'#{version}';end")
21
+ self.class_eval("def token;'#{api_key}';end")
22
+ self.class_eval("def accept_header; 'application/vnd.#{header_name}-v#{version}+#{format}'; end")
23
+ end
24
+ end
@@ -0,0 +1,30 @@
1
+
2
+ module Gorillapi
3
+ module CurlBuilder
4
+ def request_string
5
+ <<-EOF
6
+ curl -X #{request_method} -k -i -v #{content_header} #{accept} #{authorization} #{add_params} #{host}
7
+ EOF
8
+ end
9
+
10
+ def authorization
11
+ "-H \"AUTHORIZATION: #{token}\""
12
+ end
13
+
14
+ def accept
15
+ "-H \"Accept: #{accept_header}\""
16
+ end
17
+
18
+ def content_header
19
+ "-H \"Content-Type: #{content_type}\""
20
+ end
21
+
22
+ def add_params
23
+ "-d '#{params.to_json}'" unless params.nil? || params.empty? || params == ''
24
+ end
25
+
26
+ def host
27
+ "http://#{url}#{path}"
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ module Gorillapi
2
+ class RequestBuilder
3
+ include CurlBuilder
4
+
5
+ attr_reader :request_method, :path, :params, :url, :content_type, :accept_header, :token
6
+
7
+ def initialize(options = {})
8
+ options.each do |key, value|
9
+ instance_variable_set("@#{key}", value) unless value.nil? || value.empty?
10
+ end
11
+ end
12
+
13
+ def send
14
+ `#{request_string}`
15
+ end
16
+
17
+ def print
18
+ request_string
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,124 @@
1
+
2
+ module Gorillapi
3
+ module PrintResponse
4
+ def print_request(builder, print_only)
5
+
6
+ case print_only
7
+ when 'curl'
8
+ show_curl_request(builder)
9
+ when 'details'
10
+ request_details
11
+ else
12
+ request_details
13
+ show_curl_request(builder)
14
+ end
15
+
16
+ true
17
+ end
18
+
19
+ def request_details
20
+ puts
21
+ puts "-" * 100
22
+ puts
23
+ puts "Request Details: #{@caller}"
24
+ puts "-" * 100
25
+ puts "Url: #{url}"
26
+ puts "Request: #{@request_method}"
27
+ puts "Endpoint: #{@path}"
28
+ puts "Content-Type #{content_type}"
29
+ puts "Accept: #{accept_header}"
30
+ puts "Api Token: #{token}"
31
+ puts "Params: #{@params}"
32
+ puts
33
+ end
34
+
35
+ def print_results(builder, results)
36
+ request_details
37
+ show_curl_request(builder)
38
+
39
+ puts
40
+ puts "Api Call Results:"
41
+ puts "-" * 100
42
+ puts results
43
+ puts
44
+ end
45
+
46
+ def show_curl_request(builder)
47
+ puts
48
+ puts "cURL Request:"
49
+ puts "-" * 100
50
+ puts builder.print
51
+ puts
52
+ end
53
+ end
54
+
55
+ module RequestMethods
56
+ include PrintResponse
57
+
58
+ def handle_print(builder, print_only = false)
59
+ if print_only
60
+ print_request(builder, print_only)
61
+ else
62
+ results = builder.send
63
+ print_results(builder, results)
64
+ end
65
+ end
66
+
67
+
68
+ def get(path, print_only = false, params: {})
69
+ @caller = caller[0][/`.*'/][1..-2].humanize.titleize
70
+ @request_method = 'GET'
71
+ @path = path
72
+ @params = params
73
+
74
+ builder = build_request
75
+ handle_print(builder, print_only)
76
+ end
77
+
78
+
79
+ def post(path, print_only = false, params: {})
80
+ @caller = caller[0][/`.*'/][1..-2].humanize.titleize
81
+ @request_method = 'POST'
82
+ @path = path
83
+ @params = params
84
+
85
+ builder = build_request
86
+ handle_print(builder, print_only)
87
+ end
88
+
89
+
90
+ def put(path, print_only = false, params: {})
91
+ @caller = caller[0][/`.*'/][1..-2].humanize.titleize
92
+ @request_method = 'PUT'
93
+ @path = path
94
+ @params = params
95
+
96
+ builder = build_request
97
+ handle_print(builder, print_only)
98
+ end
99
+
100
+ def delete(path, print_only = false, params: {})
101
+ @caller = caller[0][/`.*'/][1..-2].humanize.titleize
102
+ @request_method = 'DELETE'
103
+ @path = path
104
+ @params = params
105
+
106
+ builder = build_request
107
+ handle_print(builder, print_only)
108
+ end
109
+
110
+ def build_request
111
+ options = {
112
+ request_method: @request_method,
113
+ url: url,
114
+ path: @path,
115
+ params: @params,
116
+ content_type: content_type,
117
+ accept_header: accept_header,
118
+ token: token,
119
+ }
120
+
121
+ RequestBuilder.new(options)
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,3 @@
1
+ module Gorillapi
2
+ VERSION = "0.0.11"
3
+ end
@@ -0,0 +1,44 @@
1
+ require 'test_helper'
2
+
3
+ class GorillapiTest < Minitest::Test
4
+
5
+ def test_api_settings
6
+ assert_equal 'test.api', Gorilla.url
7
+ assert_equal "123abc", Gorilla.token
8
+ assert_equal "json", Gorilla.format
9
+ assert_equal '1', Gorilla.version
10
+ assert_equal 'application/json', Gorilla.content_type
11
+
12
+ assert_equal "application/vnd.gorilla_header-v1+json",
13
+ Gorilla.accept_header
14
+ end
15
+
16
+
17
+ def test_get_with_print
18
+ assert Gorilla.ping(true)
19
+ assert Gorilla.ping('curl')
20
+ assert Gorilla.ping('details')
21
+ end
22
+
23
+ def test_get_without_print
24
+ # assert Gorilla.user
25
+ end
26
+
27
+ def test_post_with_print
28
+ assert Gorilla.create_post(true)
29
+ assert Gorilla.create_post('curl')
30
+ assert Gorilla.create_post('details')
31
+ end
32
+
33
+ def test_put_with_print
34
+ assert Gorilla.update_post(true)
35
+ assert Gorilla.update_post('curl')
36
+ assert Gorilla.update_post('details')
37
+ end
38
+
39
+ def test_delete_with_print
40
+ assert Gorilla.delete_post(true)
41
+ assert Gorilla.delete_post('curl')
42
+ assert Gorilla.delete_post('details')
43
+ end
44
+ end
@@ -0,0 +1,43 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+
4
+ require_relative '../lib/gorillapi'
5
+
6
+ module Gorilla
7
+ extend self, Gorillapi
8
+
9
+ test_api url: 'test.api'
10
+ header header_name: 'gorilla_header',
11
+ api_key: '123abc',
12
+ format: 'json',
13
+ version: 1
14
+
15
+ def ping(print_only = false)
16
+ get "/api/v1/ping", print_only
17
+ end
18
+
19
+ def user
20
+ get "/api/v1/user", params: { id: 1 }
21
+ end
22
+
23
+ def create_post(print_only = false)
24
+ post "/api/v1/post", print_only, params: {
25
+ title: 'My Post',
26
+ content: 'Lorem ipsum....',
27
+ }
28
+ end
29
+
30
+ def update_post(print_only = false)
31
+ put "/api/v1/post", print_only, params: {
32
+ id: 1,
33
+ title: 'My Post',
34
+ content: 'Lorem ipsum....',
35
+ }
36
+ end
37
+
38
+ def delete_post(print_only = false)
39
+ delete "/api/v1/post", print_only, params: {
40
+ id: 1
41
+ }
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gorillapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.11
5
+ platform: ruby
6
+ authors:
7
+ - Robert Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A simple interface to test an Api
70
+ email:
71
+ - robert@codewranglers.org
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - gorillapi.gemspec
82
+ - lib/gorillapi.rb
83
+ - lib/gorillapi/curl_builder.rb
84
+ - lib/gorillapi/request_builder.rb
85
+ - lib/gorillapi/request_methods.rb
86
+ - lib/gorillapi/version.rb
87
+ - test/gorillapi_test.rb
88
+ - test/test_helper.rb
89
+ homepage: ''
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A simple interface to test an Api
113
+ test_files:
114
+ - test/gorillapi_test.rb
115
+ - test/test_helper.rb