Class: Fuselage::Api

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/fuselage/api.rb,
lib/fuselage/api.rb

Overview

This is the real API class.

API requests are limited to 60 per minute.

Sets up basic methods for accessing the API.

Direct Known Subclasses

AnonymousApi, AuthApi

Constant Summary collapse

RETRYABLE_STATUS =
[403]
MAX_RETRIES =
5
@@api =
Fuselage::AnonymousApi.instance
@@authenticated =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



103
104
105
# File 'lib/fuselage/api.rb', line 103

def method_missing(method, *args)
  api.send(method, *args)
end

Instance Attribute Details

#read_onlyObject

Returns the value of attribute read_only.



6
7
8
# File 'lib/fuselage/api.rb', line 6

def read_only
  @read_only
end

#tokenObject

Returns the value of attribute token.



6
7
8
# File 'lib/fuselage/api.rb', line 6

def token
  @token
end

Class Method Details

.apiObject Also known as: me

The API we’re using



64
65
66
# File 'lib/fuselage/api.rb', line 64

def self.api
  @@api
end

.api=(value) ⇒ Object

set the API we’re using



73
74
75
# File 'lib/fuselage/api.rb', line 73

def self.api=(value)
  @@api = value
end

.authenticatedObject

We use this to check if we use the auth or anonymous api



54
55
56
# File 'lib/fuselage/api.rb', line 54

def self.authenticated
  @@authenticated
end

.authenticated=(value) ⇒ Object

We set this to true when the user has auth’d.



59
60
61
# File 'lib/fuselage/api.rb', line 59

def self.authenticated=(value)
  @@authenticated = value
end

Instance Method Details

#delete(path, params = {}, klass = nil) ⇒ Object

Raises:



93
94
95
96
97
98
99
# File 'lib/fuselage/api.rb', line 93

def delete(path, params = {}, klass=nil)
  resp = self.class.delete(path, { :query => auth_parameters, :body => params.to_json } )
  raise NotFound, klass || self.class if resp.code.to_i == 404
  raise APIError, 
    "GitHub returned status #{resp.code}" unless resp.code.to_i == 200 || resp.code.to_i == 201 || resp.code.to_i == 204
  resp
end

#get(path, params = {}, klass = nil) ⇒ Object

Raises:



77
78
79
80
81
82
83
# File 'lib/fuselage/api.rb', line 77

def get(path, params = {}, klass=nil)
  resp = self.class.get(path, { :query => params.merge(auth_parameters) })
  raise NotFound, klass || self.class if resp.code.to_i == 404
  raise APIError, 
    "GitHub returned status #{resp.code}" unless resp.code.to_i == 200 || resp.code.to_i == 201
  resp
end

#post(path, params = {}, klass = nil) ⇒ Object

Raises:



85
86
87
88
89
90
91
# File 'lib/fuselage/api.rb', line 85

def post(path, params = {}, klass=nil)
  resp = self.class.post(path, { :query => auth_parameters, :body => params.to_json } )
  raise NotFound, klass || self.class if resp.code.to_i == 404
  raise APIError, 
    "GitHub returned status #{resp.code}" unless resp.code.to_i == 200 || resp.code.to_i == 201
  resp
end