paper-cup 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d26bd9a2d6d938e5548afdc7d6fc007351bce1aa
4
+ data.tar.gz: 72d254b6cecb88fc6218ad3dcdf125a01da5dfed
5
+ SHA512:
6
+ metadata.gz: c13a59518e7e3839d5945b40c7e4a33ac6d755afe4bf74c7adf19477ba90bae302b370e5134c3480080c49e2e7bb6ba7ab3c2d6129f782a3ab274cd077f2408c
7
+ data.tar.gz: 3425637ac57ea033ff2e7e113802bf858eee11d131a352425e2e4d76a966f2e3a48d277cdeb4c0e85a8a67e1738b726a5d40e29b21dafe94ac099bf883597ac0
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ \#*
2
+ *~
3
+ .#*
4
+ .DS_Store
5
+ .idea
6
+ .project
7
+ .sass-cache
8
+ coverage
9
+ Gemfile.lock
10
+ tmp
11
+ pkg
12
+ *.swp
13
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ Instalation
2
+ ----
3
+
4
+ Add `gem paper-cup` to your gemfile.
5
+
6
+ Configuration
7
+ -----
8
+
9
+ None!!! Yeaaaah!
10
+
11
+ Usage
12
+ -----
13
+
14
+ At the moment it only works for `POSTs`, `GETs` and `PUTs`. If the the response is a JSON the gem will parse it, if not it will show as it cames
15
+
16
+ GET
17
+ ----
18
+
19
+ `PaperCup.get(url: url)`
20
+
21
+ POST - PUT
22
+ ----------
23
+
24
+ `PaperCup.post(url: url, body: {name: 'pepe'}, headers: { "Content-Type" => "application/json"})`
25
+
26
+
27
+ Response
28
+ ---------
29
+ Each request will return a `Response` object.
30
+
31
+ Example:
32
+
33
+ ```ruby
34
+ r = PaperCup.post(url, some_hash.to_json)
35
+ puts r.response # It will print the response returned by the endpoint
36
+ puts r.status # It will print the status code returned by the endpoint
37
+ ```
@@ -0,0 +1,45 @@
1
+ require "open3"
2
+ require_relative 'response'
3
+
4
+ module PaperCup
5
+ class Request
6
+
7
+ attr_accessor :method, :url, :headers, :params, :body
8
+
9
+ COMMAND_TEMPLATE = "curl -X %{method} -w %%{http_code} %{headers} %{params} %{body} %{url}"
10
+
11
+ def initialize(method:, url:, headers: {}, params: {}, body: "")
12
+ @url, @method, @headers, @params, @body = url, method, headers, params, body
13
+ end
14
+
15
+ def exec
16
+ Response.new(Open3.capture3(command).first)
17
+ end
18
+
19
+ def command
20
+ COMMAND_TEMPLATE % {
21
+ method: method.upcase,
22
+ headers: build_headers,
23
+ params: build_params,
24
+ body: build_body,
25
+ url: url
26
+ }
27
+ end
28
+
29
+ def build_headers
30
+ @headers.map { |k, v| "-H '#{k}: #{v}'" }.join(" ")
31
+ end
32
+
33
+ def build_params
34
+ json_request? ? "-d '#{params.to_json}'" : "-d '#{params}'"
35
+ end
36
+
37
+ def json_request?
38
+ @headers["Content-Type"] && @headers["Content-Type"].include?("json")
39
+ end
40
+
41
+ def build_body
42
+ body.empty? ? "" : "-d '#{body}'"
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,27 @@
1
+ require 'json'
2
+ require_relative 'utils'
3
+
4
+ module PaperCup
5
+ class Response
6
+ attr_accessor :status, :response
7
+
8
+ def initialize(raw_response)
9
+ @raw_response = raw_response
10
+ end
11
+
12
+ def response
13
+ @response ||= begin
14
+ str = @raw_response[0..-4]
15
+ PaperCup.valid_json?(str) ? JSON.parse(str) : str
16
+ end
17
+ end
18
+
19
+ def status
20
+ @status ||= @raw_response[-3..-1].to_i
21
+ end
22
+
23
+ def success?
24
+ status.between?(200, 299)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,8 @@
1
+ module PaperCup
2
+ def self.valid_json?(json)
3
+ JSON.parse(json)
4
+ true
5
+ rescue
6
+ false
7
+ end
8
+ end
data/lib/paper_cup.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "./lib/paper_cup/request.rb"
2
+ module PaperCup
3
+
4
+ INSERT_METHODS = %w(post put)
5
+ QUERY_METHODS = %w(get head)
6
+ METHODS = INSERT_METHODS + QUERY_METHODS
7
+
8
+ METHODS.each do |method|
9
+ define_singleton_method(method) do |url, opts = {}|
10
+ Request.new(opts.merge(url: url, method: method)).exec
11
+ end
12
+ end
13
+ end
data/paper-cup.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ Gem::Specification.new do |s|
2
+ s.platform = Gem::Platform::RUBY
3
+ s.name = 'paper-cup'
4
+ s.version = '0.1.0'
5
+ s.date = '2015-08-05'
6
+ s.summary = "Use of Open3 to make request"
7
+ s.description = "Use curl with Open3 for making request"
8
+ s.authors = ["Mariano Matayoshi", "Ricardo Kleine Samson", "Nicolas Oga"]
9
+ s.email = 'matayoshi.mariano@gmail.com'
10
+ s.files = `git ls-files`.split("\n")
11
+ s.homepage = 'https://github.com/casapick/paper-cup'
12
+ s.license = 'MIT'
13
+ s.require_paths = ["lib"]
14
+
15
+ s.add_development_dependency "minitest", '~> 0'
16
+ s.add_development_dependency "minitest-happy", '~> 0'
17
+ s.add_development_dependency "pry-byebug", '~> 0'
18
+ end
@@ -0,0 +1,60 @@
1
+ require "test_helper"
2
+
3
+ describe PaperCup::Request do
4
+
5
+ describe "when the request is only url and method" do
6
+ it "must upcase the method for the command" do
7
+ r = PaperCup::Request.new(method: "get", url: "https://www.google.com.ar")
8
+ assert r.command.include?("-X GET"), "The command does not have the method upcased"
9
+ end
10
+
11
+ it "must add 'httt_code' to the command in order to get the status code" do
12
+ r = PaperCup::Request.new(method: "get", url: "https://www.google.com.ar")
13
+ assert r.command.include?("-w %{http_code}"), "The command does not include 'http_code'"
14
+ end
15
+ end
16
+
17
+ describe "when the request have headers" do
18
+ it "must add them to the command" do
19
+ r = PaperCup::Request.new(method: "get", url: "https://www.google.com.ar",
20
+ headers: { "Content-Type" => "application/json"})
21
+ assert r.command.include?("-H 'Content-Type: application/json'"), "The command does not have the method upcased"
22
+ end
23
+ end
24
+
25
+ describe "when the request\'s content type is json" do
26
+ it "the params must be json" do
27
+ params = { pepe: { holis: 3 }, pepa: 3 }
28
+ r = PaperCup::Request.new(method: "get", url: "https://www.google.com.ar",
29
+ params: params,
30
+ headers: { "Content-Type" => "application/json"})
31
+ assert r.command.include?(params.to_json), "The command does not have the params as a json"
32
+ end
33
+ end
34
+
35
+ describe "when the request\'s content type is not json" do
36
+ it "the params must be json" do
37
+ params = "<html><header></header><body><h1>HOLA</h1></body></html>"
38
+ r = PaperCup::Request.new(method: "get", url: "https://www.google.com.ar",
39
+ params: params,
40
+ headers: { "Content-Type" => "application/xml"})
41
+ assert r.command.include?(params), "The command does not have the params"
42
+ end
43
+ end
44
+
45
+ describe "when the request\'s have body" do
46
+ it "the body must be present" do
47
+ body = { total: 3.12 }.to_json
48
+ r = PaperCup::Request.new(method: "get", url: "https://www.google.com.ar",
49
+ body: body, headers: { "Content-Type" => "application/json"})
50
+ assert r.command.include?(body), "The command does not have the body"
51
+ end
52
+ end
53
+
54
+ describe "when the request\'s have no body" do
55
+ it "there must not be an empty body" do
56
+ r = PaperCup::Request.new(method: "get", url: "https://www.google.com.ar")
57
+ refute r.command.include?("-d ''"), "The command does have the body"
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,28 @@
1
+ require "test_helper"
2
+
3
+ describe PaperCup::Response do
4
+
5
+ describe "when the response is not JSON" do
6
+ it "must set the response as it came " do
7
+ assert_nothing_raised { PaperCup::Response.new(Mock.html) }
8
+ end
9
+
10
+ it "must set the status code response properly" do
11
+ r = PaperCup::Response.new(Mock.html)
12
+ assert_equal 404, r.status
13
+ end
14
+
15
+ end
16
+
17
+ describe "when the response is a JSON" do
18
+ it "must set the response properly" do
19
+ r = PaperCup::Response.new(Mock.json)
20
+ assert_equal JSON.parse(Mock.json[0..-4]), r.response
21
+ end
22
+
23
+ it "must set the status code response properly" do
24
+ r = PaperCup::Response.new(Mock.json)
25
+ assert_equal 200, r.status
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,23 @@
1
+ require "test_helper"
2
+
3
+ describe PaperCup do
4
+
5
+ describe "when doing a GET request to an url" do
6
+ describe "that respond with a JSON" do
7
+ it "must parse the response" do
8
+ Open3.stub :capture3, [Mock.json] do # stub goes away once the block is done
9
+ r = PaperCup.get("http://www.google.com")
10
+ assert_equal "Rails", r.response.first["framework"], "The framework must be rails"
11
+ end
12
+ end
13
+ end
14
+ describe "that does not respond with a JSON" do
15
+ it "must set the response as it come" do
16
+ Open3.stub :capture3, [Mock.html] do # stub goes away once the block is done
17
+ r = PaperCup.get("http://www.google.com")
18
+ assert_equal Mock.html, "#{r.response}#{r.status}", "The response must be the same"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,28 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require 'pry'
4
+ require './lib/paper_cup.rb'
5
+ Dir.glob(['./lib/**/*.rb']).each { |r| require r }
6
+
7
+ module Mock
8
+ class << self
9
+ def html
10
+ "<html>\n<head>\n<meta charset=\"UTF-8\">\n<title>404</title>\n</head>\n<body>\n<code>404</code>\n</body>\n</html>\n404"
11
+ end
12
+
13
+ def json
14
+ "[{\"framework\":\"Rails\",\"language\":\"ruby\"}]200"
15
+ end
16
+
17
+ def params
18
+ { framework: 'cuba', language: 'ruby' }
19
+ end
20
+ end
21
+ end
22
+
23
+ module Minitest::Assertions
24
+ def assert_nothing_raised(*)
25
+ yield
26
+ end
27
+ end
28
+
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paper-cup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mariano Matayoshi
8
+ - Ricardo Kleine Samson
9
+ - Nicolas Oga
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-08-05 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: minitest
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: minitest-happy
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: pry-byebug
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ description: Use curl with Open3 for making request
58
+ email: matayoshi.mariano@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - README.md
66
+ - lib/paper_cup.rb
67
+ - lib/paper_cup/request.rb
68
+ - lib/paper_cup/response.rb
69
+ - lib/paper_cup/utils.rb
70
+ - paper-cup.gemspec
71
+ - test/paper_cup_test.rb
72
+ - test/paper_cup_test/request_test.rb
73
+ - test/paper_cup_test/response_test.rb
74
+ - test/test_helper.rb
75
+ homepage: https://github.com/casapick/paper-cup
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.4.8
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Use of Open3 to make request
99
+ test_files: []
100
+ has_rdoc: