test ruby code
Ruby
code posted
created at 02 Aug 18:28, updated at 05 Aug 03:52
Edit
|
Back
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
require 'httparty' require 'cgi' require 'multi_json' module Pocket class Connection include HTTParty base_uri 'https://getpocket.com' headers "Content-Type" => "application/json; charset=UTF-8" headers "X-Accept" => "application/json" class <<self attr_accessor :client_key attr_accessor :request_token ##################################################### # OAuth authorization Helpers # Sample Workflow: # 1. generate_authorize_url, redirect user to URL # 2. generate_access_token if user comes back # 3. Do a sample call to API to see if it works ##################################################### def generate_request_token(body_options={}, headers_options={}) response = post("/v3/oauth/request", :body => MultiJson.dump({:consumer_key => @client_key}.merge(body_options)), :headers => {"Content-Type" => "application/json; charset=UTF-8", "X-Accept" => "application/json"}.merge(headers_options)) if response.success? && response.parsed_response["code"] # Should be a string body like "code=12345678" @request_token = response.parsed_response["code"] else raise "could not generate request token: #{response.inspect}" end end def generate_access_token(request_token=@request_token) response = post("/v3/oauth/authorize", :body => MultiJson.dump({:code => request_token, :consumer_key => @client_key}), :headers => {"Content-Type" => "application/json; charset=UTF-8", "X-Accept" => "application/json"}) raise response.headers["X-Error"] if response.headers["X-Error"] response.parsed_response["access_token"] end def generate_authorize_url(redirect_uri, state=nil) @request_token = generate_request_token({:redirect_uri => redirect_uri}) "#{default_options[:base_uri]}/auth/authorize?request_token=#{CGI.escape(request_token)}&redirect_uri=#{CGI.escape(redirect_uri)}#{"&state=#{CGI.escape(state)}"if state}" end end # <<self end class <<self attr_accessor :client_key attr_accessor :access_token def configure(credentials={}) @client_key = credentials[:client_key] @access_token = credentials[:access_token] end # Retrieve API # Options: # * state # unread = only return unread items (default) # archive = only return archived items # all = return both unread and archived items # * favorite # 0 = only return un-favorited items # 1 = only return favorited items # * tag # tag_name = only return items tagged with tag_name # _untagged_ = only return untagged items # * contentType # article = only return articles # video = only return videos or articles with embedded videos # image = only return images # * sort # newest = return items in order of newest to oldest # oldest = return items in order of oldest to newest # title = return items in order of title alphabetically # site = return items in order of url alphabetically # * detailType # simple = only return the titles and urls of each item # complete = return all data about each item, including tags, images, authors, videos and more # * search - search query # * domain - search within a domain # * since - timestamp of modifed items after a date # * count - limit of items to return # * offset - Used only with count; start returning from offset position of results def retrieve(options={}) response = request(:get, "/v3/get", {:body => options}) response["list"] end # Add API # Options: # * title # * tags - comma-seperated list of tags # * tweet_id - Twitter tweet_id def add(url, options={}) request(:post, '/v3/add', :body => {:url => url}.merge(options)) end # Modify API # Actions: # * add # * archive # * readd - re-add # * favorite # * unfavorite # * delete # * tags_add # * tags_remove # * tags_replace # * tags_clear # * tags_rename def modify(action, options={}) request(:post, '/v3/send', :body => {:action => action}.merge(options)) end def request(method, *arguments) arguments[1] ||= {} arguments[1][:body] ||= {} arguments[1][:body] = MultiJson.dump(arguments[1][:body].merge({:consumer_key => @client_key, :access_token => @access_token})) response = Connection.__send__(method.downcase.to_sym, *arguments) raise response.headers["X-Error"] if response.headers["X-Error"] response.parsed_response end end # <<self end |
4.72 KB in 6 ms with coderay