Class: BibSonomy::API
- Inherits:
-
Object
- Object
- BibSonomy::API
- Defined in:
- lib/bibsonomy/api.rb
Instance Method Summary collapse
-
#get_document(user_name, intra_hash, file_name) ⇒ Object
Get a document belonging to a post.
- #get_document_href(user_name, intra_hash, file_name) ⇒ Object
-
#get_document_preview(user_name, intra_hash, file_name, size) ⇒ Object
Get the preview for a document belonging to a post.
-
#get_post(user_name, intra_hash) ⇒ BibSonomy::Post, String
Get a single post.
-
#get_posts(grouping, name, resource_type, tags = nil, start = 0, endc = $MAX_POSTS_PER_REQUEST) ⇒ Array<BibSonomy::Post>, String
Get posts for a user or group, optionally filtered by tags.
-
#get_posts_for_group(group_name, resource_type, tags = nil, start = 0, endc = $MAX_POSTS_PER_REQUEST) ⇒ Array<BibSonomy::Post>, String
Get the posts of the users of a group, optionally filtered by tags.
-
#get_posts_for_user(user_name, resource_type, tags = nil, start = 0, endc = $MAX_POSTS_PER_REQUEST) ⇒ Array<BibSonomy::Post>, String
Get posts owned by a user, optionally filtered by tags.
-
#initialize(user_name, api_key, format = 'ruby') ⇒ API
constructor
Initializes the client with the given credentials.
Constructor Details
#initialize(user_name, api_key, format = 'ruby') ⇒ API
Initializes the client with the given credentials.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/bibsonomy/api.rb', line 42 def initialize(user_name, api_key, format = 'ruby') # configure output format if format == 'ruby' @format = 'json' @parse = true else @format = format @parse = false end @conn = Faraday.new(:url => $API_URL) do |faraday| faraday.request :url_encoded # form-encode POST params #faraday.response :logger faraday.adapter Faraday.default_adapter # make requests with # Net::HTTP end @conn.basic_auth(user_name, api_key) end |
Instance Method Details
#get_document(user_name, intra_hash, file_name) ⇒ Object
Get a document belonging to a post.
156 157 158 159 160 161 162 |
# File 'lib/bibsonomy/api.rb', line 156 def get_document(user_name, intra_hash, file_name) response = @conn.get get_document_href(user_name, intra_hash, file_name) if response.status == 200 return [response.body, response.headers['content-type']] end return nil, nil end |
#get_document_href(user_name, intra_hash, file_name) ⇒ Object
145 146 147 |
# File 'lib/bibsonomy/api.rb', line 145 def get_document_href(user_name, intra_hash, file_name) return "/api/users/" + CGI.escape(user_name) + "/posts/" + CGI.escape(intra_hash) + "/documents/" + CGI.escape(file_name) end |
#get_document_preview(user_name, intra_hash, file_name, size) ⇒ Object
Get the preview for a document belonging to a post.
172 173 174 175 176 177 178 |
# File 'lib/bibsonomy/api.rb', line 172 def get_document_preview(user_name, intra_hash, file_name, size) response = @conn.get get_document_href(user_name, intra_hash, file_name), { :preview => size } if response.status == 200 return [response.body, 'image/jpeg'] end return nil, nil end |
#get_post(user_name, intra_hash) ⇒ BibSonomy::Post, String
Get a single post
71 72 73 74 75 76 77 78 79 |
# File 'lib/bibsonomy/api.rb', line 71 def get_post(user_name, intra_hash) response = @conn.get "/api/users/" + CGI.escape(user_name) + "/posts/" + CGI.escape(intra_hash), { :format => @format } if @parse attributes = JSON.parse(response.body) return Post.new(attributes["post"]) end return response.body end |
#get_posts(grouping, name, resource_type, tags = nil, start = 0, endc = $MAX_POSTS_PER_REQUEST) ⇒ Array<BibSonomy::Post>, String
Get posts for a user or group, optionally filtered by tags.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/bibsonomy/api.rb', line 117 def get_posts(grouping, name, resource_type, = nil, start = 0, endc = $MAX_POSTS_PER_REQUEST) params = { :format => @format, :resourcetype => get_resource_type(resource_type), :start => start, :end => endc } # decide what to get if grouping == "user" params[:user] = name elsif grouping == "group" params[:group] = name end # add tags, if requested if != nil params[:tags] = .join(" ") end response = @conn.get "/api/posts", params if @parse posts = JSON.parse(response.body)["posts"]["post"] return posts.map { |attributes| Post.new(attributes) } end return response.body end |
#get_posts_for_group(group_name, resource_type, tags = nil, start = 0, endc = $MAX_POSTS_PER_REQUEST) ⇒ Array<BibSonomy::Post>, String
Get the posts of the users of a group, optionally filtered by tags.
103 104 105 |
# File 'lib/bibsonomy/api.rb', line 103 def get_posts_for_group(group_name, resource_type, = nil, start = 0, endc = $MAX_POSTS_PER_REQUEST) return get_posts("group", group_name, resource_type, , start, endc) end |
#get_posts_for_user(user_name, resource_type, tags = nil, start = 0, endc = $MAX_POSTS_PER_REQUEST) ⇒ Array<BibSonomy::Post>, String
Get posts owned by a user, optionally filtered by tags.
90 91 92 |
# File 'lib/bibsonomy/api.rb', line 90 def get_posts_for_user(user_name, resource_type, = nil, start = 0, endc = $MAX_POSTS_PER_REQUEST) return get_posts("user", user_name, resource_type, , start, endc) end |