Class: BibSonomy::API

Inherits:
Object
  • Object
show all
Defined in:
lib/bibsonomy/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(user_name, api_key, format = 'ruby') ⇒ API

Initializes the client with the given credentials.

Parameters:

  • user_name (String)

    the name of the user account used for accessing the API

  • api_key (String)

    the API key corresponding to the user account - can be obtained from www.bibsonomy.org/settings?selTab=1

  • format (String) (defaults to: 'ruby')

    The requested return format. One of: ‘xml’, ‘json’, ‘ruby’, ‘csl’, ‘bibtex’. The default is ‘ruby’ which returns Ruby objects defined by this library. Currently, ‘csl’ and ‘bibtex’ are only available for publications.



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.

Parameters:

  • user_name
  • intra_hash
  • file_name

Returns:

  • the document and the content type



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.

Parameters:

  • user_name
  • intra_hash
  • file_name
  • size (String)

    requested preview size (allowed values: SMALL, MEDIUM, LARGE)

Returns:

  • the preview image and the content type ‘image/jpeg`



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

Parameters:

  • user_name (String)

    the name of the post’s owner

  • intra_hash (String)

    the intrag hash of the post

Returns:



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.

Parameters:

  • grouping (String)

    the type of the name (either “user” or “group”)

  • name (String)

    the name of the group or user

  • resource_type (String)

    the type of the post. Currently supported are ‘bookmark’ and ‘publication’.

  • tags (Array<String>) (defaults to: nil)

    the tags that all posts must contain (can be empty)

  • start (Integer) (defaults to: 0)

    number of first post to download

  • endc (Integer) (defaults to: $MAX_POSTS_PER_REQUEST)

    number of last post to download

Returns:



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, tags = 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 tags != nil
    params[:tags] = 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.

Parameters:

  • group_name (String)

    the name of the group

  • resource_type (String)

    the type of the post. Currently supported are ‘bookmark’ and ‘publication’.

  • tags (Array<String>) (defaults to: nil)

    the tags that all posts must contain (can be empty)

  • start (Integer) (defaults to: 0)

    number of first post to download

  • endc (Integer) (defaults to: $MAX_POSTS_PER_REQUEST)

    number of last post to download

Returns:



103
104
105
# File 'lib/bibsonomy/api.rb', line 103

def get_posts_for_group(group_name, resource_type, tags = nil, start = 0, endc = $MAX_POSTS_PER_REQUEST)
  return get_posts("group", group_name, resource_type, tags, 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.

Parameters:

  • user_name (String)

    the name of the posts’ owner

  • resource_type (String)

    the type of the post. Currently supported are ‘bookmark’ and ‘publication’.

  • tags (Array<String>) (defaults to: nil)

    the tags that all posts must contain (can be empty)

  • start (Integer) (defaults to: 0)

    number of first post to download

  • endc (Integer) (defaults to: $MAX_POSTS_PER_REQUEST)

    number of last post to download

Returns:



90
91
92
# File 'lib/bibsonomy/api.rb', line 90

def get_posts_for_user(user_name, resource_type, tags = nil, start = 0, endc = $MAX_POSTS_PER_REQUEST)
  return get_posts("user", user_name, resource_type, tags, start, endc)
end