Class: Crib::API

Inherits:
Object
  • Object
show all
Defined in:
lib/crib/api.rb

Overview

Defines an explorable REST API

Examples:

Define the GitHub API with a HTTP header

Crib::API.new('https://api.github.com') do |c|
  c.headers[:user_agent] = 'crib'
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, sawyer_options = {}, &block) ⇒ API

Defines an API by constructing a request handler

Parameters:

  • endpoint (String)

    API endpoint

  • sawyer_options (Hash) (defaults to: {})

    options for Sawyer

  • block (Block)

    block for Sawyer


20
21
22
23
24
25
26
# File 'lib/crib/api.rb', line 20

def initialize(endpoint, sawyer_options = {}, &block)
  @_agent = Sawyer::Agent.new(
    endpoint,
    sawyer_options.merge(links_parser: Sawyer::LinkParsers::Simple.new),
    &block
  )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)


30
31
32
# File 'lib/crib/api.rb', line 30

def method_missing(method_name, *args)
  Request.new(self, Helpers.construct_path(method_name, args))
end

Instance Attribute Details

#_agentSawyer::Agent (readonly)

Returns request handler.

Returns:

  • (Sawyer::Agent)

    request handler


10
11
12
# File 'lib/crib/api.rb', line 10

def _agent
  @_agent
end

#_last_responseSawyer::Response?

Returns most recent response, if any.

Returns:

  • (Sawyer::Response, nil)

    most recent response, if any


13
14
15
# File 'lib/crib/api.rb', line 13

def _last_response
  @_last_response
end