Class: Rubyoverflow::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/rubyoverflow.rb

Constant Summary collapse

HOST =

Most of this class is borrowed from the Pilha (github.com/dlt/pilha) project because a) it works, b) a lot of this code would be the same regardless of implementation (especially the open => gzip code, query string, and normalize), and c) I’m new to ruby, so I’m going to skip pass some of the more ‘boring’ stuff and get to the interesting parts. I plan to re-examine this at a later point

'http://api.stackoverflow.com'
VERSION =
'1.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = OpenStruct.new) ⇒ Client

Returns a new instance of Client.



63
64
65
66
67
68
69
70
71
# File 'lib/rubyoverflow.rb', line 63

def initialize(options = OpenStruct.new)
  if options.kind_of? OpenStruct
    @host = options.host || HOST
    @version = options.version || VERSION
    @api_key = options.api_key if options.api_key
  end
  
 
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



61
62
63
# File 'lib/rubyoverflow.rb', line 61

def api_key
  @api_key
end

#hostObject (readonly)

Returns the value of attribute host.



60
61
62
# File 'lib/rubyoverflow.rb', line 60

def host
  @host
end

Class Method Details

.change_end_point(val = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rubyoverflow.rb', line 95

def change_end_point(val = nil)
  options = OpenStruct.new
  if val.kind_of? ApiSite
    options.host = val.api_endpoint
  end

  if val.kind_of? String
    options.host = val
  end
  options.api_key = Base.client.api_key
  
  init_client! Client.new(options)
end

.config {|options| ... } ⇒ Object

this specifically works well, and I don’t fully understand it, so I borrowed this from Pilha more than anything else If you want to explain why I only have to configure this once than forget about it, please do, because this stuff is currently over my head

Yields:

  • (options)


89
90
91
92
93
# File 'lib/rubyoverflow.rb', line 89

def config &block
  options = OpenStruct.new
  yield options if block_given?
  init_client! Client.new(options)
end

.init_client!(client) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/rubyoverflow.rb', line 109

def init_client!(client)
  base_eigenclass = class << Base; self; end
  base_eigenclass.send :define_method, :client do
    @client = client
  end
  client
end

.stackauth_client(api_key = '') ⇒ Object



117
118
119
120
121
122
# File 'lib/rubyoverflow.rb', line 117

def stackauth_client(api_key = '')
  options = OpenStruct.new
  options.host = 'http://stackauth.com/'
  options.api_key = api_key if api_key
  Client.new options
end

Instance Method Details

#host_pathObject



82
83
84
# File 'lib/rubyoverflow.rb', line 82

def host_path
  normalize(@host) +  normalize(@version)
end

#request(path, parameters) ⇒ Object



75
76
77
78
79
80
# File 'lib/rubyoverflow.rb', line 75

def request(path, parameters)
  parameters['key'] = @api_key if @api_key
  url = host_path + normalize(path) + query_string(parameters)
  response = self.class.get url
  return response.parsed_response, url
end