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.

accessing the API

account - can be obtained from www.bibsonomy.org/settings?selTab=1

‘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.

Parameters:

  • user_name (String)

    The name of the user account used for

  • api_key (String)

    The API key corresponding to the user

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

    The requested return format. One of:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bibsonomy/api.rb', line 41

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.

Params:

user_name
intra_hash
file_name


122
123
124
125
126
127
128
# File 'lib/bibsonomy/api.rb', line 122

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



111
112
113
# File 'lib/bibsonomy/api.rb', line 111

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.

Params:

user_name
intra_hash
file_name
size


138
139
140
141
142
143
144
# File 'lib/bibsonomy/api.rb', line 138

def get_document_preview(user_name, intra_hash, file_name, size)
  response = 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) ⇒ Object

Get a single post

Params:

user_name
String

The name of the post’s owner.

intra_hash
String

The intrag hash of the post.

return
BibSonomy::Post

the requested post



72
73
74
75
76
77
78
79
80
# File 'lib/bibsonomy/api.rb', line 72

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_for_user(user_name, resource_type, tags = nil, start = 0, endc = $MAX_POSTS_PER_REQUEST) ⇒ Object

Get posts owned by a user, optionally filtered by tags.

Params:

user_name
String

The name of the posts’ owner.

resource_type
String

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

tags
start
endc


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/bibsonomy/api.rb', line 91

def get_posts_for_user(user_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
  }
  # add tags, if requested
  if tags != nil
    params[:tags] = tags.join(" ")
  end
  response = @conn.get "/api/users/" + CGI.escape(user_name) + "/posts", params

  if @parse
    posts = JSON.parse(response.body)["posts"]["post"]
    return posts.map { |attributes| Post.new(attributes) }
  end
  return response.body
end