freelancer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. data/.gitignore +3 -0
  2. data/Gemfile +14 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +56 -0
  5. data/Rakefile +34 -0
  6. data/VERSION +1 -0
  7. data/freelancer.gemspec +299 -0
  8. data/init.rb +2 -0
  9. data/lib/freelancer.rb +59 -0
  10. data/lib/freelancer/api.rb +20 -0
  11. data/lib/freelancer/api/common.rb +58 -0
  12. data/lib/freelancer/api/employer.rb +56 -0
  13. data/lib/freelancer/api/freelancer.rb +36 -0
  14. data/lib/freelancer/api/job.rb +46 -0
  15. data/lib/freelancer/api/message.rb +89 -0
  16. data/lib/freelancer/api/notification.rb +25 -0
  17. data/lib/freelancer/api/payment.rb +123 -0
  18. data/lib/freelancer/api/profile.rb +30 -0
  19. data/lib/freelancer/api/project.rb +133 -0
  20. data/lib/freelancer/api/user.rb +86 -0
  21. data/lib/freelancer/client.rb +152 -0
  22. data/lib/freelancer/extensions/hash.rb +11 -0
  23. data/lib/freelancer/models/address.rb +46 -0
  24. data/lib/freelancer/models/balance.rb +22 -0
  25. data/lib/freelancer/models/bid.rb +41 -0
  26. data/lib/freelancer/models/config_version.rb +21 -0
  27. data/lib/freelancer/models/eligibility.rb +23 -0
  28. data/lib/freelancer/models/job.rb +33 -0
  29. data/lib/freelancer/models/job_category.rb +18 -0
  30. data/lib/freelancer/models/message.rb +32 -0
  31. data/lib/freelancer/models/milestone.rb +57 -0
  32. data/lib/freelancer/models/news.rb +15 -0
  33. data/lib/freelancer/models/notification.rb +15 -0
  34. data/lib/freelancer/models/notification_status.rb +45 -0
  35. data/lib/freelancer/models/project.rb +116 -0
  36. data/lib/freelancer/models/project_budget.rb +21 -0
  37. data/lib/freelancer/models/project_fee.rb +18 -0
  38. data/lib/freelancer/models/project_options.rb +34 -0
  39. data/lib/freelancer/models/rating.rb +21 -0
  40. data/lib/freelancer/models/review.rb +87 -0
  41. data/lib/freelancer/models/transaction.rb +39 -0
  42. data/lib/freelancer/models/user.rb +225 -0
  43. data/lib/freelancer/models/withdrawal.rb +51 -0
  44. data/lib/freelancer/models/withdrawal_fee.rb +39 -0
  45. data/lib/freelancer/support/json_mapper.rb +95 -0
  46. data/test/fixtures/common/get_config_version.json +1 -0
  47. data/test/fixtures/common/get_config_version.xml +5 -0
  48. data/test/fixtures/common/get_pending_feedback.json +1 -0
  49. data/test/fixtures/common/get_pending_feedback.xml +20 -0
  50. data/test/fixtures/common/get_terms.json +1 -0
  51. data/test/fixtures/common/get_terms.xml +4 -0
  52. data/test/fixtures/common/post_feedback.json +1 -0
  53. data/test/fixtures/common/post_feedback.xml +4 -0
  54. data/test/fixtures/common/post_reply_for_feedback.json +1 -0
  55. data/test/fixtures/common/post_reply_for_feedback.xml +4 -0
  56. data/test/fixtures/common/request_cancel_project.json +1 -0
  57. data/test/fixtures/common/request_cancel_project.xml +4 -0
  58. data/test/fixtures/common/request_withdraw_feedback.json +1 -0
  59. data/test/fixtures/common/request_withdraw_feedback.xml +4 -0
  60. data/test/fixtures/employer/choose_winner_for_project.json +1 -0
  61. data/test/fixtures/employer/choose_winner_for_project.xml +3 -0
  62. data/test/fixtures/employer/delete_draft_project.json +1 -0
  63. data/test/fixtures/employer/delete_draft_project.xml +4 -0
  64. data/test/fixtures/employer/eligible_for_trial_project.json +1 -0
  65. data/test/fixtures/employer/eligible_for_trial_project.xml +5 -0
  66. data/test/fixtures/employer/get_posted_project_list.json +1 -0
  67. data/test/fixtures/employer/get_posted_project_list.xml +38 -0
  68. data/test/fixtures/employer/invite_user_for_project.json +1 -0
  69. data/test/fixtures/employer/invite_user_for_project.xml +4 -0
  70. data/test/fixtures/employer/post_new_draft_project.json +1 -0
  71. data/test/fixtures/employer/post_new_draft_project.xml +5 -0
  72. data/test/fixtures/employer/post_new_project.json +1 -0
  73. data/test/fixtures/employer/post_new_project.xml +5 -0
  74. data/test/fixtures/employer/post_new_trial_project.json +1 -0
  75. data/test/fixtures/employer/post_new_trial_project.xml +5 -0
  76. data/test/fixtures/employer/publish_draft_project.json +1 -0
  77. data/test/fixtures/employer/publish_draft_project.xml +4 -0
  78. data/test/fixtures/employer/update_project_details.json +1 -0
  79. data/test/fixtures/employer/update_project_details.xml +4 -0
  80. data/test/fixtures/employer/upgrade_trial_project.json +1 -0
  81. data/test/fixtures/employer/upgrade_trial_project.xml +4 -0
  82. data/test/fixtures/freelancer/accept_bid_won.json +1 -0
  83. data/test/fixtures/freelancer/accept_bid_won.xml +4 -0
  84. data/test/fixtures/freelancer/get_project_list_for_placed_bids.json +1 -0
  85. data/test/fixtures/freelancer/get_project_list_for_placed_bids.xml +16 -0
  86. data/test/fixtures/freelancer/place_bid_on_project.json +1 -0
  87. data/test/fixtures/freelancer/place_bid_on_project.xml +4 -0
  88. data/test/fixtures/freelancer/retract_bid_from_project.json +1 -0
  89. data/test/fixtures/freelancer/retract_bid_from_project.xml +4 -0
  90. data/test/fixtures/job/get_category_job_list.json +1 -0
  91. data/test/fixtures/job/get_category_job_list.xml +34 -0
  92. data/test/fixtures/job/get_job_list.json +1 -0
  93. data/test/fixtures/job/get_job_list.xml +17 -0
  94. data/test/fixtures/job/get_my_job_list.json +1 -0
  95. data/test/fixtures/job/get_my_job_list.xml +11 -0
  96. data/test/fixtures/message/get_inbox_messages.json +1 -0
  97. data/test/fixtures/message/get_inbox_messages.xml +22 -0
  98. data/test/fixtures/message/get_sent_messages.json +1 -0
  99. data/test/fixtures/message/get_sent_messages.xml +22 -0
  100. data/test/fixtures/message/get_unread_count.json +1 -0
  101. data/test/fixtures/message/get_unread_count.xml +4 -0
  102. data/test/fixtures/message/load_message_thread.json +1 -0
  103. data/test/fixtures/message/load_message_thread.xml +54 -0
  104. data/test/fixtures/message/mark_message_as_read.json +1 -0
  105. data/test/fixtures/message/mark_message_as_read.xml +4 -0
  106. data/test/fixtures/message/send_message.json +1 -0
  107. data/test/fixtures/message/send_message.xml +4 -0
  108. data/test/fixtures/notification/get_news.json +1 -0
  109. data/test/fixtures/notification/get_news.xml +14 -0
  110. data/test/fixtures/notification/get_notification.json +1 -0
  111. data/test/fixtures/notification/get_notification.xml +10 -0
  112. data/test/fixtures/payment/cancel_milestone.json +1 -0
  113. data/test/fixtures/payment/cancel_milestone.xml +4 -0
  114. data/test/fixtures/payment/create_milestone_payment.json +1 -0
  115. data/test/fixtures/payment/create_milestone_payment.xml +4 -0
  116. data/test/fixtures/payment/get_account_balance_status.json +1 -0
  117. data/test/fixtures/payment/get_account_balance_status.xml +12 -0
  118. data/test/fixtures/payment/get_account_milestone_list.json +1 -0
  119. data/test/fixtures/payment/get_account_milestone_list.xml +30 -0
  120. data/test/fixtures/payment/get_account_transaction_list.json +1 -0
  121. data/test/fixtures/payment/get_account_transaction_list.xml +57 -0
  122. data/test/fixtures/payment/get_account_withdrawal_list.json +1 -0
  123. data/test/fixtures/payment/get_account_withdrawal_list.xml +42 -0
  124. data/test/fixtures/payment/get_balance.json +1 -0
  125. data/test/fixtures/payment/get_balance.xml +4 -0
  126. data/test/fixtures/payment/get_project_list_for_transfer.json +1 -0
  127. data/test/fixtures/payment/get_project_list_for_transfer.xml +52 -0
  128. data/test/fixtures/payment/get_withdrawal_fees.json +1 -0
  129. data/test/fixtures/payment/get_withdrawal_fees.xml +41 -0
  130. data/test/fixtures/payment/prepare_transfer.json +1 -0
  131. data/test/fixtures/payment/prepare_transfer.xml +4 -0
  132. data/test/fixtures/payment/release_milestone.json +1 -0
  133. data/test/fixtures/payment/release_milestone.xml +4 -0
  134. data/test/fixtures/payment/request_cancel_withdrawal.json +1 -0
  135. data/test/fixtures/payment/request_cancel_withdrawal.xml +4 -0
  136. data/test/fixtures/payment/request_release_milestone.json +1 -0
  137. data/test/fixtures/payment/request_release_milestone.xml +4 -0
  138. data/test/fixtures/payment/request_withdrawal.json +1 -0
  139. data/test/fixtures/payment/request_withdrawal.xml +6 -0
  140. data/test/fixtures/payment/transfer_money.json +1 -0
  141. data/test/fixtures/payment/transfer_money.xml +4 -0
  142. data/test/fixtures/profile/get_account_details.json +1 -0
  143. data/test/fixtures/profile/get_account_details.xml +71 -0
  144. data/test/fixtures/profile/get_profile_info.json +1 -0
  145. data/test/fixtures/profile/get_profile_info.xml +53 -0
  146. data/test/fixtures/profile/set_profile_info.json +1 -0
  147. data/test/fixtures/profile/set_profile_info.xml +4 -0
  148. data/test/fixtures/project/get_bids_details.json +1 -0
  149. data/test/fixtures/project/get_bids_details.xml +19 -0
  150. data/test/fixtures/project/get_project_budget_config.json +1 -0
  151. data/test/fixtures/project/get_project_budget_config.xml +42 -0
  152. data/test/fixtures/project/get_project_details.json +1 -0
  153. data/test/fixtures/project/get_project_details.xml +78 -0
  154. data/test/fixtures/project/get_project_fees.json +1 -0
  155. data/test/fixtures/project/get_project_fees.xml +31 -0
  156. data/test/fixtures/project/get_public_messages.json +1 -0
  157. data/test/fixtures/project/get_public_messages.xml +27 -0
  158. data/test/fixtures/project/get_sent_messages.xml +22 -0
  159. data/test/fixtures/project/get_unread_count.xml +4 -0
  160. data/test/fixtures/project/load_message_thread.xml +54 -0
  161. data/test/fixtures/project/mark_message_as_read.xml +4 -0
  162. data/test/fixtures/project/post_public_message.json +1 -0
  163. data/test/fixtures/project/post_public_message.xml +4 -0
  164. data/test/fixtures/project/search_projects.json +1 -0
  165. data/test/fixtures/project/search_projects.xml +37 -0
  166. data/test/fixtures/project/send_message.xml +4 -0
  167. data/test/fixtures/user/get_user_details.json +1 -0
  168. data/test/fixtures/user/get_user_details.xml +32 -0
  169. data/test/fixtures/user/get_user_feedback.json +1 -0
  170. data/test/fixtures/user/get_user_feedback.xml +58 -0
  171. data/test/fixtures/user/get_users_by_search.json +1 -0
  172. data/test/fixtures/user/get_users_by_search.xml +26 -0
  173. data/test/freelancer/api/common_api_test.rb +82 -0
  174. data/test/freelancer/api/employer_api_test.rb +105 -0
  175. data/test/freelancer/api/freelancer_api_test.rb +72 -0
  176. data/test/freelancer/api/job_api_test.rb +76 -0
  177. data/test/freelancer/api/message_api_test.rb +144 -0
  178. data/test/freelancer/api/notification_api_test.rb +56 -0
  179. data/test/freelancer/api/payment_api_test.rb +225 -0
  180. data/test/freelancer/api/profile_api_test.rb +54 -0
  181. data/test/freelancer/api/project_api_test.rb +229 -0
  182. data/test/freelancer/api/user_api_test.rb +153 -0
  183. data/test/freelancer/client_test.rb +5 -0
  184. data/test/freelancer/models/balance_test.rb +24 -0
  185. data/test/freelancer/models/bid_test.rb +25 -0
  186. data/test/freelancer/models/config_version.rb +14 -0
  187. data/test/freelancer/models/eligibility_test.rb +14 -0
  188. data/test/freelancer/models/job_category_test.rb +16 -0
  189. data/test/freelancer/models/job_test.rb +19 -0
  190. data/test/freelancer/models/message_test.rb +42 -0
  191. data/test/freelancer/models/milestone_test.rb +21 -0
  192. data/test/freelancer/models/news_test.rb +15 -0
  193. data/test/freelancer/models/notification_test.rb +15 -0
  194. data/test/freelancer/models/project_budget_test.rb +18 -0
  195. data/test/freelancer/models/project_fee_test.rb +17 -0
  196. data/test/freelancer/models/project_test.rb +97 -0
  197. data/test/freelancer/models/review_test.rb +36 -0
  198. data/test/freelancer/models/transaction_test.rb +29 -0
  199. data/test/freelancer/models/user_test.rb +94 -0
  200. data/test/freelancer/models/withdrawal_fee_test.rb +19 -0
  201. data/test/freelancer/models/withdrawal_test.rb +20 -0
  202. data/test/freelancer/oauth_test.rb +112 -0
  203. data/test/freelancer_test.rb +1 -0
  204. data/test/test_helper.rb +42 -0
  205. metadata +427 -0
@@ -0,0 +1,30 @@
1
+ module Freelancer
2
+ module API
3
+ module Profile
4
+ module InstanceMethods
5
+
6
+ # Retrieve profile information for the current user
7
+ def account_details
8
+
9
+ result = api_get("/Profile/getAccountDetails.json")
10
+ ::Freelancer::Models::User.parse(result, :shift => :"json-result")
11
+
12
+ end
13
+
14
+ # Retrieve profile information about a specific user
15
+ #
16
+ # Valid parameters are:
17
+ # - user_id: the user id to retrieve profile for
18
+ def profile_info(*args)
19
+
20
+ params = extract_params(args)
21
+
22
+ result = api_get("/Profile/getProfileInfo.json", { :userid => params[:user_id] })
23
+ ::Freelancer::Models::User.parse(result, :shift => :"json-result")
24
+
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,133 @@
1
+ module Freelancer
2
+ module API
3
+ module Project
4
+ module InstanceMethods
5
+
6
+ # Search for projects using a specified set of criterias.
7
+ #
8
+ # Valid parameters are:
9
+ # - featured: if only featured projects should be returned
10
+ # - non_public: if only non-public projects should be returned
11
+ # - keyword: the keyword to use for searching
12
+ # - job: a specific job type, or an array of job types, to search for
13
+ # - status: the project status to filter projects by
14
+ # - minimum_budget: the minimum budget to return projects by
15
+ # - maximum_budget: the maximum budget to return projects by
16
+ # - full_time: if only full time projects should be returned
17
+ # - trial: if only trial projects should be returned
18
+ # - gold_members_only: if only gold member only-projects should be returned
19
+ # - duration: the bid end duration for the project (submitdate (default), bid_enddate, id or state)
20
+ # - count: the number of results to return (defaults to 50)
21
+ # - page: the page to retrieve (defaults to 0)
22
+ def project_search(*args)
23
+
24
+ params = extract_params(args)
25
+
26
+ # Handle the job attribute, make sure it's an array and not set to nil to
27
+ # simplify the handling below.
28
+ params[:job] ||= []
29
+ params[:job] = params[:job].to_a unless params[:job].is_a?(Array)
30
+
31
+ # Delete some boolean data from the params if the values are set to fault,
32
+ # since that's the default already
33
+ [ :featured, :non_public, :full_time, :trial, :gold_members_only ].each do |k|
34
+ params.delete(k) if params.key?(k) && params[k] == false
35
+ end
36
+
37
+ # Execute the service call
38
+ result = api_get("/Project/searchProjects.json", build_api_params({
39
+ :isfeatured => params[:featured],
40
+ :isnonpublic => params[:non_public],
41
+ :searchkeyword => params[:keyword],
42
+ :searchjobtypecsv => params[:job].join(","),
43
+ :status => params[:status],
44
+ :budgetmin => params[:minimum_budget],
45
+ :budgetmax => params[:maximum_budget],
46
+ :isfulltime => params[:full_time],
47
+ :istrial => params[:trial],
48
+ :isgoldmembersonly => params[:gold_members_only],
49
+ :bidendsduration => params[:duration],
50
+ :count => params[:count],
51
+ :page => params[:page]
52
+ }))
53
+
54
+ # Parse and return the response
55
+ ::Freelancer::Models::Project.parse_collection(result, :shift => [ :"json-result", :items ])
56
+
57
+ end
58
+
59
+ # Retrieve the available project type fees
60
+ def project_fees
61
+
62
+ result = api_get("/Project/getProjectFees.json")
63
+ ::Freelancer::Models::ProjectFee.parse_collection(result, :shift => [ :"json-result", :items ])
64
+
65
+ end
66
+
67
+ # Retrieve information about a project
68
+ #
69
+ # Valid parameters are:
70
+ # - project_id: the id of the project to retrieve
71
+ def project_details(*args)
72
+
73
+ params = extract_params(args)
74
+
75
+ # Execute the service call
76
+ result = api_get("/Project/getProjectDetails.json", build_api_params({
77
+ :projectid => params[:project_id]
78
+ }))
79
+
80
+ # Parse and return the response
81
+ ::Freelancer::Models::Project.parse(result, :shift => :"json-result")
82
+
83
+ end
84
+
85
+ # Retrieve the bids for a project
86
+ #
87
+ # Valid parameters are:
88
+ # - project_id: the id of the project to retrieve bids for
89
+ def project_bids(*args)
90
+
91
+ params = extract_params(args)
92
+
93
+ # Execute the service call
94
+ result = api_get("/Project/getBidsDetails.json", build_api_params({
95
+ :projectid => params[:project_id]
96
+ }))
97
+
98
+ # Parse and return the response
99
+ ::Freelancer::Models::Bid.parse_collection(result, :shift => [ :"json-result", :items ])
100
+
101
+ end
102
+
103
+ # Retrieve the public messages for a project
104
+ #
105
+ # Valid parameters are:
106
+ # - project_id: the id of the project to retrieve bids for
107
+ def public_messages(*args)
108
+
109
+ params = extract_params(args)
110
+
111
+ # Execute the service call
112
+ result = api_get("/Project/getPublicMessages.json", build_api_params({
113
+ :projectid => params[:project_id]
114
+ }))
115
+
116
+ # Parse and return the response
117
+ ::Freelancer::Models::Message.parse_collection(result, :shift => [ :"json-result", :items ])
118
+
119
+ end
120
+
121
+ # Retrieve the project budget configurations
122
+ def project_budgets
123
+
124
+ result = api_get("/Project/getProjectBudgetConfig.json")
125
+ ::Freelancer::Models::ProjectBudget.parse_collection(result, :shift => [ :"json-result", :items ])
126
+
127
+ end
128
+
129
+ end
130
+
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,86 @@
1
+ module Freelancer
2
+ module API
3
+ module User
4
+ module InstanceMethods
5
+
6
+ # Search for users
7
+ #
8
+ # Valid parameters are:
9
+ # - username: the username to search for
10
+ # - expertise: a specific expertise, or an array of expertises, to search for
11
+ # - country: a specific country, or an array of countries, to search for
12
+ # - rating: the minimum rating to search for
13
+ # - count: the number of results to return (defaults to 50)
14
+ # - page: the page to retrieve (defaults to 0)
15
+ def user_search(*args)
16
+
17
+ params = extract_params(args)
18
+
19
+ # Handle the expertise and country attributes. Make sure they're an array and not set
20
+ # to nil to simplify the handling below.
21
+ params[:country] ||= []
22
+ params[:country] = params[:country].to_a unless params[:country].is_a?(Array)
23
+ params[:expertise] ||= []
24
+ params[:expertise] = params[:expertise].to_a unless params[:expertise].is_a?(Array)
25
+
26
+ # Execute the service call
27
+ result = api_get("/User/getUsersBySearch.json", build_api_params({
28
+ :username => params[:username],
29
+ :expertise_csv => params[:expertise].join(","),
30
+ :country_csv => params[:country].join(","),
31
+ :rating => params[:rating],
32
+ :count => params[:count],
33
+ :page => params[:page]
34
+ }))
35
+
36
+ # Parse and return the response
37
+ ::Freelancer::Models::User.parse_collection(result, :shift => [ :"json-result", :items ])
38
+
39
+ end
40
+
41
+ # Search for users
42
+ #
43
+ # Valid parameters are:
44
+ # - username: the username to list feedback for
45
+ # - user_id: the user id to list feedback for
46
+ # - type: the type of feedback to show ("P" = provider only, "B" = buyer only, "A" = all (default))
47
+ def user_feedback(*args)
48
+
49
+ params = extract_params(args)
50
+
51
+ # Execute the service call
52
+ result = api_get("/User/getUserFeedback.json", build_api_params({
53
+ :username => params[:username],
54
+ :userid => params[:user_id],
55
+ :type => params[:type]
56
+ }))
57
+
58
+ # Parse and return the response
59
+ ::Freelancer::Models::Review.parse_collection(result, :shift => [ :user, :totalreviews, :items ])
60
+
61
+ end
62
+
63
+ # Retrieve user details for a specific user
64
+ #
65
+ # Valid parameters are:
66
+ # - username: the username to retrieve details for
67
+ # - user_id: the user id to retrieve details for
68
+ def user_details(*args)
69
+
70
+ params = extract_params(args)
71
+
72
+ # Execute the service call
73
+ result = api_get("/User/getUserDetails.json", build_api_params({
74
+ :username => params[:username],
75
+ :userid => params[:user_id]
76
+ }))
77
+
78
+ # Parse and return the response
79
+ ::Freelancer::Models::User.parse(result, :shift => :user)
80
+
81
+ end
82
+
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,152 @@
1
+ module Freelancer
2
+ class Client
3
+
4
+ extend API
5
+
6
+ # Load the different API plugins
7
+ api API::User
8
+ api API::Job
9
+ api API::Profile
10
+ api API::Employer
11
+ api API::Freelancer
12
+ api API::Common
13
+ api API::Payment
14
+ api API::Notification
15
+ api API::Project
16
+ api API::Message
17
+
18
+ attr_reader :consumer_token, :consumer_secret, :consumer_options
19
+
20
+ # Initialize a new Freelancer API client
21
+ def initialize(consumer_token, consumer_secret, consumer_options = {})
22
+
23
+ default_options = {
24
+ :request_token_path => "/RequestRequestToken/requestRequestToken.xml",
25
+ :authorize_url => "http://www.sandbox.freelancer.com/users/api-token/auth.php",
26
+ :access_token_path => "/RequestAccessToken/requestAccessToken.xml",
27
+ :scheme => :query_string,
28
+ :http_method => :get
29
+ }
30
+
31
+ @consumer_token, @consumer_secret, @consumer_options = consumer_token, consumer_secret, consumer_options.merge(default_options)
32
+ @api_endpoint = "http://api.sandbox.freelancer.com"
33
+
34
+ end
35
+
36
+ # Get the current oAuth consumer instance
37
+ def consumer
38
+ @consumer ||= ::OAuth::Consumer.new(consumer_token, consumer_secret, { :site => @api_endpoint }.merge(consumer_options))
39
+ end
40
+
41
+ # Set the callback URL for an oAuth request
42
+ def set_callback_url(url)
43
+ clear_request_token
44
+ request_token :oauth_callback => url
45
+ end
46
+
47
+ # Get the current oAuth request token
48
+ def request_token(options = {})
49
+ @request_token_instance ||= consumer.get_request_token(options)
50
+ end
51
+
52
+ # Authorize a user from an oAuth request
53
+ def authorize_from_request(request_token, request_secret, verifier)
54
+
55
+ request_token = ::OAuth::RequestToken.new(consumer, request_token, request_secret)
56
+ access_token = request_token.get_access_token(:oauth_verifier => verifier)
57
+ @access_token, @access_secret = access_token.token, access_token.secret
58
+
59
+ end
60
+
61
+ # Get the current oAuth access token
62
+ def access_token
63
+ @access_token_instance ||= ::OAuth::AccessToken.new(consumer, @access_token, @access_secret)
64
+ end
65
+
66
+ # Authorize a user from an oAuth access token
67
+ def authorize_from_access(access_token, access_secret)
68
+ @access_token, @access_secret = access_token, access_secret
69
+ end
70
+
71
+ # Execute a GET-request for the specified API method
72
+ def api_get(method, options = {})
73
+ response = access_token.get(to_uri(method, options))
74
+ raise_api_errors!(response)
75
+ return response.body
76
+ end
77
+
78
+ # Execute a GET-requset for the specified API method and return the raw
79
+ # result.
80
+ def api_get_raw(method, options = {})
81
+ response = access_token.get(to_uri(method, options))
82
+ raise_api_errors!(response)
83
+ response.body
84
+ end
85
+
86
+ # Execute a POST-request for the specified API method
87
+ # TODO: Finish this method, it should probably return some JSON decoded
88
+ # string.
89
+ def api_post(method, body)
90
+ response = access_token.post(method, body)
91
+ response.body
92
+ end
93
+
94
+ private
95
+
96
+ # Extract params from an array of arguments. Copied from ActiveSupport.
97
+ def extract_params(args)
98
+ args.last.is_a?(Hash) ? args.last : {}
99
+ end
100
+
101
+ # Build a hash of parameters based on the arguments, stripping out any blank
102
+ # arguments.
103
+ def build_api_params(params)
104
+ params.reject { |key, value| value.nil? || value == "" }
105
+ end
106
+
107
+ # Clear the current oAuth request token
108
+ def clear_request_token
109
+ @request_token_instance = nil
110
+ end
111
+
112
+ # Raise an error for the specified API method response if any
113
+ def raise_api_errors!(response)
114
+
115
+ case response.code.to_i
116
+ when 1..199
117
+ raise "Response code #{response.code}: #{response}"
118
+ when 201..503
119
+ raise "Response code: #{response.code}: #{response}"
120
+ end
121
+
122
+ end
123
+
124
+ # Create a URI from the specified method and options
125
+ def to_uri(method, options)
126
+
127
+ uri = URI.parse(method)
128
+ unless options.nil? || options.empty?
129
+ uri.query = to_query(options)
130
+ end
131
+
132
+ return uri.to_s
133
+
134
+ end
135
+
136
+ # Convert a hash to a proper query string
137
+ def to_query(params)
138
+ params.inject([]) do |collection, options|
139
+ options[1] = URI.escape(options[1]) if options[1].is_a?(String) && !options[1].nil?
140
+ collection << "#{options[0]}=#{options[1]}"
141
+ collection
142
+ end * "&"
143
+ end
144
+
145
+ # Decode any HTML entities in the given string
146
+ def html_decode(str)
147
+ encoder = HTMLEntities.new
148
+ encoder.decode(str)
149
+ end
150
+
151
+ end
152
+ end
@@ -0,0 +1,11 @@
1
+ class Hash
2
+
3
+ # Symbolize the keys in the hash. Copied from ActiveSupport's Core Extensions.
4
+ def symbolize_keys
5
+ inject({}) do |options, (key, value)|
6
+ options[(key.to_sym rescue key) || key] = value
7
+ options
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,46 @@
1
+ module Freelancer
2
+ module Models
3
+ class Address
4
+
5
+ include JSONMapper
6
+
7
+ # The first streetname of the address
8
+ #
9
+ # Provided from:
10
+ # - /Profile/getAccountDetails.json
11
+ json_attribute :street_name, :addressline1, String
12
+
13
+ # The second streetname of the address
14
+ #
15
+ # Provided from:
16
+ # - /Profile/getAccountDetails.json
17
+ json_attribute :street_name_2, :addressline2, String
18
+
19
+ # The postal code of the address
20
+ #
21
+ # Provided from:
22
+ # - /Profile/getAccountDetails.json
23
+ json_attribute :postal_code, :postalcode, String
24
+
25
+ # The city part of the address
26
+ #
27
+ # Provided from:
28
+ # - /User/getUserDetails.json
29
+ json_attribute :city, String
30
+
31
+
32
+ # The state part of the address
33
+ #
34
+ # Provided from:
35
+ # - /Profile/getAccountDetails.json
36
+ json_attribute :state, String
37
+
38
+ # The country the address is located in
39
+ #
40
+ # Provided from:
41
+ # - /User/getUserDetails.json
42
+ json_attribute :country, String
43
+
44
+ end
45
+ end
46
+ end