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.
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 |
# File 'lib/bibsonomy/api.rb', line 45 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) # initialise URLs @url_post = Addressable::Template.new("/api/users/{user_name}/posts/{intra_hash}?format={format}") @url_posts = Addressable::Template.new("/api/posts{?format,resourcetype,start,end,user,group,tags}") @url_doc = Addressable::Template.new("/api/users/{user_name}/posts/{intra_hash}/documents/{file_name}") end |
Instance Method Details
#get_document(user_name, intra_hash, file_name) ⇒ Object
Get a document belonging to a post.
171 172 173 174 175 176 177 |
# File 'lib/bibsonomy/api.rb', line 171 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
156 157 158 159 160 161 162 |
# File 'lib/bibsonomy/api.rb', line 156 def get_document_href(user_name, intra_hash, file_name) return @url_doc.({ :user_name => user_name, :intra_hash => intra_hash, :file_name => file_name }) end |
#get_document_preview(user_name, intra_hash, file_name, size) ⇒ Object
Get the preview for a document belonging to a post.
187 188 189 190 191 192 193 |
# File 'lib/bibsonomy/api.rb', line 187 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
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/bibsonomy/api.rb', line 78 def get_post(user_name, intra_hash) response = @conn.get @url_post.({ :user_name => user_name, :intra_hash => 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.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/bibsonomy/api.rb', line 128 def get_posts(grouping, name, resource_type, = nil, start = 0, endc = $MAX_POSTS_PER_REQUEST) url = @url_posts.({ :format => @format, :resourcetype => get_resource_type(resource_type), :start => start, :end => endc }) # decide what to get if grouping == "user" url = url.({:user => name}) elsif grouping == "group" url = url.({:group => name}) end # add tags, if requested if != nil url = url.({:tags => .join(" ")}) end response = @conn.get url.({}) 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.
114 115 116 |
# File 'lib/bibsonomy/api.rb', line 114 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.
101 102 103 |
# File 'lib/bibsonomy/api.rb', line 101 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 |