Class: Octopi::Api

Inherits:
Object
  • Object
show all
Includes:
Self, Singleton
Defined in:
lib/octopi/api.rb,
lib/octopi/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

CONTENT_TYPE =
{
  'yaml' => ['application/x-yaml', 'text/yaml', 'text/x-yaml', 'application/yaml'],
  'json' => 'application/json',
  'xml'  => 'application/xml',
  # Unexpectedly, Github returns resources such as blobs as text/html!
  # Thus, plain == text/html.
  'plain' => ['text/plain', 'text/html']
}
RETRYABLE_STATUS =
[403]
MAX_RETRIES =
10
@@api =
Octopi::AnonymousApi.instance
@@authenticated =
false

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Self

#emails, #follow!, #keys, #unfollow!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method, *args) (private)



160
161
162
# File 'lib/octopi/api.rb', line 160

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

Instance Attribute Details

- (Object) format

Returns the value of attribute format



7
8
9
# File 'lib/octopi/api.rb', line 7

def format
  @format
end

- (Object) login

Returns the value of attribute login



7
8
9
# File 'lib/octopi/api.rb', line 7

def 
  @login
end

- (Object) read_only

Returns the value of attribute read_only



7
8
9
# File 'lib/octopi/api.rb', line 7

def read_only
  @read_only
end

- (Object) token

Returns the value of attribute token



7
8
9
# File 'lib/octopi/api.rb', line 7

def token
  @token
end

- (Object) trace_level

Returns the value of attribute trace_level



7
8
9
# File 'lib/octopi/api.rb', line 7

def trace_level
  @trace_level
end

Class Method Details

+ (Object) api Also known as: me

The API we're using



72
73
74
# File 'lib/octopi/api.rb', line 72

def self.api
  @@api
end

+ (Object) api=(value)

set the API we're using



81
82
83
# File 'lib/octopi/api.rb', line 81

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

+ (Object) authenticated

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



62
63
64
# File 'lib/octopi/api.rb', line 62

def self.authenticated
  @@authenticated
end

+ (Object) authenticated=(value)

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



67
68
69
# File 'lib/octopi/api.rb', line 67

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

Instance Method Details

- (Object) find(path, result_key, resource_id, klass = nil, cache = true)



99
100
101
102
# File 'lib/octopi/api.rb', line 99

def find(path, result_key, resource_id, klass=nil, cache=true)      
  result = get(path, { :id => resource_id, :cache => cache }, klass) 
  result
end

- (Object) find_all(path, result_key, query, klass = nil, cache = true)



105
106
107
108
# File 'lib/octopi/api.rb', line 105

def find_all(path, result_key, query, klass=nil, cache=true)
  result = get(path, { :query => query, :id => query, :cache => cache }, klass)
  result[result_key]
end

- (Object) get(path, params = {}, klass = nil, format = :yaml)



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/octopi/api.rb', line 114

def get(path, params = {}, klass=nil, format = :yaml)
  @@retries = 0
  begin
    submit(path, params, klass, format) do |path, params, format, query|
      self.class.get "/#{format}#{path}", { :format => format, :query => query }
    end
  rescue RetryableAPIError => e
    if @@retries < MAX_RETRIES
      $stderr.puts e.message
      if e.code != 403
        @@retries += 1
        sleep 6
        retry
      else
        raise APIError, "Github returned status #{e.code}, you may not have access to this resource."
      end
    else  
      raise APIError, "GitHub returned status #{e.code}, despite" +
       " repeating the request #{MAX_RETRIES} times. Giving up."
    end  
  end  
end

- (Object) get_raw(path, params, klass = nil)



110
111
112
# File 'lib/octopi/api.rb', line 110

def get_raw(path, params, klass=nil)
 get(path, params, klass,  'plain')
end

- (Object) post(path, params = {}, klass = nil, format = :yaml)



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/octopi/api.rb', line 137

def post(path, params = {}, klass=nil, format = :yaml)
  @@retries = 0
  begin
    trace "POST", "/#{format}#{path}", params
    submit(path, params, klass, format) do |path, params, format, query|
      resp = self.class.post "/#{format}#{path}", { :body => params, :format => format, :query => query }
      resp
    end
  rescue RetryableAPIError => e
    if @@retries < MAX_RETRIES
      $stderr.puts e.message
      @@retries += 1
      sleep 6
      retry
    else
      raise APIError, "GitHub returned status #{e.code}, despite" +
       " repeating the request #{MAX_RETRIES} times. Giving up."
    end
  end
end

- (Object) save(resource_path, data)



92
93
94
95
96
# File 'lib/octopi/api.rb', line 92

def save(resource_path, data)
  traslate resource_path, data
  #still can't figure out on what format values are expected
  post("#{resource_path}", { :query => data })
end

- (Object) user



86
87
88
89
90
# File 'lib/octopi/api.rb', line 86

def user
  user_data = get("/user/show/#{}")
  raise "Unexpected response for user command" unless user_data and user_data['user']
  User.new(user_data['user'])
end