web_client 0.0.1
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.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/web_client/base.rb +38 -0
- data/lib/web_client/resource.rb +31 -0
- data/lib/web_client/version.rb +3 -0
- data/lib/web_client.rb +7 -0
- data/spec/base_spec.rb +77 -0
- data/spec/resource_spec.rb +83 -0
- data/spec/spec_helper.rb +3 -0
- data/web_client.gemspec +20 -0
- metadata +79 -0
    
        data/Gemfile
    ADDED
    
    
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,29 @@ | |
| 1 | 
            +
            # WebClient
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Net::HTTP wrapper easy to use
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            ## Installation
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            Add this line to your application's Gemfile:
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                gem 'web_client'
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            And then execute:
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                $ bundle
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            Or install it yourself as:
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                $ gem install web_client
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            ## Usage
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            TODO: Write usage instructions here
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            ## Contributing
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            1. Fork it
         | 
| 26 | 
            +
            2. Create your feature branch (`git checkout -b my-new-feature`)
         | 
| 27 | 
            +
            3. Commit your changes (`git commit -am 'Added some feature'`)
         | 
| 28 | 
            +
            4. Push to the branch (`git push origin my-new-feature`)
         | 
| 29 | 
            +
            5. Create new Pull Request
         | 
    
        data/Rakefile
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            require "bundler/gem_tasks"
         | 
| @@ -0,0 +1,38 @@ | |
| 1 | 
            +
            module WebClient
         | 
| 2 | 
            +
              class Base
         | 
| 3 | 
            +
                include Net
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                attr_reader :http
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                def initialize(host, port=nil)
         | 
| 8 | 
            +
                  @http = HTTP.new(host, port)
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                def get(path, data={}, &block)
         | 
| 12 | 
            +
                  request(HTTP::Get, path, data, &block)
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                def post(path, data, &block)
         | 
| 16 | 
            +
                  request(HTTP::Post, path, data, &block)
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                def put(path, data, &block)
         | 
| 20 | 
            +
                  request(HTTP::Put, path, data, &block)
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                def delete(path, data={}, &block)
         | 
| 24 | 
            +
                  request(HTTP::Delete, path, data, &block)
         | 
| 25 | 
            +
                end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                private
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                def request(method_class, path, data=nil)
         | 
| 30 | 
            +
                  request = method_class.new(path)
         | 
| 31 | 
            +
                  request.set_form_data(data) if data.is_a? Hash
         | 
| 32 | 
            +
                  request.body = data if data.is_a? String
         | 
| 33 | 
            +
                  yield(request, http) if block_given?
         | 
| 34 | 
            +
                  http.request(request)
         | 
| 35 | 
            +
                end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
              end
         | 
| 38 | 
            +
            end
         | 
| @@ -0,0 +1,31 @@ | |
| 1 | 
            +
            module WebClient
         | 
| 2 | 
            +
              class Resource < Base
         | 
| 3 | 
            +
                attr_reader :resource
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                def initialize(resource, *args)
         | 
| 6 | 
            +
                  @resource = resource
         | 
| 7 | 
            +
                  super(*args)
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                def index(&block)
         | 
| 11 | 
            +
                  get("/#{resource}.json", &block)
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                def show(id, &block)
         | 
| 15 | 
            +
                  get("/#{resource}/#{id}.json", &block)
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                def create(data, &block)
         | 
| 19 | 
            +
                  post("/#{resource}.json", data, &block)
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                def update(id, data, &block)
         | 
| 23 | 
            +
                  put("/#{resource}/#{id}.json", data, &block)
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                def destroy(id, &block)
         | 
| 27 | 
            +
                  delete("/#{resource}/#{id}.json", &block)
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
            end
         | 
    
        data/lib/web_client.rb
    ADDED
    
    
    
        data/spec/base_spec.rb
    ADDED
    
    | @@ -0,0 +1,77 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            HOST = 'www.mock.dev'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            describe WebClient::Base do
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              let(:client) { WebClient::Base.new(HOST) }
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              context 'HTTP methods' do
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                it 'get' do
         | 
| 12 | 
            +
                  stub_request(:get, "#{HOST}/get_stub").to_return(body: 'content')
         | 
| 13 | 
            +
                  response = client.get('/get_stub')
         | 
| 14 | 
            +
                  response.should be_a Net::HTTPOK
         | 
| 15 | 
            +
                  response.body.should eq 'content'
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                it 'post' do
         | 
| 19 | 
            +
                  stub_request(:post, "#{HOST}/post_stub").with(body: 'form_data')
         | 
| 20 | 
            +
                  response = client.post('/post_stub', 'form_data')
         | 
| 21 | 
            +
                  response.should be_a Net::HTTPOK
         | 
| 22 | 
            +
                  response.body.should be_nil
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                it 'put' do
         | 
| 26 | 
            +
                  stub_request(:put, "#{HOST}/put_stub").with(body: 'form_data')
         | 
| 27 | 
            +
                  response = client.put('/put_stub', 'form_data')
         | 
| 28 | 
            +
                  response.should be_a Net::HTTPOK
         | 
| 29 | 
            +
                  response.body.should be_nil
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                it 'delete' do
         | 
| 33 | 
            +
                  stub_request(:delete, "#{HOST}/delete_stub").with(body: 'form_data')
         | 
| 34 | 
            +
                  response = client.delete('/delete_stub', 'form_data')
         | 
| 35 | 
            +
                  response.should be_a Net::HTTPOK
         | 
| 36 | 
            +
                  response.body.should be_nil
         | 
| 37 | 
            +
                end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
              end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
              context 'Actions with a block' do
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                it 'get' do
         | 
| 44 | 
            +
                  stub_request(:get, /.*/)
         | 
| 45 | 
            +
                  client.get('/get_stub') do |request, http|
         | 
| 46 | 
            +
                    request.path.should eq '/get_stub'
         | 
| 47 | 
            +
                    http.address.should eq HOST
         | 
| 48 | 
            +
                  end
         | 
| 49 | 
            +
                end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                it 'post' do
         | 
| 52 | 
            +
                  stub_request(:post, /.*/)
         | 
| 53 | 
            +
                  client.post('/post_stub', {}) do |request, http|
         | 
| 54 | 
            +
                    request.path.should eq '/post_stub'
         | 
| 55 | 
            +
                    http.address.should eq HOST
         | 
| 56 | 
            +
                  end
         | 
| 57 | 
            +
                end
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                it 'put' do
         | 
| 60 | 
            +
                  stub_request(:put, /.*/)
         | 
| 61 | 
            +
                  client.put('/put_stub', {}) do |request, http|
         | 
| 62 | 
            +
                    request.path.should eq '/put_stub'
         | 
| 63 | 
            +
                    http.address.should eq HOST
         | 
| 64 | 
            +
                  end
         | 
| 65 | 
            +
                end
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                it 'delete' do
         | 
| 68 | 
            +
                  stub_request(:delete, /.*/)
         | 
| 69 | 
            +
                  client.delete('/delete_stub') do |request, http|
         | 
| 70 | 
            +
                    request.path.should eq '/delete_stub'
         | 
| 71 | 
            +
                    http.address.should eq HOST
         | 
| 72 | 
            +
                  end
         | 
| 73 | 
            +
                end
         | 
| 74 | 
            +
             | 
| 75 | 
            +
              end
         | 
| 76 | 
            +
             | 
| 77 | 
            +
            end
         | 
| @@ -0,0 +1,83 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
             | 
| 4 | 
            +
            describe WebClient::Resource do
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              let(:resource) { WebClient::Resource.new(:users, 'localhost', 3000) }
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              it 'index' do
         | 
| 9 | 
            +
                content = '[{"email":"jperez@mail.com","first_name":"Juan","id":1,"last_name":"Perez","organization":"Test"}]'
         | 
| 10 | 
            +
                stub_request(:get, "http://localhost:3000/users.json").to_return(body: content)
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                response = resource.index
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                response.should be_a Net::HTTPOK
         | 
| 15 | 
            +
                data = JSON.parse(response.body)
         | 
| 16 | 
            +
                data.should be_a Array
         | 
| 17 | 
            +
                data.first['id'].should_not be_nil
         | 
| 18 | 
            +
                data.first['first_name'].should_not be_nil
         | 
| 19 | 
            +
                data.first['last_name'].should_not be_nil
         | 
| 20 | 
            +
                data.first['email'].should_not be_nil
         | 
| 21 | 
            +
                data.first['organization'].should_not be_nil
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
              it 'show' do
         | 
| 25 | 
            +
                content = '{"email":"jperez@mail.com","first_name":"Juan","id":1,"last_name":"Perez","organization":"Test"}'
         | 
| 26 | 
            +
                stub_request(:get, "http://localhost:3000/users/1.json").to_return(body: content)
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                response = resource.show(1)
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                response.should be_a Net::HTTPOK
         | 
| 31 | 
            +
                data = JSON.parse(response.body)
         | 
| 32 | 
            +
                data['id'].should_not be_nil
         | 
| 33 | 
            +
                data['first_name'].should eq 'Juan'
         | 
| 34 | 
            +
                data['last_name'].should eq 'Perez'
         | 
| 35 | 
            +
                data['email'].should eq 'jperez@mail.com'
         | 
| 36 | 
            +
                data['organization'].should eq 'Test'
         | 
| 37 | 
            +
              end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
              it 'create' do
         | 
| 40 | 
            +
                content = '{"email":"jperez@mail.com","first_name":"Juan","id":1,"last_name":"Perez","organization":"Test"}'
         | 
| 41 | 
            +
                stub_request(:post, "http://localhost:3000/users.json").to_return(body: content, status: 201)
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                params = {
         | 
| 44 | 
            +
                    'user[first_name]' => 'Juan',
         | 
| 45 | 
            +
                    'user[last_name]' => 'Perez',
         | 
| 46 | 
            +
                    'user[email]' => 'jperez@mail.com',
         | 
| 47 | 
            +
                    'user[organization]' => 'Test'
         | 
| 48 | 
            +
                }
         | 
| 49 | 
            +
                response = resource.create(params)
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                response.should be_a Net::HTTPCreated
         | 
| 52 | 
            +
                data = JSON.parse(response.body)
         | 
| 53 | 
            +
                data['id'].should_not be_nil
         | 
| 54 | 
            +
                data['first_name'].should eq 'Juan'
         | 
| 55 | 
            +
                data['last_name'].should eq 'Perez'
         | 
| 56 | 
            +
                data['email'].should eq 'jperez@mail.com'
         | 
| 57 | 
            +
                data['organization'].should eq 'Test'
         | 
| 58 | 
            +
              end
         | 
| 59 | 
            +
             | 
| 60 | 
            +
              it 'update' do
         | 
| 61 | 
            +
                stub_request(:put, "http://localhost:3000/users/1.json").to_return(status: 204)
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                params = {
         | 
| 64 | 
            +
                    'user[first_name]' => 'Juan',
         | 
| 65 | 
            +
                    'user[last_name]' => 'Perez',
         | 
| 66 | 
            +
                    'user[email]' => 'jperez@mail.com',
         | 
| 67 | 
            +
                    'user[organization]' => 'Test'
         | 
| 68 | 
            +
                }
         | 
| 69 | 
            +
                response = resource.update(1, params)
         | 
| 70 | 
            +
             | 
| 71 | 
            +
                response.should be_a Net::HTTPNoContent
         | 
| 72 | 
            +
                response.body.should be_nil
         | 
| 73 | 
            +
              end
         | 
| 74 | 
            +
             | 
| 75 | 
            +
              it 'destroy' do
         | 
| 76 | 
            +
                stub_request(:delete, "http://localhost:3000/users/1.json").to_return(status: 204)
         | 
| 77 | 
            +
             | 
| 78 | 
            +
                response = resource.destroy(1)
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                response.should be_a Net::HTTPNoContent
         | 
| 81 | 
            +
                response.body.should be_nil
         | 
| 82 | 
            +
              end
         | 
| 83 | 
            +
            end
         | 
    
        data/spec/spec_helper.rb
    ADDED
    
    
    
        data/web_client.gemspec
    ADDED
    
    | @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            # -*- encoding: utf-8 -*-
         | 
| 2 | 
            +
            $:.push File.expand_path("../lib", __FILE__)
         | 
| 3 | 
            +
            require "web_client/version"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Gem::Specification.new do |s|
         | 
| 6 | 
            +
              s.name         = 'web_client'
         | 
| 7 | 
            +
              s.version      = WebClient::VERSION
         | 
| 8 | 
            +
              s.authors      = ['Gabriel Naiman']
         | 
| 9 | 
            +
              s.email        = ['gabynaiman@gmail.com']
         | 
| 10 | 
            +
              s.homepage     = 'https://github.com/gabynaiman/web_client'
         | 
| 11 | 
            +
              s.summary      = %q{Net::HTTP wrapper easy to use}
         | 
| 12 | 
            +
              s.description  = %q{Net::HTTP wrapper easy to use}
         | 
| 13 | 
            +
              s.files         = `git ls-files`.split("\n")
         | 
| 14 | 
            +
              s.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
         | 
| 15 | 
            +
              s.executables   = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
         | 
| 16 | 
            +
              s.require_paths = ["lib"]
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              s.add_development_dependency 'rspec'
         | 
| 19 | 
            +
              s.add_development_dependency 'webmock'
         | 
| 20 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,79 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: web_client
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.0.1
         | 
| 5 | 
            +
              prerelease: 
         | 
| 6 | 
            +
            platform: ruby
         | 
| 7 | 
            +
            authors:
         | 
| 8 | 
            +
            - Gabriel Naiman
         | 
| 9 | 
            +
            autorequire: 
         | 
| 10 | 
            +
            bindir: bin
         | 
| 11 | 
            +
            cert_chain: []
         | 
| 12 | 
            +
            date: 2012-08-24 00:00:00.000000000 Z
         | 
| 13 | 
            +
            dependencies:
         | 
| 14 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 15 | 
            +
              name: rspec
         | 
| 16 | 
            +
              requirement: &20428260 !ruby/object:Gem::Requirement
         | 
| 17 | 
            +
                none: false
         | 
| 18 | 
            +
                requirements:
         | 
| 19 | 
            +
                - - ! '>='
         | 
| 20 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 21 | 
            +
                    version: '0'
         | 
| 22 | 
            +
              type: :development
         | 
| 23 | 
            +
              prerelease: false
         | 
| 24 | 
            +
              version_requirements: *20428260
         | 
| 25 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 26 | 
            +
              name: webmock
         | 
| 27 | 
            +
              requirement: &20427120 !ruby/object:Gem::Requirement
         | 
| 28 | 
            +
                none: false
         | 
| 29 | 
            +
                requirements:
         | 
| 30 | 
            +
                - - ! '>='
         | 
| 31 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 32 | 
            +
                    version: '0'
         | 
| 33 | 
            +
              type: :development
         | 
| 34 | 
            +
              prerelease: false
         | 
| 35 | 
            +
              version_requirements: *20427120
         | 
| 36 | 
            +
            description: Net::HTTP wrapper easy to use
         | 
| 37 | 
            +
            email:
         | 
| 38 | 
            +
            - gabynaiman@gmail.com
         | 
| 39 | 
            +
            executables: []
         | 
| 40 | 
            +
            extensions: []
         | 
| 41 | 
            +
            extra_rdoc_files: []
         | 
| 42 | 
            +
            files:
         | 
| 43 | 
            +
            - .gitignore
         | 
| 44 | 
            +
            - Gemfile
         | 
| 45 | 
            +
            - README.md
         | 
| 46 | 
            +
            - Rakefile
         | 
| 47 | 
            +
            - lib/web_client.rb
         | 
| 48 | 
            +
            - lib/web_client/base.rb
         | 
| 49 | 
            +
            - lib/web_client/resource.rb
         | 
| 50 | 
            +
            - lib/web_client/version.rb
         | 
| 51 | 
            +
            - spec/base_spec.rb
         | 
| 52 | 
            +
            - spec/resource_spec.rb
         | 
| 53 | 
            +
            - spec/spec_helper.rb
         | 
| 54 | 
            +
            - web_client.gemspec
         | 
| 55 | 
            +
            homepage: https://github.com/gabynaiman/web_client
         | 
| 56 | 
            +
            licenses: []
         | 
| 57 | 
            +
            post_install_message: 
         | 
| 58 | 
            +
            rdoc_options: []
         | 
| 59 | 
            +
            require_paths:
         | 
| 60 | 
            +
            - lib
         | 
| 61 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 62 | 
            +
              none: false
         | 
| 63 | 
            +
              requirements:
         | 
| 64 | 
            +
              - - ! '>='
         | 
| 65 | 
            +
                - !ruby/object:Gem::Version
         | 
| 66 | 
            +
                  version: '0'
         | 
| 67 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 68 | 
            +
              none: false
         | 
| 69 | 
            +
              requirements:
         | 
| 70 | 
            +
              - - ! '>='
         | 
| 71 | 
            +
                - !ruby/object:Gem::Version
         | 
| 72 | 
            +
                  version: '0'
         | 
| 73 | 
            +
            requirements: []
         | 
| 74 | 
            +
            rubyforge_project: 
         | 
| 75 | 
            +
            rubygems_version: 1.8.16
         | 
| 76 | 
            +
            signing_key: 
         | 
| 77 | 
            +
            specification_version: 3
         | 
| 78 | 
            +
            summary: Net::HTTP wrapper easy to use
         | 
| 79 | 
            +
            test_files: []
         |