Makes building SDKs for RESTful JSON services easy.
Ruby
Switch branches/tags
Nothing to show
Latest commit 35ba615 Jan 18, 2016 @daws version 1.2.0

README.rdoc

Simple SDK Builder

A set of libraries that make it easy to build an SDK gem on top of an HTTP web service.

Currently, only RESTful JSON services are supported.

Synopsis:

class MyNotFoundError < StandardError
  def initialize(response)
    super "object not found: #{response.body}"
  end
end

class MyUnknownError < StandardError; end

class MyBaseClass
  include SimpleSDKBuilder::Base

  config :service_url => 'https://api.davidmdawson.com'
  config :timeout => 15000
  config :error_handlers => {
    '404' => MyNotFoundError,
    '*' => MyUnknownError
  }

end

class MyResource < MyBaseClass
  include SimpleSDKBuilder::Resource

  simple_sdk_attribute :id, :name, :value

  class << self

    def all
      response = json_request( :path => "/my_resources" )
      response.build(MyResource)
    end

    def find(id)
      response = json_request( :path => "/my_resources/#{id}" )
      response.build(MyResource)
    end

  end

  def create
    response = json_request( :path => "/my_resources", :method => :post, :body => self )
    self.attributes = response.parsed_body
    self
  end

  def update(attrs)
    response = json_request( :path => "/my_resources/#{id}", :method => :put, :body => attrs )
    self.attributes = response.parsed_body
    self
  end

  def destroy
    json_request( :path => "/my_resources/#{id}", :method => :delete )
    # leaves current object alone
  end

end

all_resources = MyResource.all
some_resource = MyResource.find(1)
new_resource = MyResource.new( :name => 'test', :value => 'this is a test' ).create
new_resource.update( :value => 'this test is almost done...' )
new_resource.destroy