Class: Lard
- Inherits:
-
Object
- Object
- Lard
- Defined in:
- lib/lard.rb
Overview
A set of utility functions for working with the Larder HTTP API
Constant Summary collapse
- VERSION =
'0.0.8'.freeze
Instance Method Summary collapse
- #api_url_prefix ⇒ Object
- #authorized ⇒ Object
- #bookmarks(folder_id) ⇒ Object
- #folders ⇒ Object
-
#get(url, params = nil) ⇒ Object
Perform a GET request to an endpoint in the Larder API.
- #get_folder_by_name(name) ⇒ Object
-
#initialize(token = nil) ⇒ Lard
constructor
A new instance of Lard.
-
#post(endpoint, args = {}) ⇒ Object
Perform a POST request to an endpoint in the Larder API Posts args as JSON in the post body, where args is a hash.
- #tags ⇒ Object
Constructor Details
#initialize(token = nil) ⇒ Lard
Returns a new instance of Lard.
8 9 10 11 |
# File 'lib/lard.rb', line 8 def initialize(token = nil) @token = token @folders = [] end |
Instance Method Details
#api_url_prefix ⇒ Object
17 18 19 |
# File 'lib/lard.rb', line 17 def api_url_prefix 'https://larder.io/api/1/@me/' end |
#authorized ⇒ Object
13 14 15 |
# File 'lib/lard.rb', line 13 def !@token.nil? end |
#bookmarks(folder_id) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lard.rb', line 40 def bookmarks(folder_id) res = get "folders/#{folder_id}", limit: 200 bookmarks = res[:results] || [] until res[:next].nil? res = get res[:next] bookmarks.push(*res[:results]) end bookmarks end |
#folders ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/lard.rb', line 52 def folders res = get 'folders', limit: 200 @folders = res[:results] || @folders until res[:next].nil? res = get res[:next] @folders.push(*res[:results]) end @folders end |
#get(url, params = nil) ⇒ Object
Perform a GET request to an endpoint in the Larder API
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/lard.rb', line 65 def get(url, params = nil) raise "You're not logged in! Run 'lard login' first." unless # Make a URI based on whether we received a full URL or just endpoint uri = prepare_uri url uri.query = URI.encode_www_form params unless params.nil? res = Net::HTTP.start uri.host, uri.port, use_ssl: true do |http| http.request prepare_request 'get', uri end parse_response res end |
#get_folder_by_name(name) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/lard.rb', line 21 def get_folder_by_name(name) folders if @folders.empty? @folders.find do |folder| folder[:name] == name end end |
#post(endpoint, args = {}) ⇒ Object
Perform a POST request to an endpoint in the Larder API Posts args as JSON in the post body, where args is a hash
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/lard.rb', line 80 def post(endpoint, args = {}) raise "You're not logged in! Run 'lard login' first." unless uri = prepare_uri endpoint request = prepare_request 'post', uri request.set_form_data args res = Net::HTTP.start uri.host, uri.port, use_ssl: true do |http| http.request request end parse_response res end |
#tags ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/lard.rb', line 28 def res = get 'tags', limit: 200 = res[:results] || [] until res[:next].nil? res = get res[:next] .push(*res[:results]) end end |