Class: Readit::API

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token = '', access_token_secret = '') ⇒ API

initializer if creating a new Readit API client

Parameters:

  • access_token (defaults to: '')

    access_token of user

  • access_token_secret (defaults to: '')

    access_token_secret of user



87
88
89
90
91
92
93
94
95
96
# File 'lib/readit.rb', line 87

def initialize(access_token='',access_token_secret='')
  if access_token == '' or access_token_secret == ''
    raise ReaditError.new('have to provide access_token and access_token_secret')
  end
  if Readit::Config.consumer_key=='' or Readit::Config.consumer_secret==''
    raise ReaditError.new('please set Readit::Config.consumer_key or Readit::Config.consumer_secret first')
  end
  @access_token = access_token
  @access_token_secret = access_token_secret
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



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

def access_token
  @access_token
end

Instance Method Details

#add_tags(bookmark_id, tags_string) ⇒ Object

add tags to one bookmark api rest address POST :/bookmarks/bookmark_id/tags



225
226
227
# File 'lib/readit.rb', line 225

def add_tags(bookmark_id, tags_string)
  request(:post, "/bookmarks/#{bookmark_id}/tags",{ :tags => tags_string }).tags
end

#all_tagsObject

Retrieve all tags of current user api rest address :/tags



219
220
221
# File 'lib/readit.rb', line 219

def all_tags
  request(:get,"/tags")
end

#archive(bookmark_id) ⇒ Object

archive a bookmark by id

Parameters:

  • bookmark_id

    bookmark to archive



182
183
184
# File 'lib/readit.rb', line 182

def archive(bookmark_id)
  update_bookmark(bookmark_id,:archive=>1)
end

#article(article_id) ⇒ Object

Retrieve a single Article, including its content. api rest address: /articles/article_id

Parameters:

  • article_id

    the article_id



108
109
110
# File 'lib/readit.rb', line 108

def article(article_id)
  request(:get,"/articles/#{article_id}")
end

#bookmark(args = {}) ⇒ Object

Add a bookmark. Returns 202 Accepted, meaning that the bookmark has been added but no guarantees are made as to whether the article proper has yet been parsed. url the address to bookmark favorite 0 or 1 archive 0 or 1 Return example success: => ‘202’,:bookmark_id => ‘233444’, :article_id => ‘t323r2’ conflicts => ‘409’

Parameters:

  • args (defaults to: {})

    args to bookmark a url

Raises:



160
161
162
163
164
165
166
167
168
# File 'lib/readit.rb', line 160

def bookmark(args={})
  raise ReaditError.new('expect at lease a hash argument with key :url') unless args[:url]
  request(:post,'/bookmarks',args) do |response|
    Hashie::Mash.new({
      :status =>response.code,
      :bookmark_id=> ['202', '409'].include?(response.code) ? response["Location"].match(/bookmarks\/(.*)/)[1] : '',
      :article_id=> response.code== '202' ? response["X-Article-Location"].match(/articles\/(.*)/)[1] : ''})
  end
end

#bookmarks(args = {}) ⇒ Object

Retrieve the bookmarks collection. Automatically filtered to the current user. api rest address : /bookmarks? or /bookmarks/bookmark_id bookmark_id (at most return one record) archive favorite domain added_since added_until opened_since opened_until archived_since archived_until favorited_since favorited_until updated_since updated_until order page per_page default 20,max 50 exclude_accessibility only_deleted ***** Special args ***** to get bookmarks meta infos :include_meta => true item_count=20 item_count_total=633 num_pages=32 page=1 bookmarks,meta = @api.bookmarks(:include_meta => true)



139
140
141
142
143
144
145
146
147
# File 'lib/readit.rb', line 139

def bookmarks(args={})
  if args[:bookmark_id] and args[:bookmark_id]!=''
    request(:get,"/bookmarks/#{args[:bookmark_id]}")
  else
    params = args.map{|k,v| "#{k}=#{v}"}.join('&')
    result = request(:get,"/bookmarks?#{URI.escape(params)}")
    args[:include_meta] ? [result.bookmarks,result.meta] : result.bookmarks
  end
end

#contributions(args = {}) ⇒ Object

Retrieve the contributions collection, which is a set of payments by a user to a specific domain. Automatically filtered to the current user. api rest address : /contributions?since&until&domain&page&per_page



207
208
209
# File 'lib/readit.rb', line 207

def contributions(args={})
  request(:get,"/contributions",args)
end

#delete_bookmark(bookmark_id) ⇒ Object

Remove a single bookmark from this user’s history. NOTE: THIS IS PROBABLY NOT WHAT YOU WANT. This is particularly for the case where a user accidentally bookmarks something they have no intention of reading or supporting. In almost all cases, you’ll probably want to use archive by POSTing archive=1 to this bookmark. If you use DELETE and this months bookmarks have not yet been tallied, the site associated with this bookmark will not receive any contributions for this bookmark. Use archive! It’s better. Returns a 204 on successful remove.

Parameters:

  • bookmark_id

    bookmark to delete



201
202
203
# File 'lib/readit.rb', line 201

def delete_bookmark(bookmark_id)
  request(:delete,"/bookmarks/#{bookmark_id}")
end

#favorite(bookmark_id) ⇒ Object

favorite a bookmark by id

Parameters:

  • bookmark_id

    bookmark_id to favorite



188
189
190
# File 'lib/readit.rb', line 188

def favorite(bookmark_id)
  update_bookmark(bookmark_id,:favorite=>1)
end

#meObject

Retrieve the current user’s information. api rest address :/users/_current



213
214
215
# File 'lib/readit.rb', line 213

def me
  request(:get,"/users/_current")
end

#remove_tag(bookmark_id, tag_id) ⇒ Object

remove tag info from bookmark api rest address DELETE /bookmarks/bookmark_id/tags/tag_id



237
238
239
# File 'lib/readit.rb', line 237

def remove_tag(bookmark_id, tag_id)
  request(:delete, "/bookmarks/#{bookmark_id}/tags/#{tag_id}")
end

#resource_infoObject

Retrieve the base API URI - information about subresources.



101
102
103
# File 'lib/readit.rb', line 101

def resource_info
  request(:get,'/')
end

#tags(bookmark_id) ⇒ Object

Retrieve all tags of one bookmark api rest address GET :/bookmarks/bookmark_id/tags



231
232
233
# File 'lib/readit.rb', line 231

def tags(bookmark_id)
  request(:get, "/bookmarks/#{bookmark_id}/tags").tags
end

#update_bookmark(bookmark_id, args = {}) ⇒ Object

Update a bookmark. Returns 200 on successful update. api rest address : /bookmarks/bookmark_id favorite 0 or 1 archive 0 or 1

Parameters:

  • bookmark_id

    bookmark to update

  • args (defaults to: {})

    args to update the bookmark



176
177
178
# File 'lib/readit.rb', line 176

def update_bookmark(bookmark_id,args={})
  request(:post,"/bookmarks/#{bookmark_id}",args)
end