Class: Hexapic::API::Instagram
- Inherits:
-
Object
- Object
- Hexapic::API::Instagram
- Defined in:
- lib/hexapic/api.rb
Constant Summary collapse
- INSTAGRAM_API_URL =
'https://api.instagram.com/v1/'
Instance Method Summary collapse
-
#initialize(client_id) ⇒ Instagram
constructor
A new instance of Instagram.
- #search(tag, count = 20) ⇒ Object
- #search_by_user(username, count = 100) ⇒ Object
- #search_user(username) ⇒ Object
- #user_media(user_id, count) ⇒ Object
Constructor Details
#initialize(client_id) ⇒ Instagram
Returns a new instance of Instagram.
6 7 8 9 10 11 12 |
# File 'lib/hexapic/api.rb', line 6 def initialize(client_id) @client_id = client_id @conn = Faraday.new(url: INSTAGRAM_API_URL) do |faraday| faraday.request :url_encoded faraday.adapter :net_http end end |
Instance Method Details
#search(tag, count = 20) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/hexapic/api.rb', line 18 def search(tag, count = 20) tag = URI.encode(tag) res = @conn.get("tags/#{tag}/media/recent",{client_id: @client_id, count: count}) data = JSON.parse(res.body) data['data'].map do |img| { id: img['id'], likes: img['likes']['count'], link: img['link'], url: img['images']['standard_resolution']['url'], width: img['images']['standard_resolution']['width'], height: img['images']['standard_resolution']['height'] } end end |
#search_by_user(username, count = 100) ⇒ Object
14 15 16 |
# File 'lib/hexapic/api.rb', line 14 def search_by_user(username, count = 100) user_media(search_user(username), count) end |
#search_user(username) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/hexapic/api.rb', line 51 def search_user(username) puts "Searching user #{username}" username = URI.encode(username) res = @conn.get("users/search", {q: username, client_id: @client_id}) data = JSON.parse(res.body)['data'].first raise UserNotFound.new("User with name #{username} not found") if data.nil? puts "Find user #{data['username']} #{data['first_name']} #{data['last_name']} with id #{data['id']}" data['id'] end |
#user_media(user_id, count) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/hexapic/api.rb', line 35 def user_media(user_id, count) res = @conn.get("users/#{user_id}/media/recent/", {client_id: @client_id, count: count}) data = JSON.parse(res.body)['data'] data.map do |img| { id: img['id'], likes: img['likes']['count'], link: img['link'], url: img['images']['standard_resolution']['url'], width: img['images']['standard_resolution']['width'], height: img['images']['standard_resolution']['height'] } end end |