Class: MySpace::Connection
- Inherits:
-
Object
- Object
- MySpace::Connection
- Includes:
- Net
- Defined in:
- lib/myspace/connection.rb
Overview
initialization.
Instance Attribute Summary collapse
-
#apiKey ⇒ Object
API key.
-
#apiSecret ⇒ Object
API password.
-
#format ⇒ Object
preferred output format.
-
#host ⇒ Object
host to connect.
-
#username ⇒ Object
API username.
Instance Method Summary collapse
-
#connect {|@http| ... } ⇒ Object
Connects to the MySapce API servers, returning a Net::HTTP class instance.
- #consumer ⇒ Object
-
#get(path = "/") {|la.call| ... } ⇒ Object
Performs a GET request against the MySpace API servers.
-
#get_body(path) ⇒ Object
Fetches the response body from the API servers.
-
#initialize(attributes = {}) {|_self| ... } ⇒ Connection
constructor
Constructor for the MySpace::Connection class.
-
#post(path = "/", data = "", headers = {}, content_type = "application/x-www-form-urlencoded") {|@http.request(req)| ... } ⇒ Object
Perform a POST operation against the MySpace API servers.
-
#timestamp ⇒ Object
Returns an rfc1123 format string.
Constructor Details
#initialize(attributes = {}) {|_self| ... } ⇒ Connection
Constructor for the MySpace::Connection class
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/myspace/connection.rb', line 23 def initialize(attributes={}) @apiKey = attributes[:apiKey] ||= CONF["apiKey"] @secretKey = attributes[:apiSecret] ||= CONF["secretKey"] @host = attributes[:host] ||= API_HOST @format = attributes[:format] ||= "json" @oauthOptions = { :scheme => :query_string, :nonce => rand(65535).to_s, :timestamp => Time.now.to_i.to_s, :site => "http://#{API_HOST}", } yield self if block_given? end |
Instance Attribute Details
#apiKey ⇒ Object
API key
16 17 18 |
# File 'lib/myspace/connection.rb', line 16 def apiKey @apiKey end |
#apiSecret ⇒ Object
API password
14 15 16 |
# File 'lib/myspace/connection.rb', line 14 def apiSecret @apiSecret end |
#format ⇒ Object
preferred output format
20 21 22 |
# File 'lib/myspace/connection.rb', line 20 def format @format end |
#host ⇒ Object
host to connect
18 19 20 |
# File 'lib/myspace/connection.rb', line 18 def host @host end |
#username ⇒ Object
API username
12 13 14 |
# File 'lib/myspace/connection.rb', line 12 def username @username end |
Instance Method Details
#connect {|@http| ... } ⇒ Object
Connects to the MySapce API servers, returning a Net::HTTP class instance. If a block is given, then that instance is yielded.
50 51 52 53 54 55 56 |
# File 'lib/myspace/connection.rb', line 50 def connect # require "net/https" # http = Net::HTTP.new(MYSPACE_API_HOST, 443) # http.use_ssl = true @http = HTTP.start(@host) yield @http if block_given? end |
#consumer ⇒ Object
44 45 46 |
# File 'lib/myspace/connection.rb', line 44 def consumer OAuth::Consumer.new(CGI.escape(@apiKey),@secretKey,@oauthOptions) end |
#get(path = "/") {|la.call| ... } ⇒ Object
Performs a GET request against the MySpace API servers. The ‘path’ is the first argument, and the second argument ‘http’ is an optional, existing Net::HTTP connection. Returns or yields a Net::HTTP::Response instance, depending on how it’s called.
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/myspace/connection.rb', line 97 def get(path="/") req = HTTP::Get.new(path) connect if !@http la = lambda { req.oauth!(@http,consumer,nil,@oauthOptions) @http.request(req) } yield la.call if block_given? end |
#get_body(path) ⇒ Object
Fetches the response body from the API servers
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/myspace/connection.rb', line 75 def get_body(path) connect if !@http get(path) {|res| case res.code when "200" return res.body when "401" raise InvalidCredentials, "Access denied. (#{res.code})\n#{res.body}" else raise InvalidResponse,"(#{res.code})\n#{res.body}" end } end |
#post(path = "/", data = "", headers = {}, content_type = "application/x-www-form-urlencoded") {|@http.request(req)| ... } ⇒ Object
Perform a POST operation against the MySpace API servers. You can pass a ‘path’, post data ‘data’ and a ‘content_type’ optionally. application/x-www-form-urlencoded text/xml; charset=utf-8
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/myspace/connection.rb', line 63 def post(path="/",data="",headers={},content_type="application/x-www-form-urlencoded") connect if !@http req = HTTP::Post.new(path,headers) req.set_form_data({ }, ';') req.body = data req.content_type = content_type yield @http.request(req) if block_given? end |
#timestamp ⇒ Object
Returns an rfc1123 format string
40 41 42 |
# File 'lib/myspace/connection.rb', line 40 def Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S GMT") end |