Class: Sputnik::Connection
- Inherits:
-
Object
- Object
- Sputnik::Connection
- Defined in:
- lib/sputnik/connection.rb
Instance Attribute Summary collapse
-
#apikey ⇒ Object
readonly
Returns the value of attribute apikey.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Instance Method Summary collapse
- #connect ⇒ Object
- #default_header ⇒ Object
- #get(path, params = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #post(path, params = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Connection
Returns a new instance of Connection.
11 12 13 14 |
# File 'lib/sputnik/connection.rb', line 11 def initialize(={}) @apikey = [:apikey] @base_url = [:base_url] || DEFAULT_HOST end |
Instance Attribute Details
#apikey ⇒ Object (readonly)
Returns the value of attribute apikey.
9 10 11 |
# File 'lib/sputnik/connection.rb', line 9 def apikey @apikey end |
#base_url ⇒ Object (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
#connect ⇒ Object
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_header ⇒ Object
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 |