|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require_relative "fb/version" |
4 | | - |
5 | | -require 'net/http' |
6 | | -require 'json' |
| 4 | +require_relative "object" |
7 | 5 |
|
8 | 6 | require_relative "fb/config" |
9 | 7 | require_relative "fb/http_request" |
10 | 8 |
|
11 | | -module Fb |
12 | | - class Error < StandardError; end |
13 | | - |
14 | | - class Business |
15 | | - attr_reader :id, :name |
16 | | - |
17 | | - def initialize(options = {}) |
18 | | - @access_token = options[:access_token] |
19 | | - @id = options[:id] |
20 | | - @name = options[:name] |
21 | | - end |
22 | | - |
23 | | - def owned_pages |
24 | | - @owned_pages ||= begin |
25 | | - params = { access_token: @access_token, fields: "category,name,access_token" } |
26 | | - request = HTTPRequest.new path: "/#{@id}/owned_pages", params: params |
27 | | - request.run.body['data'].map do |page_data| |
28 | | - # unless page_data.key?("access_token") |
29 | | - # page_data.merge! access_token: @access_token |
30 | | - # end |
31 | | - Page.new symbolize_keys(page_data) |
32 | | - end |
33 | | - end |
34 | | - end |
35 | | - |
36 | | - def client_pages |
37 | | - @client_pages ||= begin |
38 | | - params = { access_token: @access_token, fields: "category,name,access_token" } |
39 | | - request = HTTPRequest.new path: "/#{@id}/client_pages", params: params |
40 | | - request.run.body['data'].map do |page_data| |
41 | | - # unless page_data.key?("access_token") |
42 | | - # page_data.merge! access_token: @access_token |
43 | | - # end |
44 | | - Page.new symbolize_keys(page_data) |
45 | | - end |
46 | | - end |
47 | | - end |
48 | | - |
49 | | - private |
50 | | - |
51 | | - def symbolize_keys(hash) |
52 | | - {}.tap do |new_hash| |
53 | | - hash.each_key {|key| new_hash[key.to_sym] = hash[key] } |
54 | | - end |
55 | | - end |
56 | | - end |
57 | | - |
58 | | - class User |
59 | | - def initialize(options = {}) |
60 | | - @access_token = options[:access_token] |
61 | | - end |
62 | | - |
63 | | - def email |
64 | | - @email ||= begin |
65 | | - request = HTTPRequest.new path: '/me', params: { |
66 | | - fields: :email, |
67 | | - access_token: @access_token |
68 | | - } |
69 | | - request.run.body['email'] |
70 | | - end |
71 | | - end |
72 | | - |
73 | | - def revoke_permissions |
74 | | - params = { access_token: @access_token } |
75 | | - request = HTTPRequest.new path: '/me/permissions', params: params, method: :delete |
76 | | - request.run.body['success'] |
77 | | - end |
78 | | - |
79 | | - def pages |
80 | | - @pages ||= begin |
81 | | - params = { access_token: @access_token, fields: "category,name,access_token" } |
82 | | - request = HTTPRequest.new path: '/me/accounts', params: params |
83 | | - request.run.body['data'].map do |page_data| |
84 | | - # unless page_data.key?("access_token") |
85 | | - # page_data.merge! access_token: @access_token |
86 | | - # end |
87 | | - Page.new symbolize_keys(page_data) |
88 | | - end |
89 | | - end |
90 | | - end |
91 | | - |
92 | | - def businesses |
93 | | - @businesses ||= begin |
94 | | - params = { access_token: @access_token, fields: "name,access_token" } |
95 | | - request = HTTPRequest.new path: '/me/businesses', params: params |
96 | | - request.run.body['data'].map do |business_data| |
97 | | - unless business_data.key?("access_token") |
98 | | - business_data.merge! access_token: @access_token |
99 | | - end |
100 | | - Business.new symbolize_keys(business_data) |
101 | | - end |
102 | | - end |
103 | | - end |
104 | | - |
105 | | - private |
106 | | - |
107 | | - def symbolize_keys(hash) |
108 | | - {}.tap do |new_hash| |
109 | | - hash.each_key {|key| new_hash[key.to_sym] = hash[key] } |
110 | | - end |
111 | | - end |
112 | | - end |
113 | | - |
114 | | - class Page |
115 | | - attr_reader :id, :name, :category |
116 | | - |
117 | | - def initialize(options = {}) |
118 | | - @id = options[:id] |
119 | | - @name = options[:name] |
120 | | - @category = options[:category] |
121 | | - @access_token = options[:access_token] |
122 | | - end |
123 | | - |
124 | | - def thumbnail_url |
125 | | - "https://graph.facebook.com/#{@id}/picture?width=240&height=240" |
126 | | - end |
127 | | - |
128 | | - # # For test the page token - temporary code |
129 | | - # def feed |
130 | | - # params = { access_token: @access_token } |
131 | | - # request = HTTPRequest.new(path: "/#{@id}/feed", params: params) |
132 | | - # request.run.body['data'] |
133 | | - # end |
134 | | - |
135 | | - # Either link or message must be supplied. |
136 | | - # https://developers.facebook.com/docs/graph-api/reference/v21.0/page/feed#publish |
137 | | - def publish(options = {}) |
138 | | - params = { access_token: @access_token } |
139 | | - params[:link] = options[:link] if options[:link] |
140 | | - params[:message] = options[:message] if options[:message] |
141 | | - request = HTTPRequest.new(path: "/#{@id}/feed", method: :post, params: params) |
142 | | - request.run.body['id'] |
143 | | - end |
144 | | - |
145 | | - # crossposted_video_id must be provided. |
146 | | - # @see https://developers.facebook.com/docs/video-api/guides/crossposting |
147 | | - def crosspost_video(options = {}) |
148 | | - params = { access_token: @access_token } |
149 | | - params[:crossposted_video_id] = options[:crossposted_video_id] || "" |
150 | | - # params[:message] = options[:message] if options[:message] |
151 | | - request = HTTPRequest.new(path: "/#{@id}/videos", method: :post, params: params) |
152 | | - request.run.body['id'] |
153 | | - end |
154 | | - end |
155 | | -end |
| 9 | +require "fb/business" |
| 10 | +require "fb/error" |
| 11 | +require "fb/page" |
| 12 | +require "fb/user" |
0 commit comments