Giter Site home page Giter Site logo

rom-http's Introduction

rom-http Join the chat at https://rom-rb.zulipchat.com

Gem Version Build Status Code Climate Test Coverage Inline docs

HTTP adapter for rom-rb.

Resources:

Installation

Add this line to your application's Gemfile:

gem 'rom-http'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rom-http

Quick-start

require 'rom'

class Users < ROM::Relation[:http]
  schema(:users) do
    attribute :id, ROM::Types::Integer
    attribute :name, ROM::Types::String
    attribute :username, ROM::Types::String
    attribute :email, ROM::Types::String
    attribute :phone, ROM::Types::String
    attribute :website, ROM::Types::String
  end

  def by_id(id)
    with_path(id.to_s)
  end
end

configuration = ROM::Configuration.new(:http, {
  uri: 'http://jsonplaceholder.typicode.com',
  headers: {
    Accept: 'application/json'
  },
  handlers: :json
})

configuration.register_relation(Users)

container = ROM.container(configuration)

container.relation(:users).by_id(1).to_a
# => GET http://jsonplaceholder.typicode.com/users/1 [ Accept: application/json ]

Extending

require 'rom-http'

module ROM
  module MyAdapter
    class Dataset < ROM::HTTP::Dataset
      config.default_request_handler = ->(dataset) do
        uri = dataset.uri

        http = Net::HTTP.new(uri.host, uri.port)
        request_klass = Net::HTTP.const_get(Inflecto.classify(dataset.request_method))

        request = request_klass.new(uri.request_uri)
        
        dataset.headers.each_with_object(request) do |(header, value), request|
          request[header.to_s] = value
        end

        response = http.request(request)
      end

      config.default_response_handler = ->(response, dataset) do
        if %i(post put patch).include?(dataset.request_method)
          JSON.parse(response.body, symbolize_names: true)
        else
          Array([JSON.parse(response.body, symbolize_names: true)]).flatten
        end
      end
    end

    class Gateway < ROM::HTTP::Gateway; end

    class Relation < ROM::HTTP::Relation
      adapter :my_adapter
    end

    module Commands
      class Create < ROM::HTTP::Commands::Create
        adapter :my_adapter
      end

      class Update < ROM::HTTP::Commands::Update
        adapter :my_adapter
      end

      class Delete < ROM::HTTP::Commands::Delete
        adapter :my_adapter
      end
    end
  end
end

ROM.register_adapter(:my_adapter, ROM::MyAdapter)

configuration = ROM::Configuration.new(:my_adapter, {
  uri: 'http://jsonplaceholder.typicode.com',
  headers: {
    Accept: 'application/json'
  }
})

class Users < ROM::Relation[:my_adapter]
  schema(:users) do
    attribute :id, ROM::Types::Integer
    attribute :name, ROM::Types::String
    attribute :username, ROM::Types::String
    attribute :email, ROM::Types::String
    attribute :phone, ROM::Types::String
    attribute :website, ROM::Types::String
  end

  def by_id(id)
    with_path(id.to_s)
  end
end

configuration.register_relation(Users)

container = ROM.container(configuration)

container.relation(:users).by_id(1).to_a
# => GET http://jsonplaceholder.typicode.com/users/1 [ Accept: application/json ]

License

See LICENSE file.

rom-http's People

Contributors

amhol avatar solnic avatar cflipse avatar maximderbin avatar parndt avatar flash-gordon avatar fbernier avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.