Class: Sputnik::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/sputnik/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



11
12
13
14
# File 'lib/sputnik/connection.rb', line 11

def initialize(options={})
  @apikey = options[:apikey]
  @base_url = options[:base_url] || DEFAULT_HOST
end

Instance Attribute Details

#apikeyObject (readonly)

Returns the value of attribute apikey.



9
10
11
# File 'lib/sputnik/connection.rb', line 9

def apikey
  @apikey
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



9
10
11
# File 'lib/sputnik/connection.rb', line 9

def base_url
  @base_url
end

Instance Method Details

#connectObject



16
17
18
19
20
21
22
# File 'lib/sputnik/connection.rb', line 16

def connect
  conn = Faraday.new(:url => base_url) do |builder|
    builder.request :json
    builder.adapter :net_http
  end
  conn
end

#default_headerObject



51
52
53
# File 'lib/sputnik/connection.rb', line 51

def default_header
  "Sputnik/#{VERSION}/ruby"
end

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



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sputnik/connection.rb', line 24

def get(path, params={})
  params = params.merge({:_apikey => apikey})
  response = connect.get do |req|
    req.url(path)
    req.headers['User-Agent'] = default_header
    req.params = params
  end

  verify_status!(response)

  return JSON.parse(response.body) || {}
end

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



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sputnik/connection.rb', line 37

def post(path, params={})
  params = params.merge({:_apikey => apikey})
  response = connect.post do |req|
    req.url(path)
    req.params = params
    req.headers['Content-Type'] = 'application/json'
    req.headers['User-Agent'] = default_header
  end

  verify_status!(response)

  return JSON.parse(response.body) || {}
end