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.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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)

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



168
169
170
171
172
173
174
# File 'lib/bibsonomy/api.rb', line 168

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



153
154
155
156
157
158
159
# File 'lib/bibsonomy/api.rb', line 153

def get_document_href(user_name, intra_hash, file_name)
  return @url_doc.expand({
                           :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.



184
185
186
187
188
189
190
# File 'lib/bibsonomy/api.rb', line 184

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



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bibsonomy/api.rb', line 75

def get_post(user_name, intra_hash)
  response = @conn.get @url_post.expand({
                                          :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.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/bibsonomy/api.rb', line 125

def get_posts(grouping, name, resource_type, tags = nil, start = 0, endc = $MAX_POSTS_PER_REQUEST)
  url = @url_posts.partial_expand({
                                    :format => @format,
                                    :resourcetype => get_resource_type(resource_type),
                                    :start => start,
                                    :end => endc
                                  })
  # decide what to get
  if grouping == "user"
    url = url.partial_expand({:user => name})
  elsif grouping == "group"
    url = url.partial_expand({:group => name})
  end
  # add tags, if requested
  if tags != nil
    url = url.partial_expand({:tags => tags.join(" ")})
  end

  response = @conn.get url.expand({})

  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.



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

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.



98
99
100
# File 'lib/bibsonomy/api.rb', line 98

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