Skip to content

Commit

Permalink
get, post, put for users
Browse files Browse the repository at this point in the history
  • Loading branch information
lorainegarutti committed Jul 6, 2021
0 parents commit 9e904db
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'faker'
gem 'httparty'
gem 'rspec'
gem 'rubocop'
61 changes: 61 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
concurrent-ruby (1.1.9)
diff-lcs (1.4.4)
faker (2.18.0)
i18n (>= 1.6, < 2)
httparty (0.18.1)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2021.0704)
multi_xml (0.6.0)
parallel (1.20.1)
parser (3.0.1.1)
ast (~> 2.4.1)
rainbow (3.0.0)
regexp_parser (2.1.1)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
rubocop (1.18.3)
parallel (~> 1.10)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.7.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.7.0)
parser (>= 3.0.1.1)
ruby-progressbar (1.11.0)
unicode-display_width (2.0.0)

PLATFORMS
x86_64-darwin-19

DEPENDENCIES
faker
httparty
rspec
rubocop

BUNDLED WITH
2.2.21
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### This repository uses:

- https://github.com/jnunemaker/httparty/
- https://github.com/rspec/rspec
- https://github.com/serverest/serverest


### Documentation:

- https://www.rubydoc.info/github/jnunemaker/httparty/HTTParty/ClassMethods
- https://www.rubydoc.info/github/jnunemaker/httparty/HTTParty/Response
- https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
7 changes: 7 additions & 0 deletions services/user_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module Request
include HTTParty
base_uri 'https://serverest.dev'
format :json
end
34 changes: 34 additions & 0 deletions spec/get_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

describe 'getting information' do
it 'get users' do
@users = Request.get('/usuarios')
expect(@users.code).to eq(200)
puts @users
# expect(@users['usuarios'][0]['nome']).to eq('Fulano da Silva')
end
end

# # Or wrap things up in your own class
# class StackExchange
# include HTTParty
# base_uri 'api.stackexchange.com'

# def initialize(service, page)
# @options = { query: { site: service, page: page } }
# end

# def questions
# self.class.get("/2.2/questions", @options)
# end

# def users
# self.class.get("/2.2/users", @options)
# end
# end

# stack_exchange = StackExchange.new("stackoverflow", 1)
# puts stack_exchange.questions
# puts stack_exchange.users

# httparty "https://api.stackexchange.com/2.2/questions?site=stackoverflow" --format json (--help)
19 changes: 19 additions & 0 deletions spec/post_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

describe 'posting information' do
it 'register user' do
@new_user = {
'nome' => Faker::Name.name,
'email' => Faker::Internet.email,
'password' => Faker::Internet.password,
'administrador' => Faker::Boolean.boolean
}
# puts @new_user

@request = Request.post('/usuarios', body: @new_user)
expect(@request.code).to eq(201)
expect(@request['_id']).not_to be nil
puts @id_user = @request['_id']
expect(@request['message']).to eq('Cadastro realizado com sucesso')
end
end
18 changes: 18 additions & 0 deletions spec/put_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

describe 'changing information' do
it 'modify user information' do
@new_user = {
'nome' => Faker::Name.name,
'email' => Faker::Internet.email,
'password' => Faker::Internet.password,
'administrador' => Faker::Boolean.boolean
}
# puts @new_user

@id_user = 'DE7RTKT8SWmcIc3e'
@request = Request.put(%(/usuarios/#{@id_user}), body: @new_user)
expect(@request.code).to eq(200)
expect(@request['message']).to eq('Registro alterado com sucesso')
end
end
36 changes: 36 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require 'faker'
require 'httparty'
require 'rubocop'

require_relative '../services/user_service'

RSpec.configure do |config|
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end

# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
# have no way to turn it off -- the option exists only for backwards
# compatibility in RSpec 3). It causes shared context metadata to be
# inherited by the metadata hash of host groups and examples, rather than
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups
end

0 comments on commit 9e904db

Please sign in to comment.