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.

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bibsonomy/api.rb', line 37

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.



116
117
118
119
120
121
122
# File 'lib/bibsonomy/api.rb', line 116

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



105
106
107
# File 'lib/bibsonomy/api.rb', line 105

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.



132
133
134
135
136
137
138
# File 'lib/bibsonomy/api.rb', line 132

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) ⇒ BibSonomy::Post, String

Get a single post



66
67
68
69
70
71
72
73
74
# File 'lib/bibsonomy/api.rb', line 66

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) ⇒ Array<BibSonomy::Post>, String

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bibsonomy/api.rb', line 85

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