Skip to content

Commit

Permalink
Introduce basic_auth method to DRY up authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
twe4ked committed Jul 12, 2014
1 parent c23d1c8 commit d5dc78c
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions lib/feedbin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,49 @@ def initialize(email, password)
# Entries

def entries(options = {})
HTTParty.get("https://api.feedbin.me/v2/entries.json", query: options, basic_auth: { username: @email, password: @password })
HTTParty.get("https://api.feedbin.me/v2/entries.json", query: options, basic_auth: basic_auth)
end

def entry(id)
HTTParty.get("https://api.feedbin.me/v2/entries/#{id}.json", basic_auth: { username: @email, password: @password })
HTTParty.get("https://api.feedbin.me/v2/entries/#{id}.json", basic_auth: basic_auth)
end

def unread_entries
HTTParty.get("https://api.feedbin.me/v2/unread_entries.json", basic_auth: { username: @email, password: @password })
HTTParty.get("https://api.feedbin.me/v2/unread_entries.json", basic_auth: basic_auth)
end

def star(id)
HTTParty.post("https://api.feedbin.me/v2/starred_entries.json",
body: { 'starred_entries' => id }.to_json,
headers: { 'Content-Type' => 'application/json' },
basic_auth: { username: @email, password: @password }).code
basic_auth: basic_auth).code
end

def unstar(id)
HTTParty.post("https://api.feedbin.me/v2/starred_entries/delete.json",
body: { 'starred_entries' => id }.to_json,
headers: { 'Content-Type' => 'application/json' },
basic_auth: { username: @email, password: @password }).code
basic_auth: basic_auth).code
end

def mark_as_read(id)
HTTParty.post("https://api.feedbin.me/v2/unread_entries/delete.json",
body: { 'unread_entries' => id }.to_json,
headers: { 'Content-Type' => 'application/json' },
basic_auth: { username: @email, password: @password }).code
basic_auth: basic_auth).code
end

def mark_as_unread(id)
HTTParty.post("https://api.feedbin.me/v2/unread_entries.json",
body: { 'unread_entries' => id }.to_json,
headers: { 'Content-Type' => 'application/json' },
basic_auth: { username: @email, password: @password }).code
basic_auth: basic_auth).code
end

# Feeds

def feed(id)
HTTParty.get("https://api.feedbin.me/v2/feeds/#{id}.json", basic_auth: { username: @email, password: @password })
HTTParty.get("https://api.feedbin.me/v2/feeds/#{id}.json", basic_auth: basic_auth)
end

# Subscriptions
Expand All @@ -63,19 +63,25 @@ def subscribe(url)
HTTParty.post("https://api.feedbin.me/v2/subscriptions.json",
body: { 'feed_url' => url }.to_json,
headers: { 'Content-Type' => 'application/json' },
basic_auth: { username: @email, password: @password }).code
basic_auth: basic_auth).code
end

def unsubscribe(id)
HTTParty.delete("https://api.feedbin.me/v2/subscriptions/#{id}.json", basic_auth: { username: @email, password: @password }).code
HTTParty.delete("https://api.feedbin.me/v2/subscriptions/#{id}.json", basic_auth: basic_auth).code
end

def subscriptions(options = {})
if options[:since]
resp = HTTParty.get("https://api.feedbin.me/v2/subscriptions.json", query: { since: options[:since] }, basic_auth: { username: @email, password: @password })
resp = HTTParty.get("https://api.feedbin.me/v2/subscriptions.json", query: { since: options[:since] }, basic_auth: basic_auth)
return resp == [] ? resp : 'There have been no subscriptions since this date.'.to_json unless resp.code != 200
else
HTTParty.get("https://api.feedbin.me/v2/subscriptions.json", basic_auth: { username: @email, password: @password })
HTTParty.get("https://api.feedbin.me/v2/subscriptions.json", basic_auth: basic_auth)
end
end

private

def basic_auth
{ username: @email, password: @password }
end
end

0 comments on commit d5dc78c

Please sign in to comment.